コード例 #1
0
    def test_outgoing(self, if_mock):
        """Test outgoing telegrams in telegram queue."""
        xknx = XKNX()

        async_if_send_telegram = asyncio.Future()
        async_if_send_telegram.set_result(None)
        if_mock.send_telegram.return_value = async_if_send_telegram

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            direction=TelegramDirection.OUTGOING,
            payload=GroupValueWrite(DPTBinary(1)),
        )

        # log a warning if there is no KNXIP interface instanciated
        with self.assertRaises(CommunicationError):
            self.loop.run_until_complete(
                xknx.telegram_queue.process_telegram_outgoing(telegram))
        if_mock.send_telegram.assert_not_called()

        # if we have an interface send the telegram
        xknx.knxip_interface = if_mock
        self.loop.run_until_complete(
            xknx.telegram_queue.process_telegram_outgoing(telegram))
        if_mock.send_telegram.assert_called_once_with(telegram)
コード例 #2
0
ファイル: telegram_queue_test.py プロジェクト: mielune/xknx
    def test_outgoing(self, logger_warning_mock, if_mock):
        """Test outgoing telegrams in telegram queue."""
        # pylint: disable=no-self-use
        xknx = XKNX()

        async_if_send_telegram = asyncio.Future()
        async_if_send_telegram.set_result(None)
        if_mock.send_telegram.return_value = async_if_send_telegram

        telegram = Telegram(
            direction=TelegramDirection.OUTGOING,
            payload=DPTBinary(1),
            group_address=GroupAddress("1/2/3"),
        )

        # log a warning if there is no KNXIP interface instanciated
        self.loop.run_until_complete(
            xknx.telegram_queue.process_telegram_outgoing(telegram))
        logger_warning_mock.assert_called_once_with(
            "No KNXIP interface defined")
        if_mock.send_telegram.assert_not_called()

        # if we have an interface send the telegram
        xknx.knxip_interface = if_mock
        self.loop.run_until_complete(
            xknx.telegram_queue.process_telegram_outgoing(telegram))
        if_mock.send_telegram.assert_called_once_with(telegram)
コード例 #3
0
    async def test_array_sensor_loop(self, value_type, test_payload, test_value):
        """Test sensor and expose_sensor with different values."""
        xknx = XKNX()
        xknx.knxip_interface = AsyncMock()
        xknx.rate_limit = False
        await xknx.telegram_queue.start()

        expose = ExposeSensor(
            xknx,
            "TestExpose",
            group_address="1/1/1",
            value_type=value_type,
        )
        assert expose.resolve_state() is None
        # set a value from expose - HA sends strings for new values
        stringified_value = str(test_value)
        await expose.set(stringified_value)

        outgoing_telegram = Telegram(
            destination_address=GroupAddress("1/1/1"),
            direction=TelegramDirection.OUTGOING,
            payload=GroupValueWrite(test_payload),
        )
        await xknx.telegrams.join()
        xknx.knxip_interface.send_telegram.assert_called_with(outgoing_telegram)
        assert expose.resolve_state() == test_value

        # init sensor after expose is set - with same group address
        sensor = Sensor(
            xknx,
            "TestSensor",
            group_address_state="1/1/1",
            value_type=value_type,
        )
        assert sensor.resolve_state() is None

        # read sensor state (from expose as it has the same GA)
        # wait_for_result so we don't have to await self.xknx.telegrams.join()
        await sensor.sync(wait_for_result=True)
        read_telegram = Telegram(
            destination_address=GroupAddress("1/1/1"),
            direction=TelegramDirection.OUTGOING,
            payload=GroupValueRead(),
        )
        response_telegram = Telegram(
            destination_address=GroupAddress("1/1/1"),
            direction=TelegramDirection.OUTGOING,
            payload=GroupValueResponse(test_payload),
        )
        xknx.knxip_interface.send_telegram.assert_has_calls(
            [
                call(read_telegram),
                call(response_telegram),
            ]
        )
        # test if Sensor has successfully read from ExposeSensor
        assert sensor.resolve_state() == test_value
        assert expose.resolve_state() == sensor.resolve_state()
        await xknx.telegram_queue.stop()
