def test_process_wrong_payload(self): """Test process wrong telegram (wrong payload type).""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, 'Warning', group_address='1/2/3') telegram = Telegram(GroupAddress('1/2/3'), payload=DPTBinary(1)) with self.assertRaises(CouldNotParseTelegram): self.loop.run_until_complete(asyncio.Task(notification.process(telegram)))
def test_process_wrong_payload(self): """Test process wrong telegram (wrong payload type).""" xknx = XKNX() notification = Notification(xknx, "Warning", group_address="1/2/3") telegram = Telegram(GroupAddress("1/2/3"), payload=DPTBinary(1)) with self.assertRaises(CouldNotParseTelegram): self.loop.run_until_complete(notification.process(telegram))
def test_do(self): """Test 'do' functionality.""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, 'Warning', group_address='1/2/3') self.loop.run_until_complete( asyncio.Task(notification.do("message:Ein Prosit!"))) self.assertEqual(notification.message, "Ein Prosit!")
def test_has_group_address(self): """Test has_group_address.""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, 'Warning', group_address='1/2/3', group_address_state='1/2/4') self.assertTrue(notification.has_group_address(GroupAddress('1/2/3'))) self.assertTrue(notification.has_group_address(GroupAddress('1/2/4'))) self.assertFalse(notification.has_group_address(GroupAddress('2/2/2')))
def test_state_addresses(self): """Test expose sensor returns empty list as state addresses.""" xknx = XKNX(loop=self.loop) notification_1 = Notification(xknx, 'Warning', group_address='1/2/3', group_address_state='1/2/4') notification_2 = Notification(xknx, 'Warning', group_address='1/2/5') self.assertEqual(notification_1.state_addresses(), [GroupAddress('1/2/4')]) self.assertEqual(notification_2.state_addresses(), [])
def test_set(self): """Test notificationing off notification.""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, "Warning", group_address="1/2/3") self.loop.run_until_complete(notification.set("Ein Prosit!")) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual( telegram, Telegram( GroupAddress("1/2/3"), payload=DPTArray(DPTString().to_knx("Ein Prosit!")), ), ) # test if message longer than 14 chars gets cropped self.loop.run_until_complete(notification.set("This is too long.")) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual( telegram, Telegram( GroupAddress("1/2/3"), payload=DPTArray(DPTString().to_knx("This is too lo")), ), )
def test_process_payload_invalid_length(self): """Test process wrong telegram (wrong payload length).""" # pylint: disable=invalid-name xknx = XKNX(loop=self.loop) notification = Notification(xknx, 'Warning', group_address='1/2/3') telegram = Telegram(GroupAddress('1/2/3'), payload=DPTArray((23, 24))) with self.assertRaises(CouldNotParseTelegram): self.loop.run_until_complete(asyncio.Task(notification.process(telegram)))
def test_wrong_do(self): """Test wrong do command.""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, 'Warning', group_address='1/2/3') with patch('logging.Logger.warning') as mock_warn: self.loop.run_until_complete(asyncio.Task(notification.do("execute"))) mock_warn.assert_called_with('Could not understand action %s for device %s', 'execute', 'Warning') self.assertEqual(xknx.telegrams.qsize(), 0)
def test_do(self): """Test 'do' functionality.""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, "Warning", group_address="1/2/3") self.loop.run_until_complete(notification.do("message:Ein Prosit!")) self.loop.run_until_complete( xknx.devices.process(xknx.telegrams.get_nowait())) self.assertEqual(notification.message, "Ein Prosit!")
def test_sync_state(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, "Warning", group_address='1/2/3') self.loop.run_until_complete(asyncio.Task(notification.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 test_set(self): """Test notificationing off notification.""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, 'Warning', group_address='1/2/3') self.loop.run_until_complete(asyncio.Task(notification.set("Ein Prosit!"))) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual(telegram, Telegram(GroupAddress('1/2/3'), payload=DPTArray(DPTString().to_knx("Ein Prosit!"))))
def test_wrong_do(self): """Test wrong do command.""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, "Warning", group_address="1/2/3") with patch("logging.Logger.warning") as mock_warn: self.loop.run_until_complete(notification.do("execute")) mock_warn.assert_called_with( "Could not understand action %s for device %s", "execute", "Warning") self.assertEqual(xknx.telegrams.qsize(), 0)
def test_has_group_address(self): """Test has_group_address.""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, "Warning", group_address="1/2/3", group_address_state="1/2/4") self.assertTrue(notification.has_group_address(GroupAddress("1/2/3"))) self.assertTrue(notification.has_group_address(GroupAddress("1/2/4"))) self.assertFalse(notification.has_group_address(GroupAddress("2/2/2")))
def test_has_group_address(self): """Test has_group_address.""" xknx = XKNX() notification = Notification(xknx, "Warning", group_address="1/2/3", group_address_state="1/2/4") assert notification.has_group_address(GroupAddress("1/2/3")) assert notification.has_group_address(GroupAddress("1/2/4")) assert not notification.has_group_address(GroupAddress("2/2/2"))
def test_process_payload_invalid_length(self): """Test process wrong telegram (wrong payload length).""" # pylint: disable=invalid-name xknx = XKNX() notification = Notification(xknx, "Warning", group_address="1/2/3") telegram = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTArray((23, 24))), ) with self.assertRaises(CouldNotParseTelegram): self.loop.run_until_complete(notification.process(telegram))
def test_process(self): """Test process telegram with notification. Test if device was updated.""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, 'Warning', group_address='1/2/3') telegram_set = Telegram(GroupAddress('1/2/3'), payload=DPTArray(DPTString().to_knx("Ein Prosit!"))) self.loop.run_until_complete(asyncio.Task(notification.process(telegram_set))) self.assertEqual(notification.message, "Ein Prosit!") telegram_unset = Telegram(GroupAddress('1/2/3'), payload=DPTArray(DPTString().to_knx(""))) self.loop.run_until_complete(asyncio.Task(notification.process(telegram_unset))) self.assertEqual(notification.message, "")
def test_sync_state(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX(loop=self.loop) notification = Notification( xknx, "Warning", group_address='1/2/3', group_address_state='1/2/4') self.loop.run_until_complete(asyncio.Task(notification.sync(False))) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual(telegram, Telegram(GroupAddress('1/2/4'), TelegramType.GROUP_READ))
def test_notification(self): """Test string representation of notification object.""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, name='Alarm', group_address='1/2/3') self.assertEqual( str(notification), '<Notification name="Alarm" group_address="GroupAddress("1/2/3")" message="" />' ) notification.message = 'Einbrecher im Haus' self.assertEqual( str(notification), '<Notification name="Alarm" group_address="GroupAddress("1/2/3")" message="Einbrecher im Haus" />' )
async def test_process_callback(self): """Test process / reading telegrams from telegram queue. Test if callback was called.""" xknx = XKNX() notification = Notification(xknx, "Warning", group_address="1/2/3") after_update_callback = AsyncMock() notification.register_device_updated_cb(after_update_callback) telegram_set = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTArray( DPTString().to_knx("Ein Prosit!"))), ) await notification.process(telegram_set) after_update_callback.assert_called_with(notification)
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()), )
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) notification = Notification(xknx, 'Warning', group_address='1/2/3') after_update_callback = Mock() async def async_after_update_callback(device): """Async callback.""" after_update_callback(device) notification.register_device_updated_cb(async_after_update_callback) telegram_set = Telegram(GroupAddress('1/2/3'), payload=DPTArray(DPTString().to_knx("Ein Prosit!"))) self.loop.run_until_complete(asyncio.Task(notification.process(telegram_set))) after_update_callback.assert_called_with(notification)
def test_notification(self): """Test string representation of notification object.""" xknx = XKNX(loop=self.loop) notification = Notification( xknx, name='Alarm', group_address='1/2/3', group_address_state='1/2/4') self.assertEqual( str(notification), '<Notification name="Alarm" group_address="GroupAddress("1/2/3")" group_address_state="GroupAddress("1/2/4")" message="" />') notification.message = 'Einbrecher im Haus' self.assertEqual( str(notification), '<Notification name="Alarm" group_address="GroupAddress("1/2/3")" group_address_state="GroupAddress("1/2/4")" ' 'message="Einbrecher im Haus" />')
def parse_group_notification(self, entries): """Parse a sensor section of xknx.yaml.""" for entry in entries: notification = Notification.from_config( self.xknx, entry, entries[entry]) self.xknx.devices.add(notification)
def test_process(self): """Test process telegram with notification. Test if device was updated.""" xknx = XKNX() notification = Notification(xknx, "Warning", group_address="1/2/3") telegram_set = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTArray( DPTString().to_knx("Ein Prosit!"))), ) self.loop.run_until_complete(notification.process(telegram_set)) self.assertEqual(notification.message, "Ein Prosit!") telegram_unset = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTArray(DPTString().to_knx(""))), ) self.loop.run_until_complete(notification.process(telegram_unset)) self.assertEqual(notification.message, "")
def test_config_notification(self): """Test reading DateTime object from config file.""" xknx = XKNX(config='xknx.yaml', loop=self.loop) self.assertEqual( xknx.devices['AlarmWindow'], Notification(xknx, 'AlarmWindow', group_address='2/7/1', device_updated_cb=xknx.devices.device_updated))
async def test_process_wrong_payload(self): """Test process wrong telegram (wrong payload type).""" xknx = XKNX() notification = Notification(xknx, "Warning", group_address="1/2/3") telegram = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(1)), ) with pytest.raises(CouldNotParseTelegram): await notification.process(telegram)
def test_notification(self): """Test string representation of notification object.""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, name='Alarm', group_address='1/2/3', group_address_state='1/2/4') self.assertEqual( str(notification), '<Notification name="Alarm" message="GroupAddress("1/2/3")/GroupAddress("1/2/4")/None/None" />' ) self.loop.run_until_complete( asyncio.Task(notification.set('Einbrecher im Haus'))) self.assertEqual( str(notification), '<Notification name="Alarm" ' 'message="GroupAddress("1/2/3")/' 'GroupAddress("1/2/4")/' '<DPTArray value="[0x45,0x69,0x6e,0x62,0x72,0x65,0x63,0x68,0x65,0x72,0x20,0x69,0x6d,0x20]" />/' 'Einbrecher im " />')
def test_config_notification(self): """Test reading DateTime object from config file.""" self.assertEqual( TestConfig.xknx.devices['AlarmWindow'], Notification( TestConfig.xknx, 'AlarmWindow', group_address='2/7/1', group_address_state='2/7/2', device_updated_cb=TestConfig.xknx.devices.device_updated))
async def test_process_payload_invalid_length(self): """Test process wrong telegram (wrong payload length).""" xknx = XKNX() notification = Notification(xknx, "Warning", group_address="1/2/3") telegram = Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueWrite(DPTArray((23, 24))), ) with pytest.raises(CouldNotParseTelegram): await notification.process(telegram)
def test_config_notification(self): """Test reading DateTime object from config file.""" self.assertEqual( TestConfig.xknx.devices["AlarmWindow"], Notification( TestConfig.xknx, "AlarmWindow", group_address="2/7/1", group_address_state="2/7/2", ), )
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_notification(self): """Test string representation of notification object.""" xknx = XKNX() notification = Notification(xknx, name="Alarm", group_address="1/2/3", group_address_state="1/2/4") self.assertEqual( str(notification), '<Notification name="Alarm" message="GroupAddress("1/2/3")/GroupAddress("1/2/4")/None/None" />', ) self.loop.run_until_complete(notification.set("Einbrecher im Haus")) self.loop.run_until_complete( notification.process(xknx.telegrams.get_nowait())) self.assertEqual( str(notification), '<Notification name="Alarm" ' 'message="GroupAddress("1/2/3")/' 'GroupAddress("1/2/4")/' '<DPTArray value="[0x45,0x69,0x6e,0x62,0x72,0x65,0x63,0x68,0x65,0x72,0x20,0x69,0x6d,0x20]" />/' 'Einbrecher im " />', )
async def test_notification(self): """Test string representation of notification object.""" xknx = XKNX() notification = Notification(xknx, name="Alarm", group_address="1/2/3", group_address_state="1/2/4") assert ( str(notification) == '<Notification name="Alarm" message=<1/2/3, 1/2/4, [], None /> />') await notification.set("Einbrecher im Haus") await notification.process(xknx.telegrams.get_nowait()) assert (str(notification) == '<Notification name="Alarm" ' "message=<1/2/3, 1/2/4, [], 'Einbrecher im ' /> />")
def test_do(self): """Test 'do' functionality.""" xknx = XKNX(loop=self.loop) notification = Notification(xknx, 'Warning', group_address='1/2/3') self.loop.run_until_complete(asyncio.Task(notification.do("message:Ein Prosit!"))) self.assertEqual(notification.message, "Ein Prosit!")