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_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_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 self.loop.run_until_complete(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 self.loop.run_until_complete(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 self.loop.run_until_complete(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 self.loop.run_until_complete(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_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_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)