def test_wrong_do(self): """Test wrong do command.""" xknx = XKNX(loop=self.loop) switch = Switch(xknx, 'TestOutlet', group_address='1/2/3') with patch('logging.Logger.warning') as mock_warn: self.loop.run_until_complete(asyncio.Task(switch.do("execute"))) mock_warn.assert_called_with('Could not understand action %s for device %s', 'execute', 'TestOutlet') self.assertEqual(xknx.telegrams.qsize(), 0)
def test_do(self): """Test 'do' functionality.""" xknx = XKNX(loop=self.loop) switch = Switch(xknx, 'TestOutlet', group_address='1/2/3') self.loop.run_until_complete(asyncio.Task(switch.do("on"))) self.assertTrue(switch.state) self.loop.run_until_complete(asyncio.Task(switch.do("off"))) self.assertFalse(switch.state)
def test_set_on(self): """Test switching on switch.""" xknx = XKNX(loop=self.loop) switch = Switch(xknx, 'TestOutlet', group_address='1/2/3') self.loop.run_until_complete(asyncio.Task(switch.set_on())) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual(telegram, Telegram(GroupAddress('1/2/3'), payload=DPTBinary(1)))
def test_sync(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX(loop=self.loop) switch = Switch(xknx, "TestOutlet", group_address_state='1/2/3') self.loop.run_until_complete(asyncio.Task(switch.sync(False))) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual(telegram, Telegram(GroupAddress('1/2/3'), TelegramType.GROUP_READ))
def parse_group_switch(self, entries): """Parse a switch section of xknx.yaml.""" for entry in entries: switch = Switch.from_config( self.xknx, entry, entries[entry]) self.xknx.devices.add(switch)
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_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)
async 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( destination_address=GroupAddress("1/2/3"), payload=GroupValueResponse(DPTBinary(1)), ) await switch.process(telegram_on) assert switch.state assert xknx.telegrams.qsize() == 0 await asyncio.sleep(reset_after_sec / 2) # half way through the reset timer await switch.process(telegram_on) assert switch.state await asyncio.sleep(reset_after_sec / 2) assert xknx.telegrams.qsize() == 0
def test_process_action(self): """Test process / reading telegrams from telegram queue. Test if action is executed.""" xknx = XKNX(loop=self.loop) switch = Switch(xknx, 'TestOutlet', group_address='1/2/3') xknx.devices.add(switch) binary_sensor = BinarySensor(xknx, 'TestInput', group_address='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) xknx.devices.add(binary_sensor) self.assertEqual(xknx.devices['TestInput'].state, BinarySensorState.OFF) self.assertEqual(xknx.devices['TestOutlet'].state, False) telegram_on = Telegram() telegram_on.payload = DPTBinary(1) self.loop.run_until_complete( asyncio.Task(binary_sensor.process(telegram_on))) self.assertEqual(xknx.devices['TestInput'].state, BinarySensorState.ON) self.assertEqual(xknx.devices['TestOutlet'].state, True) telegram_off = Telegram() telegram_off.payload = DPTBinary(0) self.loop.run_until_complete( asyncio.Task(binary_sensor.process(telegram_off))) self.assertEqual(xknx.devices['TestInput'].state, BinarySensorState.OFF) self.assertEqual(xknx.devices['TestOutlet'].state, False)
def test_has_group_address(self): """Test has_group_address.""" xknx = XKNX(loop=self.loop) switch = Switch(xknx, 'TestOutlet', group_address='1/2/3') self.assertTrue(switch.has_group_address(GroupAddress('1/2/3'))) self.assertFalse(switch.has_group_address(GroupAddress('2/2/2')))
def parse_group_switch(self, entries): """Parse a switch section of xknx.yaml.""" for entry in entries: switch = Switch.from_config(self.xknx, entry, entries[entry]) self.xknx.devices.add(switch)
def test_config_switch(self): """Test reading Switch from config file.""" self.assertEqual( TestConfig.xknx.devices["Livingroom.Outlet_2"], Switch(TestConfig.xknx, "Livingroom.Outlet_2", group_address="1/3/2"), )
def test_has_group_address(self): """Test has_group_address.""" xknx = XKNX() switch = Switch(xknx, "TestOutlet", group_address="1/2/3") assert switch.has_group_address(GroupAddress("1/2/3")) assert not switch.has_group_address(GroupAddress("2/2/2"))
def test_has_group_address(self): """Test has_group_address.""" xknx = XKNX() switch = Switch(xknx, "TestOutlet", group_address="1/2/3") self.assertTrue(switch.has_group_address(GroupAddress("1/2/3"))) self.assertFalse(switch.has_group_address(GroupAddress("2/2/2")))
def test_unique_id(self): """Test unique id functionality.""" xknx = XKNX() switch = Switch(xknx, "TestOutlet", group_address="1/2/3") assert switch.unique_id == "1/2/3"
def switch(self, *a, **k): res = Switch(self._client, *a, **k) self._client.devices.add(res) return res