Пример #1
0
    async def test_process_callback_ignore_internal_state_no_counter(self):
        """Test after_update_callback after state of switch was changed."""
        xknx = XKNX()
        switch = BinarySensor(
            xknx,
            "TestInput",
            group_address_state="1/2/3",
            ignore_internal_state=True,
            context_timeout=0,
        )
        async_after_update_callback = AsyncMock()

        switch.register_device_updated_cb(async_after_update_callback)

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        await switch.process(telegram)
        # no _context_task started because context_timeout is False
        assert switch._context_task is None
        async_after_update_callback.assert_called_once_with(switch)

        async_after_update_callback.reset_mock()
        # send same telegram again
        await switch.process(telegram)
        async_after_update_callback.assert_called_once_with(switch)
Пример #2
0
    async def test_process_group_value_response(self):
        """Test precess of GroupValueResponse telegrams."""
        xknx = XKNX()
        switch = BinarySensor(
            xknx,
            "TestInput",
            group_address_state="1/2/3",
            ignore_internal_state=True,
        )
        async_after_update_callback = AsyncMock()

        switch.register_device_updated_cb(async_after_update_callback)

        write_telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        response_telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueResponse(DPTBinary(1), ),
        )
        assert switch.state is None
        # initial GroupValueResponse changes state and runs callback
        await switch.process(response_telegram)
        assert switch.state
        async_after_update_callback.assert_called_once_with(switch)
        # GroupValueWrite with same payload runs callback because of `ignore_internal_state`
        async_after_update_callback.reset_mock()
        await switch.process(write_telegram)
        assert switch.state
        async_after_update_callback.assert_called_once_with(switch)
        # GroupValueResponse should not run callback when state has not changed
        async_after_update_callback.reset_mock()
        await switch.process(response_telegram)
        async_after_update_callback.assert_not_called()
Пример #3
0
    def test_process_callback(self):
        """Test after_update_callback after state of switch was changed."""
        # pylint: disable=protected-access
        xknx = XKNX()
        switch = BinarySensor(xknx,
                              "TestInput",
                              group_address_state="1/2/3",
                              ignore_internal_state=False)
        async_after_update_callback = AsyncMock()

        switch.register_device_updated_cb(async_after_update_callback)

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        self.loop.run_until_complete(switch.process(telegram))
        # no _context_task started because ignore_internal_state is False
        self.assertIsNone(switch._context_task)
        async_after_update_callback.assert_called_once_with(switch)

        async_after_update_callback.reset_mock()
        # send same telegram again
        self.loop.run_until_complete(switch.process(telegram))
        async_after_update_callback.assert_not_called()
Пример #4
0
    def test_process_callback_ignore_internal_state_no_counter(self):
        """Test after_update_callback after state of switch was changed."""
        # pylint: disable=protected-access
        xknx = XKNX()
        switch = BinarySensor(
            xknx,
            "TestInput",
            group_address_state="1/2/3",
            ignore_internal_state=True,
            context_timeout=0,
        )

        after_update_callback = Mock()

        async def async_after_update_callback(device):
            """Async callback."""
            after_update_callback(device)

        switch.register_device_updated_cb(async_after_update_callback)

        telegram = Telegram(group_address=GroupAddress("1/2/3"),
                            payload=DPTBinary(1))
        self.loop.run_until_complete(switch.process(telegram))
        # no _context_task started because context_timeout is False
        self.assertIsNone(switch._context_task)
        after_update_callback.assert_called_once_with(switch)

        after_update_callback.reset_mock()
        # send same telegram again
        self.loop.run_until_complete(switch.process(telegram))
        after_update_callback.assert_called_once_with(switch)
Пример #5
0
    def test_process_group_value_response(self):
        """Test precess of GroupValueResponse telegrams."""
        # pylint: disable=protected-access
        xknx = XKNX()
        switch = BinarySensor(
            xknx,
            "TestInput",
            group_address_state="1/2/3",
            ignore_internal_state=True,
        )
        async_after_update_callback = AsyncMock()

        switch.register_device_updated_cb(async_after_update_callback)

        write_telegram = Telegram(group_address=GroupAddress("1/2/3"),
                                  payload=DPTBinary(1))
        response_telegram = Telegram(
            group_address=GroupAddress("1/2/3"),
            payload=DPTBinary(1),
            telegramtype=TelegramType.GROUP_RESPONSE,
        )
        self.assertIsNone(switch.state)
        # initial GroupValueResponse changes state and runs callback
        self.loop.run_until_complete(switch.process(response_telegram))
        self.assertTrue(switch.state)
        async_after_update_callback.assert_called_once_with(switch)
        # GroupValueWrite with same payload runs callback because of `ignore_internal_state`
        async_after_update_callback.reset_mock()
        self.loop.run_until_complete(switch.process(write_telegram))
        self.assertTrue(switch.state)
        async_after_update_callback.assert_called_once_with(switch)
        # GroupValueResponse should not run callback when state has not changed
        async_after_update_callback.reset_mock()
        self.loop.run_until_complete(switch.process(response_telegram))
        async_after_update_callback.assert_not_called()
