def test_sync_angle(self): """Test sync function for cover with angle.""" xknx = XKNX() cover = Cover( xknx, "TestCover", group_address_long="1/2/1", group_address_short="1/2/2", group_address_position_state="1/2/3", group_address_angle_state="1/2/4", ) self.loop.run_until_complete(cover.sync()) self.assertEqual(xknx.telegrams.qsize(), 2) telegram1 = xknx.telegrams.get_nowait() self.assertEqual( telegram1, Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueRead() ), ) telegram2 = xknx.telegrams.get_nowait() self.assertEqual( telegram2, Telegram( destination_address=GroupAddress("1/2/4"), payload=GroupValueRead() ), )
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_respond_to_read(self): """Test respond_to_read function.""" xknx = XKNX() responding = Switch( xknx, "TestSensor1", group_address="1/1/1", respond_to_read=True, ) non_responding = Switch( xknx, "TestSensor2", group_address="1/1/1", respond_to_read=False, ) responding_multiple = Switch( xknx, "TestSensor3", group_address=["1/1/1", "3/3/3"], group_address_state="2/2/2", respond_to_read=True, ) # set initial payload of Switch responding.switch.value = True non_responding.switch.value = True responding_multiple.switch.value = True 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(DPTBinary(True)), ) # 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(DPTBinary(True)), ) 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
def test_telegram_not_equal(self): """Test not equals operator.""" assert Telegram(GroupAddress("1/2/3"), payload=GroupValueRead()) != Telegram( GroupAddress("1/2/4"), payload=GroupValueRead()) assert Telegram( GroupAddress("1/2/3"), payload=GroupValueRead()) != Telegram( GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(1))) assert Telegram(GroupAddress("1/2/3"), payload=GroupValueRead()) != Telegram( GroupAddress("1/2/3"), TelegramDirection.INCOMING, payload=GroupValueRead(), )
def test_EndTOEnd_group_read(self): """Test parsing and streaming CEMIFrame KNX/IP packet, group read.""" # State request raw = bytes.fromhex("0610053000112900BCD0FFF901B8010000") xknx = XKNX() knxipframe = KNXIPFrame(xknx) knxipframe.from_knx(raw) telegram = knxipframe.body.cemi.telegram self.assertEqual( telegram, Telegram( destination_address=GroupAddress("440"), payload=GroupValueRead(), source_address=IndividualAddress("15.15.249"), ), ) cemi = CEMIFrame(xknx, src_addr=IndividualAddress("15.15.249")) cemi.telegram = telegram cemi.set_hops(5) routing_indication = RoutingIndication(xknx, cemi=cemi) knxipframe2 = KNXIPFrame.init_from_body(routing_indication) self.assertEqual(knxipframe2.header.to_knx(), list(raw[0:6])) self.assertEqual(knxipframe2.body.to_knx(), list(raw[6:])) self.assertEqual(knxipframe2.to_knx(), list(raw))
async def test_process(self): """Test if telegram is handled by the correct process_* method.""" xknx = XKNX() device = Device(xknx, "TestDevice") with patch("xknx.devices.Device.process_group_read", new_callable=AsyncMock) as mock_group_read: telegram = Telegram(destination_address=GroupAddress("1/2/1"), payload=GroupValueRead()) await device.process(telegram) mock_group_read.assert_called_with(telegram) with patch("xknx.devices.Device.process_group_write", new_callable=AsyncMock) as mock_group_write: telegram = Telegram( destination_address=GroupAddress("1/2/1"), payload=GroupValueWrite(DPTArray((0x01, 0x02))), ) await device.process(telegram) mock_group_write.assert_called_with(telegram) with patch("xknx.devices.Device.process_group_response", new_callable=AsyncMock) as mock_group_response: telegram = Telegram( destination_address=GroupAddress("1/2/1"), payload=GroupValueResponse(DPTArray((0x01, 0x02))), ) await device.process(telegram) mock_group_response.assert_called_with(telegram)
def test_process_read(self): """Test test process a read telegram from KNX bus.""" xknx = XKNX() datetime = DateTime(xknx, "TestDateTime", group_address="1/2/3", broadcast_type="TIME") telegram_read = Telegram(destination_address=GroupAddress("1/2/3"), payload=GroupValueRead()) with patch("time.localtime") as mock_time: mock_time.return_value = time.struct_time( [2017, 1, 7, 9, 13, 14, 6, 0, 0]) self.loop.run_until_complete(datetime.process(telegram_read)) # initial Telegram from broadcasting on init self.assertEqual(xknx.telegrams.qsize(), 2) _throwaway_initial = xknx.telegrams.get_nowait() telegram = xknx.telegrams.get_nowait() self.assertEqual( telegram, Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueResponse(DPTArray((0xE9, 0xD, 0xE))), ), )
async def test_array_sensor_loop(self, value_type, test_payload, test_value): """Test sensor and expose_sensor with different values.""" xknx = XKNX() xknx.knxip_interface = AsyncMock() xknx.rate_limit = False await xknx.telegram_queue.start() expose = ExposeSensor( xknx, "TestExpose", group_address="1/1/1", value_type=value_type, ) assert expose.resolve_state() is None # set a value from expose - HA sends strings for new values stringified_value = str(test_value) await expose.set(stringified_value) outgoing_telegram = Telegram( destination_address=GroupAddress("1/1/1"), direction=TelegramDirection.OUTGOING, payload=GroupValueWrite(test_payload), ) await xknx.telegrams.join() xknx.knxip_interface.send_telegram.assert_called_with(outgoing_telegram) assert expose.resolve_state() == test_value # init sensor after expose is set - with same group address sensor = Sensor( xknx, "TestSensor", group_address_state="1/1/1", value_type=value_type, ) assert sensor.resolve_state() is None # read sensor state (from expose as it has the same GA) # wait_for_result so we don't have to await self.xknx.telegrams.join() await sensor.sync(wait_for_result=True) read_telegram = Telegram( destination_address=GroupAddress("1/1/1"), direction=TelegramDirection.OUTGOING, payload=GroupValueRead(), ) response_telegram = Telegram( destination_address=GroupAddress("1/1/1"), direction=TelegramDirection.OUTGOING, payload=GroupValueResponse(test_payload), ) xknx.knxip_interface.send_telegram.assert_has_calls( [ call(read_telegram), call(response_telegram), ] ) # test if Sensor has successfully read from ExposeSensor assert sensor.resolve_state() == test_value assert expose.resolve_state() == sensor.resolve_state() await xknx.telegram_queue.stop()
async def service_read_to_knx_bus(self, call: ServiceCall) -> None: """Service for sending a GroupValueRead telegram to the KNX bus.""" for address in call.data[KNX_ADDRESS]: telegram = Telegram( destination_address=parse_device_group_address(address), payload=GroupValueRead(), ) await self.xknx.telegrams.put(telegram)
async def service_read_to_knx_bus(self, call): """Service for sending a GroupValueRead telegram to the KNX bus.""" for address in call.data.get(KNX_ADDRESS): telegram = Telegram( destination_address=GroupAddress(address), payload=GroupValueRead(), ) await self.xknx.telegrams.put(telegram)
async def send_group_read(self) -> None: """Send group read.""" telegram = Telegram( destination_address=self.group_address, payload=GroupValueRead(), source_address=self.xknx.current_address, ) await self.xknx.telegrams.put(telegram)
async def test_sync(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX() fan = Fan(xknx, name="TestFan", group_address_speed_state="1/2/3") await fan.sync() assert xknx.telegrams.qsize() == 1 telegram1 = xknx.telegrams.get_nowait() assert telegram1 == Telegram(destination_address=GroupAddress("1/2/3"), payload=GroupValueRead())
def test_valid_command(frame): """Test for valid frame parsing""" packet_len = frame.from_knx(get_data(0x29, 0, 0, 0, 0, 1, 0, [])) assert frame.code == CEMIMessageCode.L_DATA_IND assert frame.flags == 0 assert frame.mpdu_len == 1 assert frame.payload == GroupValueRead() assert frame.src_addr == IndividualAddress(0) assert frame.dst_addr == IndividualAddress(0) assert packet_len == 11
async def test_value_reader_send_group_read(self): """Test value reader: send_group_read.""" xknx = XKNX() value_reader = ValueReader(xknx, GroupAddress("0/0/0")) await value_reader.send_group_read() assert xknx.telegrams.qsize() == 1 telegram = xknx.telegrams.get_nowait() assert telegram == Telegram(destination_address=GroupAddress("0/0/0"), payload=GroupValueRead())
async def test_sync_angle(self): """Test sync function for cover with angle.""" xknx = XKNX() cover = Cover( xknx, "TestCover", group_address_long="1/2/1", group_address_short="1/2/2", group_address_position_state="1/2/3", group_address_angle_state="1/2/4", ) await cover.sync() assert xknx.telegrams.qsize() == 2 telegram1 = xknx.telegrams.get_nowait() assert telegram1 == Telegram(destination_address=GroupAddress("1/2/3"), payload=GroupValueRead()) telegram2 = xknx.telegrams.get_nowait() assert telegram2 == Telegram(destination_address=GroupAddress("1/2/4"), payload=GroupValueRead())
def test_invalid_src_addr(): """Test for invalid src addr.""" frame = CEMIFrame() frame.code = CEMIMessageCode.L_DATA_IND frame.flags = 0 frame.payload = GroupValueRead() frame.src_addr = GroupAddress(0) frame.dst_addr = IndividualAddress(0) with pytest.raises(ConversionError, match=r"src_addr invalid"): frame.to_knx()
def test_invalid_dst_addr(frame): """Test for invalid dst addr""" frame.code = CEMIMessageCode.L_DATA_IND frame.flags = 0 frame.mpdu_len = 1 frame.payload = GroupValueRead() frame.src_addr = IndividualAddress(0) frame.dst_addr = None with raises(ConversionError, match=r"dst_addr not set"): frame.to_knx()
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())
async def test_sync_state_address(self): """Test sync function / sending group reads to KNX bus. Test with Switch with explicit state address.""" xknx = XKNX() switch = Switch(xknx, "TestOutlet", group_address="1/2/3", group_address_state="1/2/4") await switch.sync() assert xknx.telegrams.qsize() == 1 telegram = xknx.telegrams.get_nowait() assert telegram == Telegram(destination_address=GroupAddress("1/2/4"), payload=GroupValueRead())
async def test_sync_state(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX() notification = Notification(xknx, "Warning", group_address="1/2/3", group_address_state="1/2/4") await notification.sync() assert xknx.telegrams.qsize() == 1 telegram = xknx.telegrams.get_nowait() assert telegram == Telegram(destination_address=GroupAddress("1/2/4"), payload=GroupValueRead())
def test_valid_command(): """Test for valid frame parsing.""" raw = get_data(0x29, 0, 0x0080, 0, 0, 1, 0, []) frame = CEMIFrame() packet_len = frame.from_knx(raw) assert frame.code == CEMIMessageCode.L_DATA_IND assert frame.flags == 0x0080 assert frame.src_addr == IndividualAddress(0) assert frame.dst_addr == GroupAddress(0) assert frame.payload == GroupValueRead() assert frame.tpci == TDataGroup() assert packet_len == 11 assert frame.calculated_length() == 11 assert frame.to_knx() == raw
def test_value_reader_send_group_read(self): """Test value reader: send_group_read.""" xknx = XKNX() value_reader = ValueReader(xknx, GroupAddress("0/0/0")) self.loop.run_until_complete(value_reader.send_group_read()) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual( telegram, Telegram( destination_address=GroupAddress("0/0/0"), payload=GroupValueRead() ), )
def test_sync(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX() fan = Fan(xknx, name="TestFan", group_address_speed_state="1/2/3") self.loop.run_until_complete(fan.sync()) self.assertEqual(xknx.telegrams.qsize(), 1) telegram1 = xknx.telegrams.get_nowait() self.assertEqual( telegram1, Telegram(destination_address=GroupAddress("1/2/3"), payload=GroupValueRead()), )
def test_sync_state(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX() notification = Notification(xknx, "Warning", group_address="1/2/3", group_address_state="1/2/4") self.loop.run_until_complete(notification.sync()) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual( telegram, Telegram(destination_address=GroupAddress("1/2/4"), payload=GroupValueRead()), )
async def test_sync(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX() cover = Cover( xknx, "TestCover", group_address_long="1/2/1", group_address_short="1/2/2", group_address_position_state="1/2/3", ) await cover.sync() assert xknx.telegrams.qsize() == 1 telegram1 = xknx.telegrams.get_nowait() assert telegram1 == Telegram(destination_address=GroupAddress("1/2/3"), payload=GroupValueRead())
def test_sync(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX() switch = Switch(xknx, "TestOutlet", group_address_state="1/2/3") self.loop.run_until_complete(switch.sync()) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual( telegram, Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueRead() ), )
async def test_sync_state(self): """Test sync function with explicit state address.""" xknx = XKNX() cover = Cover( xknx, "TestCoverSyncState", group_address_long="1/2/1", group_address_short="1/2/2", group_address_position="1/2/3", group_address_position_state="1/2/4", ) await cover.sync() assert xknx.telegrams.qsize() == 1 telegram1 = xknx.telegrams.get_nowait() assert telegram1 == Telegram(destination_address=GroupAddress("1/2/4"), payload=GroupValueRead())
def test_value_reader_telegram_received(self): """Test value reader: telegram_received.""" xknx = XKNX() test_group_address = GroupAddress("0/0/0") expected_telegram_1 = Telegram( destination_address=test_group_address, direction=TelegramDirection.INCOMING, payload=GroupValueResponse(DPTBinary(1)), ) expected_telegram_2 = Telegram( destination_address=test_group_address, direction=TelegramDirection.INCOMING, payload=GroupValueWrite(DPTBinary(1)), ) telegram_wrong_address = Telegram( destination_address=GroupAddress("0/0/1"), direction=TelegramDirection.INCOMING, payload=GroupValueResponse(DPTBinary(1)), ) telegram_wrong_type = Telegram( destination_address=test_group_address, direction=TelegramDirection.INCOMING, payload=GroupValueRead(), ) value_reader = ValueReader(xknx, test_group_address) def async_telegram_received(test_telegram): return self.loop.run_until_complete( value_reader.telegram_received(test_telegram) ) async_telegram_received(telegram_wrong_address) self.assertIsNone(value_reader.received_telegram) self.assertFalse(value_reader.success) async_telegram_received(telegram_wrong_type) self.assertIsNone(value_reader.received_telegram) self.assertFalse(value_reader.success) async_telegram_received(expected_telegram_1) self.assertEqual(value_reader.received_telegram, expected_telegram_1) self.assertTrue(value_reader.success) async_telegram_received(expected_telegram_2) self.assertEqual(value_reader.received_telegram, expected_telegram_2) self.assertTrue(value_reader.success)
def test_sync(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX() cover = Cover( xknx, "TestCover", group_address_long="1/2/1", group_address_short="1/2/2", group_address_position_state="1/2/3", ) self.loop.run_until_complete(cover.sync()) self.assertEqual(xknx.telegrams.qsize(), 1) telegram1 = xknx.telegrams.get_nowait() self.assertEqual( telegram1, Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueRead() ), )
def test_process_temperature(self): """Test reading temperature expose sensor from bus.""" xknx = XKNX() expose_sensor = ExposeSensor( xknx, "TestSensor", value_type="temperature", group_address="1/2/3" ) expose_sensor.sensor_value.payload = DPTArray((0x0C, 0x1A)) telegram = Telegram(GroupAddress("1/2/3"), payload=GroupValueRead()) self.loop.run_until_complete(expose_sensor.process(telegram)) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual( telegram, Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueResponse(DPTArray((0x0C, 0x1A))), ), )