def test_config_datetime(self): """Test reading DateTime objects from config file.""" self.assertEqual( TestConfig.xknx.devices['General.Time'], DateTime( TestConfig.xknx, 'General.Time', group_address='2/1/1', broadcast_type=DateTimeBroadcastType.TIME, device_updated_cb=TestConfig.xknx.devices.device_updated)) self.assertEqual( TestConfig.xknx.devices['General.DateTime'], DateTime( TestConfig.xknx, 'General.DateTime', group_address='2/1/2', broadcast_type=DateTimeBroadcastType.DATETIME, device_updated_cb=TestConfig.xknx.devices.device_updated)) self.assertEqual( TestConfig.xknx.devices['General.Date'], DateTime( TestConfig.xknx, 'General.Date', group_address='2/1/3', broadcast_type=DateTimeBroadcastType.DATE, device_updated_cb=TestConfig.xknx.devices.device_updated))
class KNXExposeTime: """Object to Expose Time/Date object to KNX bus.""" def __init__(self, xknx: XKNX, expose_type: str, address: str): """Initialize of Expose class.""" self.xknx = xknx self.expose_type = expose_type self.address = address self.device = None @callback def async_register(self): """Register listener.""" self.device = DateTime( self.xknx, name=self.expose_type.capitalize(), broadcast_type=self.expose_type.upper(), localtime=True, group_address=self.address, ) @callback def shutdown(self): """Prepare for deletion.""" if self.device is not None: self.device.shutdown()
def test_sync_datetime(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX() datetime = DateTime(xknx, "TestDateTime", group_address="1/2/3", broadcast_type="DATETIME") with patch("time.localtime") as mock_time: mock_time.return_value = time.struct_time( [2017, 1, 7, 9, 13, 14, 6, 0, 0]) self.loop.run_until_complete(datetime.sync()) # initial Telegram from broadcasting on init self.assertEqual(xknx.telegrams.qsize(), 2) _throwaway_initial = xknx.telegrams.get_nowait() self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual(telegram.destination_address, GroupAddress("1/2/3")) self.assertEqual(len(telegram.payload.value.value), 8) self.assertEqual( telegram.payload.value.value, (0x75, 0x01, 0x07, 0xE9, 0x0D, 0x0E, 0x20, 0x80), )
def test_config_datetime(self): """Test reading DateTime objects from config file.""" self.assertEqual( TestConfig.xknx.devices["General.Time"], DateTime( TestConfig.xknx, "General.Time", group_address="2/1/1", broadcast_type="TIME", ), ) self.assertEqual( TestConfig.xknx.devices["General.DateTime"], DateTime( TestConfig.xknx, "General.DateTime", group_address="2/1/2", broadcast_type="DATETIME", ), ) self.assertEqual( TestConfig.xknx.devices["General.Date"], DateTime( TestConfig.xknx, "General.Date", group_address="2/1/3", broadcast_type="DATE", ), )
async def test_sync_datetime(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX() self.datetime = DateTime(xknx, "TestDateTime", group_address="1/2/3", broadcast_type="DATETIME") with patch("time.localtime") as mock_time: mock_time.return_value = time.struct_time( [2017, 1, 7, 9, 13, 14, 6, 0, 0]) await self.datetime.sync() assert xknx.telegrams.qsize() == 1 telegram = xknx.telegrams.get_nowait() assert telegram.destination_address == GroupAddress("1/2/3") assert len(telegram.payload.value.value) == 8 assert telegram.payload.value.value == ( 0x75, 0x01, 0x07, 0xE9, 0x0D, 0x0E, 0x20, 0x80, )
def test_process_read(self): """Test test process a read telegram from KNX bus.""" xknx = XKNX(loop=self.loop) datetime = DateTime(xknx, "TestDateTime", group_address="1/2/3", broadcast_type="TIME") telegram_read = Telegram(group_address=GroupAddress("1/2/3"), telegramtype=TelegramType.GROUP_READ) with patch("time.localtime") as mock_time: mock_time.return_value = time.struct_time( [2017, 1, 7, 9, 13, 14, 6, 0, 0]) self.loop.run_until_complete(datetime.process(telegram_read)) # initial Telegram from broadcasting on init self.assertEqual(xknx.telegrams.qsize(), 2) _throwaway_initial = xknx.telegrams.get_nowait() telegram = xknx.telegrams.get_nowait() self.assertEqual( telegram, Telegram( group_address=GroupAddress("1/2/3"), telegramtype=TelegramType.GROUP_RESPONSE, payload=DPTArray((0xE9, 0xD, 0xE)), ), )
def test_has_group_address(self): """Test if has_group_address function works.""" xknx = XKNX() self.datetime = DateTime(xknx, "TestDateTime", group_address="1/2/3", localtime=False) assert self.datetime.has_group_address(GroupAddress("1/2/3")) assert not self.datetime.has_group_address(GroupAddress("1/2/4"))
def test_has_group_address(self): """Test if has_group_address function works.""" xknx = XKNX(loop=self.loop) datetime = DateTime(xknx, "TestDateTime", group_address="1/2/3", localtime=False) self.assertTrue(datetime.has_group_address(GroupAddress("1/2/3"))) self.assertFalse(datetime.has_group_address(GroupAddress("1/2/4")))
def async_register(self): """Register listener.""" self.device = DateTime( self.xknx, name=self.expose_type.capitalize(), broadcast_type=self.expose_type.upper(), localtime=True, group_address=self.address, )
def test_sync_time(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX(loop=self.loop) datetime = DateTime(xknx, "TestDateTime", group_address='1/2/3', broadcast_type="TIME") with patch('time.localtime') as mock_time: mock_time.return_value = time.struct_time([2017, 1, 7, 9, 13, 14, 6, 0, 0]) self.loop.run_until_complete(asyncio.Task(datetime.sync())) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual(telegram.group_address, GroupAddress('1/2/3')) self.assertEqual(telegram.telegramtype, TelegramType.GROUP_WRITE) self.assertEqual(len(telegram.payload.value), 3) self.assertEqual(telegram.payload.value, (0xE9, 0x0D, 0x0E))
def test_sync_time(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX(loop=self.loop) datetime = DateTime(xknx, "TestDateTime", group_address='1/2/3', broadcast_type=DateTimeBroadcastType.TIME) with patch('time.localtime') as mock_time: mock_time.return_value = time.struct_time([2017, 1, 7, 9, 13, 14, 6, 0, 0]) self.loop.run_until_complete(asyncio.Task(datetime.sync(False))) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual(telegram.group_address, GroupAddress('1/2/3')) self.assertEqual(telegram.telegramtype, TelegramType.GROUP_WRITE) self.assertEqual(len(telegram.payload.value), 3) self.assertEqual(telegram.payload.value, (0xE9, 0x0D, 0x0E))
async def main(): """Connect to KNX/IP device and broadcast time.""" xknx = XKNX(daemon_mode=True) DateTime(xknx, "TimeTest", group_address="1/2/3", broadcast_type="time") print("Sending time to KNX bus every hour") await xknx.start() await xknx.stop()
def parse_group_datetime(self, entries): """Parse a datetime section of xknx.yaml.""" for entry in entries: datetime = DateTime.from_config( self.xknx, entry, entries[entry]) self.xknx.devices.add(datetime)
def test_datetime(self): """Test string representation of datetime object.""" xknx = XKNX(loop=self.loop) dateTime = DateTime(xknx, name="Zeit", group_address="1/2/3") self.assertEqual( str(dateTime), '<DateTime name="Zeit" group_address="GroupAddress("1/2/3")/None/None/None" broadcast_type="TIME" />' )
def async_register(self): """Register listener.""" broadcast_type_string = self.type.upper() broadcast_type = broadcast_type_string self.device = DateTime(self.xknx, "Time", broadcast_type=broadcast_type, group_address=self.address)
async def main(): """Connect to KNX/IP device and broadcast time.""" xknx = XKNX() datetime = DateTime(xknx, 'TimeTest', group_address='1/2/3', broadcast_type=DateTimeBroadcastType.TIME) xknx.devices.add(datetime) print("Sending time to KNX bus every hour") await xknx.start(daemon_mode=True, state_updater=True) await xknx.stop()
def test_datetime(self): """Test string representation of datetime object.""" xknx = XKNX() date_time = DateTime(xknx, name="Zeit", group_address="1/2/3", localtime=False) assert ( str(date_time) == '<DateTime name="Zeit" _remote_value=<1/2/3, None, [], None /> broadcast_type="TIME" />' )
def test_process_read(self): """Test test process a read telegram from KNX bus.""" xknx = XKNX(loop=self.loop) datetime = DateTime(xknx, "TestDateTime", group_address='1/2/3', broadcast_type=DateTimeBroadcastType.TIME) telegram_read = Telegram( group_address=GroupAddress('1/2/3'), telegramtype=TelegramType.GROUP_READ) with patch('time.localtime') as mock_time: mock_time.return_value = time.struct_time([2017, 1, 7, 9, 13, 14, 6, 0, 0]) self.loop.run_until_complete(asyncio.Task(datetime.process(telegram_read))) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual( telegram, Telegram( group_address=GroupAddress('1/2/3'), telegramtype=TelegramType.GROUP_RESPONSE, payload=DPTArray((0xe9, 0xd, 0xe))))
def test_process_read(self): """Test test process a read telegram from KNX bus.""" xknx = XKNX(loop=self.loop) datetime = DateTime(xknx, "TestDateTime", group_address='1/2/3', broadcast_type="TIME") telegram_read = Telegram( group_address=GroupAddress('1/2/3'), telegramtype=TelegramType.GROUP_READ) with patch('time.localtime') as mock_time: mock_time.return_value = time.struct_time([2017, 1, 7, 9, 13, 14, 6, 0, 0]) self.loop.run_until_complete(asyncio.Task(datetime.process(telegram_read))) self.assertEqual(xknx.telegrams.qsize(), 1) telegram = xknx.telegrams.get_nowait() self.assertEqual( telegram, Telegram( group_address=GroupAddress('1/2/3'), telegramtype=TelegramType.GROUP_RESPONSE, payload=DPTArray((0xe9, 0xd, 0xe))))
def async_register(self): """Register listener.""" from xknx.devices import DateTime, DateTimeBroadcastType broadcast_type_string = self.type.upper() broadcast_type = DateTimeBroadcastType[broadcast_type_string] self.device = DateTime(self.xknx, 'Time', broadcast_type=broadcast_type, group_address=self.address) self.xknx.devices.add(self.device)
async def test_process_read(self): """Test test process a read telegram from KNX bus.""" xknx = XKNX() self.datetime = DateTime(xknx, "TestDateTime", group_address="1/2/3", broadcast_type="TIME") telegram_read = Telegram(destination_address=GroupAddress("1/2/3"), payload=GroupValueRead()) with patch("time.localtime") as mock_time: mock_time.return_value = time.struct_time( [2017, 1, 7, 9, 13, 14, 6, 0, 0]) await self.datetime.process(telegram_read) telegram = xknx.telegrams.get_nowait() assert telegram == Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueResponse(DPTArray((0xE9, 0xD, 0xE))), )
def test_config_datetime(self): """Test reading DateTime objects from config file.""" xknx = XKNX(config='xknx.yaml', loop=self.loop) self.assertEqual( xknx.devices['General.Time'], DateTime(xknx, 'General.Time', group_address='2/1/1', broadcast_type=DateTimeBroadcastType.TIME, device_updated_cb=xknx.devices.device_updated)) self.assertEqual( xknx.devices['General.DateTime'], DateTime(xknx, 'General.DateTime', group_address='2/1/2', broadcast_type=DateTimeBroadcastType.DATETIME, device_updated_cb=xknx.devices.device_updated)) self.assertEqual( xknx.devices['General.Date'], DateTime(xknx, 'General.Date', group_address='2/1/3', broadcast_type=DateTimeBroadcastType.DATE, device_updated_cb=xknx.devices.device_updated))
def parse_group_datetime(self, entries): """Parse a datetime section of xknx.yaml.""" for entry in entries: datetime = DateTime.from_config(self.xknx, entry, entries[entry]) self.xknx.devices.add(datetime)
def test_has_group_address(self): """Test if has_group_address function works.""" xknx = XKNX(loop=self.loop) datetime = DateTime(xknx, "TestDateTime", group_address='1/2/3') self.assertTrue(datetime.has_group_address(GroupAddress('1/2/3'))) self.assertFalse(datetime.has_group_address(GroupAddress('1/2/4')))
class TestDateTime: """Test class for DateTime object.""" # pylint: disable=attribute-defined-outside-init def teardown_method(self): """Cancel broadcast_task.""" self.datetime.__del__() # # SYNC DateTime # async def test_sync_datetime(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX() self.datetime = DateTime(xknx, "TestDateTime", group_address="1/2/3", broadcast_type="DATETIME") with patch("time.localtime") as mock_time: mock_time.return_value = time.struct_time( [2017, 1, 7, 9, 13, 14, 6, 0, 0]) await self.datetime.sync() assert xknx.telegrams.qsize() == 1 telegram = xknx.telegrams.get_nowait() assert telegram.destination_address == GroupAddress("1/2/3") assert len(telegram.payload.value.value) == 8 assert telegram.payload.value.value == ( 0x75, 0x01, 0x07, 0xE9, 0x0D, 0x0E, 0x20, 0x80, ) # # SYNC Date # async def test_sync_date(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX() self.datetime = DateTime(xknx, "TestDateTime", group_address="1/2/3", broadcast_type="DATE") with patch("time.localtime") as mock_time: mock_time.return_value = time.struct_time( [2017, 1, 7, 9, 13, 14, 6, 0, 0]) await self.datetime.sync() telegram = xknx.telegrams.get_nowait() assert telegram.destination_address == GroupAddress("1/2/3") assert len(telegram.payload.value.value) == 3 assert telegram.payload.value.value == (0x07, 0x01, 0x11) # # SYNC Time # async def test_sync_time(self): """Test sync function / sending group reads to KNX bus.""" xknx = XKNX() self.datetime = DateTime(xknx, "TestDateTime", group_address="1/2/3", broadcast_type="TIME") with patch("time.localtime") as mock_time: mock_time.return_value = time.struct_time( [2017, 1, 7, 9, 13, 14, 6, 0, 0]) await self.datetime.sync() telegram = xknx.telegrams.get_nowait() assert telegram.destination_address == GroupAddress("1/2/3") assert len(telegram.payload.value.value) == 3 assert telegram.payload.value.value == (0xE9, 0x0D, 0x0E) # # PROCESS # # # TEST PROCESS # async def test_process_read(self): """Test test process a read telegram from KNX bus.""" xknx = XKNX() self.datetime = DateTime(xknx, "TestDateTime", group_address="1/2/3", broadcast_type="TIME") telegram_read = Telegram(destination_address=GroupAddress("1/2/3"), payload=GroupValueRead()) with patch("time.localtime") as mock_time: mock_time.return_value = time.struct_time( [2017, 1, 7, 9, 13, 14, 6, 0, 0]) await self.datetime.process(telegram_read) telegram = xknx.telegrams.get_nowait() assert telegram == Telegram( destination_address=GroupAddress("1/2/3"), payload=GroupValueResponse(DPTArray((0xE9, 0xD, 0xE))), ) # # TEST HAS GROUP ADDRESS # def test_has_group_address(self): """Test if has_group_address function works.""" xknx = XKNX() self.datetime = DateTime(xknx, "TestDateTime", group_address="1/2/3", localtime=False) assert self.datetime.has_group_address(GroupAddress("1/2/3")) assert not self.datetime.has_group_address(GroupAddress("1/2/4"))