def test_process_state(self): """Test process / reading telegrams from telegram queue. Test if device was updated.""" xknx = XKNX() callback_mock = AsyncMock() switch1 = Switch( xknx, "TestOutlet", group_address="1/2/3", group_address_state="1/2/4", device_updated_cb=callback_mock, ) switch2 = Switch( xknx, "TestOutlet", group_address="1/2/3", group_address_state="1/2/4", device_updated_cb=callback_mock, ) self.assertEqual(switch1.state, None) self.assertEqual(switch2.state, None) callback_mock.assert_not_called() telegram_on = Telegram( destination_address=GroupAddress("1/2/4"), payload=GroupValueResponse(DPTBinary(1)), ) telegram_off = Telegram( destination_address=GroupAddress("1/2/4"), payload=GroupValueResponse(DPTBinary(0)), ) self.loop.run_until_complete(switch1.process(telegram_on)) self.assertEqual(switch1.state, True) callback_mock.assert_called_once() callback_mock.reset_mock() self.loop.run_until_complete(switch1.process(telegram_off)) self.assertEqual(switch1.state, False) callback_mock.assert_called_once() callback_mock.reset_mock() # test setting switch2 to False with first telegram self.loop.run_until_complete(switch2.process(telegram_off)) self.assertEqual(switch2.state, False) callback_mock.assert_called_once() callback_mock.reset_mock() self.loop.run_until_complete(switch2.process(telegram_on)) self.assertEqual(switch2.state, True) callback_mock.assert_called_once() callback_mock.reset_mock()
def test_process_invert(self): """Test process / reading telegrams from telegram queue with inverted switch.""" xknx = XKNX() switch = Switch(xknx, "TestOutlet", group_address="1/2/3", invert=True) self.assertEqual(switch.state, False) telegram_on = Telegram( group_address=GroupAddress("1/2/3"), payload=DPTBinary(0) ) self.loop.run_until_complete(switch.process(telegram_on)) self.assertEqual(switch.state, True) telegram_off = Telegram( group_address=GroupAddress("1/2/3"), payload=DPTBinary(1) ) self.loop.run_until_complete(switch.process(telegram_off)) self.assertEqual(switch.state, False)
def test_process_reset_after(self): """Test process reset_after.""" xknx = XKNX() reset_after_sec = 0.001 switch = Switch( xknx, "TestInput", group_address="1/2/3", reset_after=reset_after_sec ) telegram_on = Telegram( group_address=GroupAddress("1/2/3"), payload=DPTBinary(1) ) self.loop.run_until_complete(switch.process(telegram_on)) self.assertTrue(switch.state) self.assertEqual(xknx.telegrams.qsize(), 0) self.loop.run_until_complete(asyncio.sleep(reset_after_sec * 2)) self.assertEqual(xknx.telegrams.qsize(), 1) self.loop.run_until_complete(switch.process(xknx.telegrams.get_nowait())) self.assertFalse(switch.state)
def test_process(self): """Test process / reading telegrams from telegram queue. Test if device was updated.""" xknx = XKNX(loop=self.loop) switch = Switch(xknx, 'TestOutlet', group_address='1/2/3') self.assertEqual(switch.state, False) telegram_on = Telegram() telegram_on.group_address = GroupAddress('1/2/3') telegram_on.payload = DPTBinary(1) self.loop.run_until_complete(asyncio.Task(switch.process(telegram_on))) self.assertEqual(switch.state, True) telegram_off = Telegram() telegram_off.group_address = GroupAddress('1/2/3') telegram_off.payload = DPTBinary(0) self.loop.run_until_complete(asyncio.Task(switch.process(telegram_off))) self.assertEqual(switch.state, False)
def test_process(self): """Test process / reading telegrams from telegram queue. Test if device was updated.""" xknx = XKNX(loop=self.loop) switch = Switch(xknx, 'TestOutlet', group_address='1/2/3') self.assertEqual(switch.state, False) telegram_on = Telegram() telegram_on.group_address = GroupAddress('1/2/3') telegram_on.payload = DPTBinary(1) self.loop.run_until_complete(asyncio.Task(switch.process(telegram_on))) self.assertEqual(switch.state, True) telegram_off = Telegram() telegram_off.group_address = GroupAddress('1/2/3') telegram_off.payload = DPTBinary(0) self.loop.run_until_complete(asyncio.Task(switch.process(telegram_off))) self.assertEqual(switch.state, False)
def test_process_action(self): """Test process / reading telegrams from telegram queue. Test if action is executed.""" xknx = XKNX() switch = Switch(xknx, "TestOutlet", group_address="1/2/3") binary_sensor = BinarySensor(xknx, "TestInput", group_address_state="1/2/3") action_on = Action(xknx, hook="on", target="TestOutlet", method="on") binary_sensor.actions.append(action_on) action_off = Action(xknx, hook="off", target="TestOutlet", method="off") binary_sensor.actions.append(action_off) self.assertEqual(binary_sensor.state, None) self.assertEqual(switch.state, None) telegram_on = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(1)), ) self.loop.run_until_complete(binary_sensor.process(telegram_on)) # process outgoing telegram from queue self.loop.run_until_complete( switch.process(xknx.telegrams.get_nowait())) self.assertEqual(binary_sensor.state, True) self.assertEqual(switch.state, True) telegram_off = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(0)), ) self.loop.run_until_complete(binary_sensor.process(telegram_off)) self.loop.run_until_complete( switch.process(xknx.telegrams.get_nowait())) self.assertEqual(binary_sensor.state, False) self.assertEqual(switch.state, False)
def test_process_reset_after_cancel_existing(self): """Test process reset_after cancels existing reset tasks.""" xknx = XKNX() reset_after_sec = 0.01 switch = Switch( xknx, "TestInput", group_address="1/2/3", reset_after=reset_after_sec ) telegram_on = Telegram( group_address=GroupAddress("1/2/3"), payload=DPTBinary(1) ) self.loop.run_until_complete(switch.process(telegram_on)) self.assertTrue(switch.state) self.assertEqual(xknx.telegrams.qsize(), 0) self.loop.run_until_complete(asyncio.sleep(reset_after_sec / 2)) # half way through the reset timer self.loop.run_until_complete(switch.process(telegram_on)) self.assertTrue(switch.state) self.loop.run_until_complete(asyncio.sleep(reset_after_sec / 2)) self.assertEqual(xknx.telegrams.qsize(), 0)
def test_process_callback(self): """Test process / reading telegrams from telegram queue. Test if callback was called.""" # pylint: disable=no-self-use xknx = XKNX() switch = Switch(xknx, "TestOutlet", group_address="1/2/3") after_update_callback = Mock() async def async_after_update_callback(device): """Async callback.""" after_update_callback(device) switch.register_device_updated_cb(async_after_update_callback) telegram = Telegram(group_address=GroupAddress("1/2/3"), payload=DPTBinary(1)) self.loop.run_until_complete(switch.process(telegram)) after_update_callback.assert_called_with(switch)
def test_process_callback(self): """Test process / reading telegrams from telegram queue. Test if callback was called.""" # pylint: disable=no-self-use xknx = XKNX(loop=self.loop) switch = Switch(xknx, 'TestOutlet', group_address='1/2/3') after_update_callback = Mock() async def async_after_update_callback(device): """Async callback.""" after_update_callback(device) switch.register_device_updated_cb(async_after_update_callback) telegram = Telegram() telegram.group_address = GroupAddress('1/2/3') telegram.payload = DPTBinary(1) self.loop.run_until_complete(asyncio.Task(switch.process(telegram))) after_update_callback.assert_called_with(switch)
def test_process_callback(self): """Test process / reading telegrams from telegram queue. Test if callback was called.""" # pylint: disable=no-self-use xknx = XKNX(loop=self.loop) switch = Switch(xknx, 'TestOutlet', group_address='1/2/3') after_update_callback = Mock() @asyncio.coroutine def async_after_update_callback(device): """Async callback.""" after_update_callback(device) switch.register_device_updated_cb(async_after_update_callback) telegram = Telegram() telegram.group_address = Address('1/2/3') telegram.payload = DPTBinary(1) self.loop.run_until_complete(asyncio.Task(switch.process(telegram))) after_update_callback.assert_called_with(switch)