async def test_process_callback_always(self): """Test process / reading telegrams from telegram queue. Test if callback is called.""" xknx = XKNX() sensor = NumericValue( xknx, "TestSensor", group_address="1/2/3", value_type="temperature", always_callback=True, ) after_update_callback = AsyncMock() sensor.register_device_updated_cb(after_update_callback) telegram = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTArray((0x01, 0x02))), ) await sensor.process(telegram) after_update_callback.assert_called_with(sensor) assert sensor.last_telegram == telegram # every telegram shall trigger callback after_update_callback.reset_mock() await sensor.process(telegram) after_update_callback.assert_called_with(sensor)
async def test_respond_to_read(self): """Test respond_to_read function.""" xknx = XKNX() responding = NumericValue( xknx, "TestSensor1", group_address="1/1/1", respond_to_read=True, value_type="volume_liquid_litre", ) non_responding = NumericValue( xknx, "TestSensor2", group_address="1/1/1", respond_to_read=False, value_type="volume_liquid_litre", ) responding_multiple = NumericValue( xknx, "TestSensor3", group_address=["1/1/1", "3/3/3"], group_address_state="2/2/2", respond_to_read=True, value_type="volume_liquid_litre", ) # set initial payload of NumericValue responding.sensor_value.value = 256 non_responding.sensor_value.value = 256 responding_multiple.sensor_value.value = 256 read_telegram = Telegram(destination_address=GroupAddress("1/1/1"), payload=GroupValueRead()) # verify no response when respond is False await non_responding.process(read_telegram) assert xknx.telegrams.qsize() == 0 # verify response when respond is True await responding.process(read_telegram) assert xknx.telegrams.qsize() == 1 response = xknx.telegrams.get_nowait() assert response == Telegram( destination_address=GroupAddress("1/1/1"), payload=GroupValueResponse(DPTArray((0x00, 0x00, 0x01, 0x00))), ) # verify no response when GroupValueRead request is not for group_address await responding_multiple.process(read_telegram) assert xknx.telegrams.qsize() == 1 response = xknx.telegrams.get_nowait() assert response == Telegram( destination_address=GroupAddress("1/1/1"), payload=GroupValueResponse(DPTArray((0x00, 0x00, 0x01, 0x00))), ) await responding_multiple.process( Telegram(destination_address=GroupAddress("2/2/2"), payload=GroupValueRead())) await responding_multiple.process( Telegram(destination_address=GroupAddress("3/3/3"), payload=GroupValueRead())) assert xknx.telegrams.qsize() == 0
async def test_process_callback_set(self): """Test setting value. Test if callback is called.""" xknx = XKNX() after_update_callback = AsyncMock() num_value = NumericValue( xknx, "TestSensor", group_address="1/2/3", value_type="temperature" ) num_value.register_device_updated_cb(after_update_callback) await num_value.set(21.0) await xknx.devices.process(xknx.telegrams.get_nowait()) after_update_callback.assert_called_with(num_value)
async def test_process(self): """Test process / reading telegrams from telegram queue.""" xknx = XKNX() sensor = NumericValue( xknx, "TestSensor", value_type="temperature", group_address="1/2/3" ) telegram = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTArray((0x06, 0xA0))), ) await sensor.process(telegram) assert sensor.sensor_value.value == 16.96 assert sensor.sensor_value.telegram.payload.value == DPTArray((0x06, 0xA0)) assert sensor.resolve_state() == 16.96
async def test_set_temperature(self): """Test set with temperature numeric value.""" xknx = XKNX() num_value = NumericValue( xknx, "TestSensor", group_address="1/2/3", value_type="temperature" ) await num_value.set(21.0) assert xknx.telegrams.qsize() == 1 telegram = xknx.telegrams.get_nowait() assert telegram == Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTArray((0x0C, 0x1A))), ) # test HomeAssistant device class assert num_value.ha_device_class() == "temperature"
def _create_numeric_value(xknx: XKNX, config: ConfigType) -> NumericValue: """Return a KNX NumericValue to be used within XKNX.""" return NumericValue( xknx, name=config[CONF_NAME], group_address=config[KNX_ADDRESS], group_address_state=config.get(CONF_STATE_ADDRESS), respond_to_read=config[CONF_RESPOND_TO_READ], value_type=config[CONF_TYPE], )
async def test_sensor_value_types( self, value_type, raw_payload, expected_state, ): """Test sensor value types.""" xknx = XKNX() sensor = NumericValue( xknx, "TestSensor", group_address="1/2/3", value_type=value_type, ) await sensor.process( Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(value=raw_payload), )) assert sensor.resolve_state() == expected_state
async def test_sync(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX() sensor = NumericValue(xknx, "TestSensor", value_type="temperature", group_address_state="1/2/3") await sensor.sync() assert xknx.telegrams.qsize() == 1 telegram = xknx.telegrams.get_nowait() assert telegram == Telegram(destination_address=GroupAddress("1/2/3"), payload=GroupValueRead())
def test_string(self): """Test NumericValue string representation.""" xknx = XKNX() value = NumericValue( xknx, "Num", group_address="1/2/3", value_type="temperature" ) value.sensor_value.value = 4.9 assert ( str(value) == '<NumericValue name="Num" addresses=<1/2/3, None, [], 4.9 /> value=4.9 unit="°C"/>' )
async def test_set_percent(self): """Test set with percent numeric value.""" xknx = XKNX() num_value = NumericValue( xknx, "TestSensor", group_address="1/2/3", value_type="percent" ) await num_value.set(75) assert xknx.telegrams.qsize() == 1 telegram = xknx.telegrams.get_nowait() assert telegram == Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTArray((0xBF,))), )