Пример #6
0
    def test_process_callback(self):
        """Test after_update_callback after state of switch was changed."""
        # pylint: disable=no-self-use
        xknx = XKNX(loop=self.loop)
        switch = BinarySensor(xknx, 'TestInput', group_address_state='1/2/3')

        after_update_callback = Mock()

        async def async_after_update_callback(device):
            """Async callback."""
            after_update_callback(device)
        switch.register_device_updated_cb(async_after_update_callback)

        telegram = Telegram()
        telegram.payload = DPTBinary(1)
        self.loop.run_until_complete(asyncio.Task(switch.process(telegram)))
        after_update_callback.assert_called_with(switch)
Пример #7
0
    def test_process_callback(self):
        """Test after_update_callback after state of switch was changed."""
        # pylint: disable=no-self-use
        xknx = XKNX(loop=self.loop)
        switch = BinarySensor(xknx, 'TestInput', group_address_state='1/2/3')

        after_update_callback = Mock()

        async def async_after_update_callback(device):
            """Async callback."""
            after_update_callback(device)
        switch.register_device_updated_cb(async_after_update_callback)

        telegram = Telegram()
        telegram.payload = DPTBinary(1)
        self.loop.run_until_complete(asyncio.Task(switch.process(telegram)))
        after_update_callback.assert_called_with(switch)
Пример #8
0
    def test_process_callback_ignore_internal_state(self):
        """Test after_update_callback after state of switch was changed."""
        # pylint: disable=protected-access
        xknx = XKNX()
        switch = BinarySensor(
            xknx,
            "TestInput",
            group_address_state="1/2/3",
            ignore_internal_state=True,
            context_timeout=0.001,
        )

        after_update_callback = Mock()

        async def async_after_update_callback(device):
            """Async callback."""
            after_update_callback(device)

        switch.register_device_updated_cb(async_after_update_callback)

        telegram = Telegram(group_address=GroupAddress("1/2/3"),
                            payload=DPTBinary(1))
        self.assertEqual(switch.counter, 0)

        self.loop.run_until_complete(switch.process(telegram))
        after_update_callback.assert_not_called()
        self.assertEqual(switch.counter, 1)
        self.loop.run_until_complete(switch._context_task)
        after_update_callback.assert_called_with(switch)
        # once with counter 1 and once with counter 0
        self.assertEqual(after_update_callback.call_count, 2)

        after_update_callback.reset_mock()
        # send same telegram again
        self.loop.run_until_complete(switch.process(telegram))
        self.assertEqual(switch.counter, 1)
        self.loop.run_until_complete(switch.process(telegram))
        self.assertEqual(switch.counter, 2)
        after_update_callback.assert_not_called()

        self.loop.run_until_complete(switch._context_task)
        after_update_callback.assert_called_with(switch)
        # once with counter 2 and once with counter 0
        self.assertEqual(after_update_callback.call_count, 2)
        self.assertEqual(switch.counter, 0)
Пример #9
0
    async def test_process_callback_ignore_internal_state(self):
        """Test after_update_callback after state of switch was changed."""
        xknx = XKNX()
        switch = BinarySensor(
            xknx,
            "TestInput",
            group_address_state="1/2/3",
            ignore_internal_state=True,
            context_timeout=0.001,
        )
        async_after_update_callback = AsyncMock()

        switch.register_device_updated_cb(async_after_update_callback)

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        assert switch.counter == 0

        await switch.process(telegram)
        async_after_update_callback.assert_not_called()
        assert switch.counter == 1
        await switch._context_task
        async_after_update_callback.assert_called_with(switch)
        # once with counter 1 and once with counter 0
        assert async_after_update_callback.call_count == 2

        async_after_update_callback.reset_mock()
        # send same telegram again
        await switch.process(telegram)
        assert switch.counter == 1
        await switch.process(telegram)
        assert switch.counter == 2
        async_after_update_callback.assert_not_called()

        await switch._context_task
        async_after_update_callback.assert_called_with(switch)
        # once with counter 2 and once with counter 0
        assert async_after_update_callback.call_count == 2
        assert switch.counter == 0