コード例 #4
0
    async def test_register_with_outgoing_telegrams_does_not_trigger(self):
        """Test telegram_received_callback with outgoing telegrams."""

        xknx = XKNX()
        xknx.knxip_interface = AsyncMock()
        async_telegram_received_cb = AsyncMock()
        xknx.telegram_queue.register_telegram_received_cb(async_telegram_received_cb)

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            direction=TelegramDirection.OUTGOING,
            payload=GroupValueWrite(DPTBinary(1)),
        )

        await xknx.telegram_queue.process_telegram_outgoing(telegram)
        async_telegram_received_cb.assert_not_called()
コード例 #5
0
    async def test_outgoing(self, if_mock):
        """Test outgoing telegrams in telegram queue."""
        xknx = XKNX()

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            direction=TelegramDirection.OUTGOING,
            payload=GroupValueWrite(DPTBinary(1)),
        )

        # log a warning if there is no KNXIP interface instanciated
        with pytest.raises(CommunicationError):
            await xknx.telegram_queue.process_telegram_outgoing(telegram)

        if_mock.send_telegram.assert_not_called()

        # if we have an interface send the telegram
        xknx.knxip_interface = if_mock
        await xknx.telegram_queue.process_telegram_outgoing(telegram)
        if_mock.send_telegram.assert_called_once_with(telegram)
コード例 #6
0
    def test_register_with_outgoing_telegrams_does_not_trigger(self, if_mock):
        """Test telegram_received_callback with outgoing telegrams."""
        # pylint: disable=no-self-use
        xknx = XKNX()
        async_telegram_received_cb = AsyncMock()

        async_if_send_telegram = asyncio.Future()
        async_if_send_telegram.set_result(None)
        if_mock.send_telegram.return_value = async_if_send_telegram

        xknx.telegram_queue.register_telegram_received_cb(
            async_telegram_received_cb)

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            direction=TelegramDirection.OUTGOING,
            payload=GroupValueWrite(DPTBinary(1)),
        )

        xknx.knxip_interface = if_mock
        self.loop.run_until_complete(
            xknx.telegram_queue.process_telegram_outgoing(telegram))
        async_telegram_received_cb.assert_not_called()
コード例 #7
0
    async def test_binary_sensor_loop(self, value_type, test_payload, test_value):
        """Test binary_sensor and expose_sensor with binary values."""
        xknx = XKNX()
        xknx.knxip_interface = AsyncMock()
        xknx.rate_limit = False

        telegram_callback = AsyncMock()
        xknx.telegram_queue.register_telegram_received_cb(
            telegram_callback,
            address_filters=[AddressFilter("i-test")],
            match_for_outgoing=True,
        )
        await xknx.telegram_queue.start()

        expose = ExposeSensor(
            xknx,
            "TestExpose",
            group_address="i-test",
            value_type=value_type,
        )
        assert expose.resolve_state() is None

        await expose.set(test_value)
        await xknx.telegrams.join()
        outgoing_telegram = Telegram(
            destination_address=InternalGroupAddress("i-test"),
            direction=TelegramDirection.OUTGOING,
            payload=GroupValueWrite(test_payload),
        )
        # InternalGroupAddress isn't passed to knxip_interface
        xknx.knxip_interface.send_telegram.assert_not_called()
        telegram_callback.assert_called_with(outgoing_telegram)
        assert expose.resolve_state() == test_value

        bin_sensor = BinarySensor(xknx, "TestSensor", group_address_state="i-test")
        assert bin_sensor.state is None

        # read sensor state (from expose as it has the same GA)
        # wait_for_result so we don't have to await self.xknx.telegrams.join()
        await bin_sensor.sync(wait_for_result=True)
        read_telegram = Telegram(
            destination_address=InternalGroupAddress("i-test"),
            direction=TelegramDirection.OUTGOING,
            payload=GroupValueRead(),
        )
        response_telegram = Telegram(
            destination_address=InternalGroupAddress("i-test"),
            direction=TelegramDirection.OUTGOING,
            payload=GroupValueResponse(test_payload),
        )
        xknx.knxip_interface.send_telegram.assert_not_called()
        telegram_callback.assert_has_calls(
            [
                call(read_telegram),
                call(response_telegram),
            ]
        )
        # test if Sensor has successfully read from ExposeSensor
        assert bin_sensor.state == test_value
        assert expose.resolve_state() == bin_sensor.state
        await xknx.telegram_queue.stop()