def test_execute_unknown_device(self): """Test if execute method of Action calls correct do method of device.""" xknx = XKNX(loop=self.loop) action = Action(xknx, target='Light1', method='on') with patch('logging.Logger.warning') as logger_warning_mock: self.loop.run_until_complete(asyncio.Task(action.execute())) logger_warning_mock.assert_called_once_with( "Unknown device %s witin action %s.", action.target, action)
def test_execute_action(self): """Test if execute method of Action calls correct do method of device.""" xknx = XKNX(loop=self.loop) light = Light(xknx, "Light1", group_address_switch="1/6/4") action = Action(xknx, target=light.name, method="on") with patch("xknx.devices.Light.do") as mock_do: fut = asyncio.Future() fut.set_result(None) mock_do.return_value = fut self.loop.run_until_complete(action.execute()) mock_do.assert_called_with("on")
def test_execute_action(self): """Test if execute method of Action calls correct do method of device.""" xknx = XKNX(loop=self.loop) light = Light(xknx, 'Light1', group_address_switch='1/6/4') xknx.devices.add(light) action = Action(xknx, target='Light1', method='on') with patch('xknx.devices.Light.do') as mock_do: fut = asyncio.Future() fut.set_result(None) mock_do.return_value = fut self.loop.run_until_complete(asyncio.Task(action.execute())) mock_do.assert_called_with('on')
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_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) 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_execute_action(self): """Test if execute method of Action calls correct do method of device.""" xknx = XKNX(loop=self.loop) light = Light( xknx, 'Light1', group_address_switch='1/6/4') xknx.devices.add(light) action = Action(xknx, target='Light1', method='on') with patch('xknx.devices.Light.do') as mock_do: fut = asyncio.Future() fut.set_result(None) mock_do.return_value = fut self.loop.run_until_complete(asyncio.Task(action.execute())) mock_do.assert_called_with('on')
def test_config_binary_sensor(self): """Test reading BinarySensor from config file.""" self.assertEqual( TestConfig.xknx.devices['Livingroom.Switch_1'], BinarySensor(TestConfig.xknx, 'Livingroom.Switch_1', group_address_state='1/2/7', actions=[ Action(TestConfig.xknx, target="Livingroom.Outlet_1", method="on"), Action(TestConfig.xknx, target="Livingroom.Outlet_2", counter=2, method="on")], device_updated_cb=TestConfig.xknx.devices.device_updated))
def test_config_binary_sensor(self): """Test reading BinarySensor from config file.""" xknx = XKNX(config='xknx.yaml', loop=self.loop) self.assertEqual( xknx.devices['Livingroom.Switch_1'], BinarySensor(xknx, 'Livingroom.Switch_1', group_address='1/2/7', actions=[ Action(xknx, target="Livingroom.Outlet_1", method="on"), Action(xknx, target="Livingroom.Outlet_2", counter=2, method="on")], device_updated_cb=xknx.devices.device_updated))
def test_config_binary_sensor(self): """Test reading BinarySensor from config file.""" self.assertEqual( TestConfig.xknx.devices["Livingroom.Switch_1"], BinarySensor( TestConfig.xknx, "Livingroom.Switch_1", group_address_state="1/2/7", actions=[ Action(TestConfig.xknx, target="Livingroom.Outlet_1", method="on"), Action( TestConfig.xknx, target="Livingroom.Outlet_2", counter=2, method="on", ), ], ), )
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_action(self): """Test string representation of action.""" xknx = XKNX(loop=self.loop) action = Action(xknx, hook="on", target='Licht1', method='off', counter=2) self.assertEqual( str(action), '<Action target="Licht1" method="off" <ActionBase hook="on" counter="2"/>/>' )
def test_process_action_ignore_internal_state(self): """Test process / reading telegrams from telegram queue. Test if action is executed.""" xknx = XKNX(loop=self.loop) switch = Switch(xknx, 'TestOutlet', group_address='5/5/5') xknx.devices.add(switch) binary_sensor = BinarySensor(xknx, 'TestInput', group_address_state='1/2/3', ignore_internal_state=True) action_on = Action( xknx, hook='on', target='TestOutlet', method='on') binary_sensor.actions.append(action_on) xknx.devices.add(binary_sensor) self.assertEqual( xknx.devices['TestInput'].state, False) self.assertEqual( xknx.devices['TestOutlet'].state, False) telegram_on = Telegram(group_address=GroupAddress('1/2/3')) telegram_on.payload = DPTBinary(1) self.loop.run_until_complete(asyncio.Task(binary_sensor.process(telegram_on))) self.assertEqual( xknx.devices['TestInput'].state, True) self.assertEqual( xknx.devices['TestOutlet'].state, True) self.loop.run_until_complete(asyncio.Task(switch.set_off())) self.assertEqual( xknx.devices['TestOutlet'].state, False) self.assertEqual( xknx.devices['TestInput'].state, True) self.loop.run_until_complete(asyncio.sleep(1)) self.loop.run_until_complete(asyncio.Task(binary_sensor.process(telegram_on))) self.assertEqual( xknx.devices['TestInput'].state, True) self.assertEqual( xknx.devices['TestOutlet'].state, True)
def test_process_action_ignore_internal_state(self): """Test process / reading telegrams from telegram queue. Test if action is executed.""" xknx = XKNX(loop=self.loop) switch = Switch(xknx, "TestOutlet", group_address="5/5/5") binary_sensor = BinarySensor(xknx, "TestInput", group_address_state="1/2/3", ignore_internal_state=True) action_on = Action(xknx, hook="on", target="TestOutlet", method="on") binary_sensor.actions.append(action_on) self.assertEqual(binary_sensor.state, None) self.assertEqual(switch.state, False) telegram_on = Telegram(group_address=GroupAddress("1/2/3"), payload=DPTBinary(1)) with patch("time.time") as mock_time, patch( "asyncio.sleep", new_callable=AsyncMock) as mock_sleep: mock_time.return_value = 1599076123.0 self.loop.run_until_complete(binary_sensor.process(telegram_on)) self.loop.run_until_complete( xknx.devices.process(xknx.telegrams.get_nowait())) self.assertEqual(binary_sensor.state, True) self.assertEqual(switch.state, True) self.loop.run_until_complete(switch.set_off()) self.loop.run_until_complete( xknx.devices.process(xknx.telegrams.get_nowait())) self.assertEqual(switch.state, False) self.assertEqual(binary_sensor.state, True) # add a second to the time to avoid double tap behaviour here mock_time.return_value += BinarySensor.DEFAULT_CONTEXT_TIMEOUT self.loop.run_until_complete(binary_sensor.process(telegram_on)) self.loop.run_until_complete( xknx.devices.process(xknx.telegrams.get_nowait())) self.assertEqual(binary_sensor.state, True) self.assertEqual(switch.state, True)