def test_do(self): """Testing empty do.""" xknx = XKNX(loop=self.loop) device = Device(xknx, 'TestDevice') with patch('logging.Logger.info') as mock_info: self.loop.run_until_complete(asyncio.Task(device.do("xx"))) mock_info.assert_called_with("Do not implemented action '%s' for %s", 'xx', 'Device')
def test_do(self): """Testing empty do.""" xknx = XKNX(loop=self.loop) device = Device(xknx, "TestDevice") with patch("logging.Logger.info") as mock_info: self.loop.run_until_complete(device.do("xx")) mock_info.assert_called_with( "'do()' not implemented for action '%s' of %s", "xx", "Device")
async def test_sync(self): """Test sync function.""" xknx = XKNX() Device(xknx, "TestDevice1") Device(xknx, "TestDevice2") with patch("xknx.devices.Device.sync", new_callable=AsyncMock) as mock_sync: await xknx.devices.sync() assert mock_sync.call_count == 2
def test_process_group_response(self): """Test if process_group_read. Testing if mapped to group_write.""" xknx = XKNX(loop=self.loop) device = Device(xknx, 'TestDevice') with patch('xknx.devices.Device.process_group_write') as mock_group_write: fut = asyncio.Future() fut.set_result(None) mock_group_write.return_value = fut self.loop.run_until_complete(asyncio.Task(device.process_group_response(Telegram()))) mock_group_write.assert_called_with(Telegram())
def test_sync(self): """Test sync function.""" xknx = XKNX() Device(xknx, "TestDevice1") Device(xknx, "TestDevice2") with patch("xknx.devices.Device.sync") as mock_sync: fut = asyncio.Future() fut.set_result(None) mock_sync.return_value = fut self.loop.run_until_complete(xknx.devices.sync()) self.assertEqual(mock_sync.call_count, 2)
def test_sync_not_wait_for_response(self): """Testing _sync_impl() method without waiting for response (send_group_read should be called directly).""" # pylint: disable=protected-access xknx = XKNX(loop=self.loop) device = Device(xknx, 'TestDevice') with patch('xknx.devices.Device.state_addresses') as mock_state_addresses: mock_state_addresses.return_value = [GroupAddress('1/2/3'), ] with patch('xknx.core.ValueReader.send_group_read') as mock_value_reader_group_read: fut = asyncio.Future() fut.set_result(None) # Make Value reader return no response mock_value_reader_group_read.return_value = fut self.loop.run_until_complete(asyncio.Task(device._sync_impl(wait_for_result=False))) mock_value_reader_group_read.assert_called_with()
def test_sync(self): """Test sync function.""" xknx = XKNX(loop=self.loop) device1 = Device(xknx, 'TestDevice1') device2 = Device(xknx, 'TestDevice2') xknx.devices.add(device1) xknx.devices.add(device2) with patch('xknx.devices.Device.sync') as mock_sync: fut = asyncio.Future() fut.set_result(None) mock_sync.return_value = fut self.loop.run_until_complete(asyncio.Task(xknx.devices.sync())) self.assertEqual(mock_sync.call_count, 2)
def test_sync_exception(self): """Testing exception handling within sync().""" # pylint: disable=protected-access xknx = XKNX(loop=self.loop) device = Device(xknx, 'TestDevice') with patch('logging.Logger.error') as mock_error: with patch('xknx.devices.Device._sync_impl') as mock_sync_impl: fut = asyncio.Future() fut.set_result(None) mock_sync_impl.return_value = fut mock_sync_impl.side_effect = XKNXException() self.loop.run_until_complete(asyncio.Task(device.sync())) mock_sync_impl.assert_called_with(True) mock_error.assert_called_with('Error while syncing device: %s', XKNXException())
def test_sync_no_response(self): """Testing _sync_impl() method with ValueReader returning no telegram as response.""" # pylint: disable=protected-access xknx = XKNX(loop=self.loop) device = Device(xknx, 'TestDevice') with patch('xknx.devices.Device.state_addresses') as mock_state_addresses: mock_state_addresses.return_value = [GroupAddress('1/2/3'), ] with patch('xknx.core.ValueReader.read') as mock_value_reader_read: fut = asyncio.Future() fut.set_result(None) # Make Value reader return no response mock_value_reader_read.return_value = fut with patch('logging.Logger.warning') as mock_warn: self.loop.run_until_complete(asyncio.Task(device._sync_impl())) mock_warn.assert_called_with("Could not sync group address '%s' from %s", GroupAddress('1/2/3'), device)
async def test_process(self): """Test if telegram is handled by the correct process_* method.""" xknx = XKNX() device = Device(xknx, "TestDevice") with patch("xknx.devices.Device.process_group_read", new_callable=AsyncMock) as mock_group_read: telegram = Telegram(destination_address=GroupAddress("1/2/1"), payload=GroupValueRead()) await device.process(telegram) mock_group_read.assert_called_with(telegram) with patch("xknx.devices.Device.process_group_write", new_callable=AsyncMock) as mock_group_write: telegram = Telegram( destination_address=GroupAddress("1/2/1"), payload=GroupValueWrite(DPTArray((0x01, 0x02))), ) await device.process(telegram) mock_group_write.assert_called_with(telegram) with patch("xknx.devices.Device.process_group_response", new_callable=AsyncMock) as mock_group_response: telegram = Telegram( destination_address=GroupAddress("1/2/1"), payload=GroupValueResponse(DPTArray((0x01, 0x02))), ) await device.process(telegram) mock_group_response.assert_called_with(telegram)
def test_device_updated_cb(self): """Test device updated cb is added to the device.""" xknx = XKNX() after_update_callback = Mock() async def async_after_update_callback(device1): """Async callback""" after_update_callback(device1) device = Device( xknx, "TestDevice", device_updated_cb=async_after_update_callback ) self.loop.run_until_complete(device.after_update()) after_update_callback.assert_called_with(device)
async def test_process_group_response(self): """Test if process_group_read. Testing if mapped to group_write.""" xknx = XKNX() device = Device(xknx, "TestDevice") with patch("xknx.devices.Device.process_group_write", new_callable=AsyncMock) as mock_group_write: await device.process_group_response(Telegram()) mock_group_write.assert_called_with(Telegram())
async def test_device_updated_callback(self): """Test if device updated callback is called correctly if device was updated.""" xknx = XKNX() device1 = Device(xknx, "TestDevice1") device2 = Device(xknx, "TestDevice2") async_after_update_callback1 = AsyncMock() async_after_update_callback2 = AsyncMock() # Registering both callbacks xknx.devices.register_device_updated_cb(async_after_update_callback1) xknx.devices.register_device_updated_cb(async_after_update_callback2) # Triggering first device. Both callbacks to be called await device1.after_update() async_after_update_callback1.assert_called_with(device1) async_after_update_callback2.assert_called_with(device1) async_after_update_callback1.reset_mock() async_after_update_callback2.reset_mock() # Triggering 2nd device. Both callbacks have to be called await device2.after_update() async_after_update_callback1.assert_called_with(device2) async_after_update_callback2.assert_called_with(device2) async_after_update_callback1.reset_mock() async_after_update_callback2.reset_mock() # Unregistering first callback xknx.devices.unregister_device_updated_cb(async_after_update_callback1) # Triggering first device. Only second callback has to be called await device1.after_update() async_after_update_callback1.assert_not_called() async_after_update_callback2.assert_called_with(device1) async_after_update_callback1.reset_mock() async_after_update_callback2.reset_mock() # Unregistering second callback xknx.devices.unregister_device_updated_cb(async_after_update_callback2) # Triggering second device. No callback should be called await device2.after_update() async_after_update_callback1.assert_not_called() async_after_update_callback2.assert_not_called() async_after_update_callback1.reset_mock() async_after_update_callback2.reset_mock()
def test_device_updated_callback(self): """Test if device updated callback is called correctly if device was updated.""" xknx = XKNX(loop=self.loop) device1 = Device(xknx, 'TestDevice1') device2 = Device(xknx, 'TestDevice2') xknx.devices.add(device1) xknx.devices.add(device2) after_update_callback1 = Mock() after_update_callback2 = Mock() async def async_after_update_callback1(device): """Async callback No. 1.""" after_update_callback1(device) async def async_after_update_callback2(device): """Async callback No. 2.""" after_update_callback2(device) # Registering both callbacks xknx.devices.register_device_updated_cb(async_after_update_callback1) xknx.devices.register_device_updated_cb(async_after_update_callback2) # Triggering first device. Both callbacks to be called self.loop.run_until_complete(asyncio.Task(device1.after_update())) after_update_callback1.assert_called_with(device1) after_update_callback2.assert_called_with(device1) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Triggering 2nd device. Both callbacks have to be called self.loop.run_until_complete(asyncio.Task(device2.after_update())) after_update_callback1.assert_called_with(device2) after_update_callback2.assert_called_with(device2) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering first callback xknx.devices.unregister_device_updated_cb(async_after_update_callback1) # Triggering first device. Only second callback has to be called self.loop.run_until_complete(asyncio.Task(device1.after_update())) after_update_callback1.assert_not_called() after_update_callback2.assert_called_with(device1) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering second callback xknx.devices.unregister_device_updated_cb(async_after_update_callback2) # Triggering second device. No callback should be called self.loop.run_until_complete(asyncio.Task(device2.after_update())) after_update_callback1.assert_not_called() after_update_callback2.assert_not_called() after_update_callback1.reset_mock() after_update_callback2.reset_mock()
def test_sync_valid_response(self): """Testing _sync_imp() method with ValueReader.read returning a Telegram - which should be processed.""" # pylint: disable=protected-access xknx = XKNX(loop=self.loop) device = Device(xknx, 'TestDevice') with patch('xknx.devices.Device.state_addresses') as mock_state_addresses: mock_state_addresses.return_value = [GroupAddress('1/2/3'), ] with patch('xknx.core.ValueReader.read') as mock_value_reader_read: fut = asyncio.Future() telegram = Telegram(GroupAddress("1/2/3")) fut.set_result(telegram) mock_value_reader_read.return_value = fut with patch('xknx.devices.Device.process') as mock_device_process: fut2 = asyncio.Future() fut2.set_result(None) mock_device_process.return_value = fut2 self.loop.run_until_complete(asyncio.Task(device._sync_impl())) mock_device_process.assert_called_with(telegram)
def test_add_remove(self): """Tesst add and remove functions.""" xknx = XKNX() device1 = Device(xknx, "TestDevice1") device2 = Device(xknx, "TestDevice2") self.assertEqual(len(xknx.devices), 2) device1.shutdown() self.assertEqual(len(xknx.devices), 1) self.assertFalse("TestDevice1" in xknx.devices) device2.shutdown() self.assertEqual(len(xknx.devices), 0)
def test_add_remove(self): """Tesst add and remove functions.""" xknx = XKNX() device1 = Device(xknx, "TestDevice1") device2 = Device(xknx, "TestDevice2") assert len(xknx.devices) == 2 device1.shutdown() assert len(xknx.devices) == 1 assert "TestDevice1" not in xknx.devices device2.shutdown() assert len(xknx.devices) == 0
async def test_device_updated_cb(self): """Test device updated cb is added to the device.""" xknx = XKNX() after_update_callback = AsyncMock() device = Device(xknx, "TestDevice", device_updated_cb=after_update_callback) await device.after_update() after_update_callback.assert_called_with(device)
def test_process(self): """Test if telegram is handled by the correct process_* method.""" xknx = XKNX(loop=self.loop) device = Device(xknx, "TestDevice") with patch( "xknx.devices.Device.process_group_read") as mock_group_read: fut = asyncio.Future() fut.set_result(None) mock_group_read.return_value = fut telegram = Telegram( GroupAddress("1/2/1"), payload=DPTArray((0x01, 0x02)), telegramtype=TelegramType.GROUP_READ, ) self.loop.run_until_complete(device.process(telegram)) mock_group_read.assert_called_with(telegram) with patch( "xknx.devices.Device.process_group_write") as mock_group_write: fut = asyncio.Future() fut.set_result(None) mock_group_write.return_value = fut telegram = Telegram( GroupAddress("1/2/1"), payload=DPTArray((0x01, 0x02)), telegramtype=TelegramType.GROUP_WRITE, ) self.loop.run_until_complete(device.process(telegram)) mock_group_write.assert_called_with(telegram) with patch("xknx.devices.Device.process_group_response" ) as mock_group_response: fut = asyncio.Future() fut.set_result(None) mock_group_response.return_value = fut telegram = Telegram( GroupAddress("1/2/1"), payload=DPTArray((0x01, 0x02)), telegramtype=TelegramType.GROUP_RESPONSE, ) self.loop.run_until_complete(device.process(telegram)) mock_group_response.assert_called_with(telegram)
def test_process(self): """Test if telegram is handled by the correct process_* method.""" xknx = XKNX(loop=self.loop) device = Device(xknx, 'TestDevice') with patch('xknx.devices.Device.process_group_read') as mock_group_read: fut = asyncio.Future() fut.set_result(None) mock_group_read.return_value = fut telegram = Telegram( GroupAddress('1/2/1'), payload=DPTArray((0x01, 0x02)), telegramtype=TelegramType.GROUP_READ) self.loop.run_until_complete(asyncio.Task(device.process(telegram))) mock_group_read.assert_called_with(telegram) with patch('xknx.devices.Device.process_group_write') as mock_group_write: fut = asyncio.Future() fut.set_result(None) mock_group_write.return_value = fut telegram = Telegram( GroupAddress('1/2/1'), payload=DPTArray((0x01, 0x02)), telegramtype=TelegramType.GROUP_WRITE) self.loop.run_until_complete(asyncio.Task(device.process(telegram))) mock_group_write.assert_called_with(telegram) with patch('xknx.devices.Device.process_group_response') as mock_group_response: fut = asyncio.Future() fut.set_result(None) mock_group_response.return_value = fut telegram = Telegram( GroupAddress('1/2/1'), payload=DPTArray((0x01, 0x02)), telegramtype=TelegramType.GROUP_RESPONSE) self.loop.run_until_complete(asyncio.Task(device.process(telegram))) mock_group_response.assert_called_with(telegram)
def test_process(self): """Test if telegram is handled by the correct process_* method.""" xknx = XKNX() device = Device(xknx, "TestDevice") with patch("xknx.devices.Device.process_group_read") as mock_group_read: fut = asyncio.Future() fut.set_result(None) mock_group_read.return_value = fut telegram = Telegram( destination_address=GroupAddress("1/2/1"), payload=GroupValueRead() ) self.loop.run_until_complete(device.process(telegram)) mock_group_read.assert_called_with(telegram) with patch("xknx.devices.Device.process_group_write") as mock_group_write: fut = asyncio.Future() fut.set_result(None) mock_group_write.return_value = fut telegram = Telegram( destination_address=GroupAddress("1/2/1"), payload=GroupValueWrite(DPTArray((0x01, 0x02))), ) self.loop.run_until_complete(device.process(telegram)) mock_group_write.assert_called_with(telegram) with patch("xknx.devices.Device.process_group_response") as mock_group_response: fut = asyncio.Future() fut.set_result(None) mock_group_response.return_value = fut telegram = Telegram( destination_address=GroupAddress("1/2/1"), payload=GroupValueResponse(DPTArray((0x01, 0x02))), ) self.loop.run_until_complete(device.process(telegram)) mock_group_response.assert_called_with(telegram)
async def test_bad_callback(self, logging_exception_mock): """Test handling callback raising an exception""" xknx = XKNX() device = Device(xknx, "TestDevice") good_callback_1 = AsyncMock() bad_callback = AsyncMock(side_effect=Exception("Boom")) good_callback_2 = AsyncMock() device.register_device_updated_cb(good_callback_1) device.register_device_updated_cb(bad_callback) device.register_device_updated_cb(good_callback_2) await device.after_update() good_callback_1.assert_called_with(device) bad_callback.assert_called_with(device) good_callback_2.assert_called_with(device) logging_exception_mock.assert_called_once_with( "Unexpected error while processing device_updated_cb for %s", device, )
async def test_process_callback(self): """Test process / reading telegrams from telegram queue. Test if callback was called.""" xknx = XKNX() device = Device(xknx, "TestDevice") after_update_callback1 = AsyncMock() after_update_callback2 = AsyncMock() device.register_device_updated_cb(after_update_callback1) device.register_device_updated_cb(after_update_callback2) # Triggering first time. Both have to be called await device.after_update() after_update_callback1.assert_called_with(device) after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Triggering 2nd time. Both have to be called await device.after_update() after_update_callback1.assert_called_with(device) after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering first callback device.unregister_device_updated_cb(after_update_callback1) await device.after_update() after_update_callback1.assert_not_called() after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering second callback device.unregister_device_updated_cb(after_update_callback2) await device.after_update() after_update_callback1.assert_not_called() after_update_callback2.assert_not_called()
def test_unique_id(self): """Test unique id functionality.""" xknx = XKNX() device = Device(xknx, "Test") assert device.unique_id is None
def test_process_group_read(self): """Test if process_group_read. Nothing really to test here.""" xknx = XKNX(loop=self.loop) device = Device(xknx, 'TestDevice') self.loop.run_until_complete( asyncio.Task(device.process_group_read(Telegram())))
async def test_process_group_read(self): """Test if process_group_read. Nothing really to test here.""" xknx = XKNX() device = Device(xknx, "TestDevice") await device.process_group_read(Telegram())
def test_process_group_read(self): """Test if process_group_read. Nothing really to test here.""" xknx = XKNX(loop=self.loop) device = Device(xknx, 'TestDevice') self.loop.run_until_complete(asyncio.Task(device.process_group_read(Telegram())))
def test_state_addresses(self): """Test state_addresses() function.""" xknx = XKNX(loop=self.loop) device = Device(xknx, 'TestDevice') self.assertEqual(device.state_addresses(), [])
def test_process_callback(self): """Test process / reading telegrams from telegram queue. Test if callback was called.""" xknx = XKNX(loop=self.loop) device = Device(xknx, 'TestDevice') after_update_callback1 = Mock() after_update_callback2 = Mock() async def async_after_update_callback1(device): """Async callback No. 1.""" after_update_callback1(device) async def async_after_update_callback2(device): """Async callback No. 2.""" after_update_callback2(device) device.register_device_updated_cb(async_after_update_callback1) device.register_device_updated_cb(async_after_update_callback2) # Triggering first time. Both have to be called self.loop.run_until_complete(asyncio.Task(device.after_update())) after_update_callback1.assert_called_with(device) after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Triggering 2nd time. Both have to be called self.loop.run_until_complete(asyncio.Task(device.after_update())) after_update_callback1.assert_called_with(device) after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering first callback device.unregister_device_updated_cb(async_after_update_callback1) self.loop.run_until_complete(asyncio.Task(device.after_update())) after_update_callback1.assert_not_called() after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering second callback device.unregister_device_updated_cb(async_after_update_callback2) self.loop.run_until_complete(asyncio.Task(device.after_update())) after_update_callback1.assert_not_called() after_update_callback2.assert_not_called()
def test_iter_remote_value_raises_exception(self): """Test _iter_remote_value raises NotImplementedError.""" xknx = XKNX(loop=self.loop) device = Device(xknx, "TestDevice") self.assertRaises(NotImplementedError, device._iter_remote_values)
def test_process_callback(self): """Test process / reading telegrams from telegram queue. Test if callback was called.""" xknx = XKNX() device = Device(xknx, "TestDevice") after_update_callback1 = Mock() after_update_callback2 = Mock() async def async_after_update_callback1(device): """Async callback No. 1.""" after_update_callback1(device) async def async_after_update_callback2(device): """Async callback No. 2.""" after_update_callback2(device) device.register_device_updated_cb(async_after_update_callback1) device.register_device_updated_cb(async_after_update_callback2) # Triggering first time. Both have to be called self.loop.run_until_complete(device.after_update()) after_update_callback1.assert_called_with(device) after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Triggering 2nd time. Both have to be called self.loop.run_until_complete(device.after_update()) after_update_callback1.assert_called_with(device) after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering first callback device.unregister_device_updated_cb(async_after_update_callback1) self.loop.run_until_complete(device.after_update()) after_update_callback1.assert_not_called() after_update_callback2.assert_called_with(device) after_update_callback1.reset_mock() after_update_callback2.reset_mock() # Unregistering second callback device.unregister_device_updated_cb(async_after_update_callback2) self.loop.run_until_complete(device.after_update()) after_update_callback1.assert_not_called() after_update_callback2.assert_not_called()
def test_process_group_read(self): """Test if process_group_read. Nothing really to test here.""" xknx = XKNX() device = Device(xknx, "TestDevice") self.loop.run_until_complete(device.process_group_read(Telegram()))