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 test_rate_limit(self, async_sleep_mock): """Test rate limit.""" xknx = XKNX() xknx.rate_limit = 20 # 50 ms per outgoing telegram sleep_time = 0.05 # 1 / 20 telegram_in = Telegram( direction=TelegramDirection.INCOMING, payload=GroupValueWrite(DPTBinary(1)), ) telegram_out = Telegram( direction=TelegramDirection.OUTGOING, payload=GroupValueWrite(DPTBinary(1)), ) telegram_internal = Telegram( direction=TelegramDirection.OUTGOING, payload=GroupValueWrite(DPTBinary(1)), destination_address=InternalGroupAddress("i-test"), ) await xknx.telegram_queue.start() # no sleep for incoming telegrams xknx.telegrams.put_nowait(telegram_in) xknx.telegrams.put_nowait(telegram_in) await xknx.telegrams.join() assert async_sleep_mock.call_count == 0 # sleep for outgoing telegrams xknx.telegrams.put_nowait(telegram_out) xknx.telegrams.put_nowait(telegram_out) await xknx.telegrams.join() assert async_sleep_mock.call_count == 2 async_sleep_mock.assert_called_with(sleep_time) async_sleep_mock.reset_mock() # no sleep for internal group address telegrams xknx.telegrams.put_nowait(telegram_internal) xknx.telegrams.put_nowait(telegram_internal) await xknx.telegrams.join() async_sleep_mock.assert_not_called() await xknx.telegram_queue.stop()
def test_rate_limit(self, async_sleep_mock): """Test rate limit.""" # pylint: disable=no-self-use async def async_none(): return None async_sleep_mock.return_value = asyncio.ensure_future(async_none()) xknx = XKNX() xknx.rate_limit = 20 # 50 ms per outgoing telegram sleep_time = 0.05 # 1 / 20 telegram_in = Telegram( direction=TelegramDirection.INCOMING, payload=DPTBinary(1), group_address=GroupAddress("1/2/3"), ) telegram_out = Telegram( direction=TelegramDirection.OUTGOING, payload=DPTBinary(1), group_address=GroupAddress("1/2/3"), ) self.loop.run_until_complete(xknx.telegram_queue.start()) # no sleep for incoming telegrams xknx.telegrams.put_nowait(telegram_in) xknx.telegrams.put_nowait(telegram_in) self.loop.run_until_complete(xknx.telegrams.join()) self.assertEqual(async_sleep_mock.call_count, 0) # sleep for outgoing telegrams xknx.telegrams.put_nowait(telegram_out) xknx.telegrams.put_nowait(telegram_out) self.loop.run_until_complete(xknx.telegrams.join()) self.assertEqual(async_sleep_mock.call_count, 2) async_sleep_mock.assert_called_with(sleep_time) self.loop.run_until_complete(xknx.telegram_queue.stop())
async def test_binary_sensor_loop(self, value_type, test_payload, test_value): """Test binary_sensor and expose_sensor with binary values.""" xknx = XKNX() xknx.knxip_interface = AsyncMock() xknx.rate_limit = False telegram_callback = AsyncMock() xknx.telegram_queue.register_telegram_received_cb( telegram_callback, address_filters=[AddressFilter("i-test")], match_for_outgoing=True, ) await xknx.telegram_queue.start() expose = ExposeSensor( xknx, "TestExpose", group_address="i-test", value_type=value_type, ) assert expose.resolve_state() is None await expose.set(test_value) await xknx.telegrams.join() outgoing_telegram = Telegram( destination_address=InternalGroupAddress("i-test"), direction=TelegramDirection.OUTGOING, payload=GroupValueWrite(test_payload), ) # InternalGroupAddress isn't passed to knxip_interface xknx.knxip_interface.send_telegram.assert_not_called() telegram_callback.assert_called_with(outgoing_telegram) assert expose.resolve_state() == test_value bin_sensor = BinarySensor(xknx, "TestSensor", group_address_state="i-test") assert bin_sensor.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 bin_sensor.sync(wait_for_result=True) read_telegram = Telegram( destination_address=InternalGroupAddress("i-test"), direction=TelegramDirection.OUTGOING, payload=GroupValueRead(), ) response_telegram = Telegram( destination_address=InternalGroupAddress("i-test"), direction=TelegramDirection.OUTGOING, payload=GroupValueResponse(test_payload), ) xknx.knxip_interface.send_telegram.assert_not_called() telegram_callback.assert_has_calls( [ call(read_telegram), call(response_telegram), ] ) # test if Sensor has successfully read from ExposeSensor assert bin_sensor.state == test_value assert expose.resolve_state() == bin_sensor.state await xknx.telegram_queue.stop()