def test_to_process_error_operation_mode(self):
     """Test process errornous telegram."""
     xknx = XKNX()
     remote_value = RemoteValueClimateMode(
         xknx,
         group_address=GroupAddress("1/2/3"),
         climate_mode_type=RemoteValueClimateMode.ClimateModeType.HVAC_MODE,
     )
     with self.assertRaises(CouldNotParseTelegram):
         telegram = Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(DPTBinary(1)),
         )
         self.loop.run_until_complete(remote_value.process(telegram))
     with self.assertRaises(CouldNotParseTelegram):
         telegram = Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(
                 DPTArray(
                     (
                         0x64,
                         0x65,
                     )
                 )
             ),
         )
         self.loop.run_until_complete(remote_value.process(telegram))
示例#2
0
 def test_set(self):
     """Test setting value."""
     xknx = XKNX()
     remote_value = RemoteValueColorRGBW(
         xknx, group_address=GroupAddress("1/2/3"))
     self.loop.run_until_complete(remote_value.set((100, 101, 102, 103)))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(
                 DPTArray((0x64, 0x65, 0x66, 0x67, 0x00, 0x0F))),
         ),
     )
     self.loop.run_until_complete(remote_value.set((100, 101, 104, 105)))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(
                 DPTArray((0x64, 0x65, 0x68, 0x69, 0x00, 0x0F))),
         ),
     )
示例#3
0
    async def test_to_process_error(self):
        """Test process errornous telegram."""
        xknx = XKNX()
        remote_value = RemoteValueColorRGBW(xknx, group_address=GroupAddress("1/2/3"))

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        assert await remote_value.process(telegram) is False

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTArray((0x64, 0x65, 0x66))),
        )
        assert await remote_value.process(telegram) is False

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(
                DPTArray((0x00, 0x00, 0x0F, 0x64, 0x65, 0x66, 0x67))
            ),
        )
        assert await remote_value.process(telegram) is False

        assert remote_value.value is None
 def test_to_process_error_heat_cool(self):
     """Test process errornous telegram."""
     xknx = XKNX()
     remote_value = RemoteValueBinaryHeatCool(
         xknx,
         group_address=GroupAddress("1/2/3"),
         controller_mode=HVACControllerMode.COOL,
     )
     with self.assertRaises(CouldNotParseTelegram):
         telegram = Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(DPTArray((0x01,))),
         )
         self.loop.run_until_complete(remote_value.process(telegram))
     with self.assertRaises(CouldNotParseTelegram):
         telegram = Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(
                 DPTArray(
                     (
                         0x64,
                         0x65,
                     )
                 )
             ),
         )
         self.loop.run_until_complete(remote_value.process(telegram))
    async def test_to_process_error(self):
        """Test process errornous telegram."""
        xknx = XKNX()
        remote_value = RemoteValueDpt2ByteUnsigned(
            xknx, group_address=GroupAddress("1/2/3"))

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        assert await remote_value.process(telegram) is False

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTArray((0x64, ))),
        )
        assert await remote_value.process(telegram) is False

        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTArray((0x64, 0x53, 0x42))),
        )
        assert await remote_value.process(telegram) is False

        assert remote_value.value is None
示例#6
0
    def test_set(self):
        """Test notificationing off notification."""
        xknx = XKNX()
        notification = Notification(xknx, "Warning", group_address="1/2/3")
        self.loop.run_until_complete(notification.set("Ein Prosit!"))
        self.assertEqual(xknx.telegrams.qsize(), 1)
        telegram = xknx.telegrams.get_nowait()
        self.assertEqual(
            telegram,
            Telegram(
                destination_address=GroupAddress("1/2/3"),
                payload=GroupValueWrite(
                    DPTArray(DPTString().to_knx("Ein Prosit!"))),
            ),
        )
        # test if message longer than 14 chars gets cropped
        self.loop.run_until_complete(notification.set("This is too long."))

        self.assertEqual(xknx.telegrams.qsize(), 1)
        telegram = xknx.telegrams.get_nowait()
        self.assertEqual(
            telegram,
            Telegram(
                destination_address=GroupAddress("1/2/3"),
                payload=GroupValueWrite(
                    DPTArray(DPTString().to_knx("This is too lo"))),
            ),
        )
示例#7
0
    async def test_process_passive(self):
        """Test process / reading telegrams from telegram queue. Test if device was updated."""
        xknx = XKNX()
        callback_mock = AsyncMock()

        switch1 = Switch(
            xknx,
            "TestOutlet",
            group_address=["1/2/3", "4/4/4"],
            group_address_state=["1/2/30", "5/5/5"],
            device_updated_cb=callback_mock,
        )
        assert switch1.state is None
        callback_mock.assert_not_called()

        telegram_on_passive = Telegram(
            destination_address=GroupAddress("4/4/4"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        telegram_off_passive = Telegram(
            destination_address=GroupAddress("5/5/5"),
            payload=GroupValueWrite(DPTBinary(0)),
        )

        await switch1.process(telegram_on_passive)
        assert switch1.state is True
        callback_mock.assert_called_once()
        callback_mock.reset_mock()
        await switch1.process(telegram_off_passive)
        assert switch1.state is False
        callback_mock.assert_called_once()
        callback_mock.reset_mock()
示例#8
0
    def test_process(self):
        """Test process / reading telegrams from telegram queue."""
        xknx = XKNX()
        binaryinput = BinarySensor(xknx, "TestInput", "1/2/3")

        self.assertEqual(binaryinput.state, None)

        telegram_on = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        self.loop.run_until_complete(binaryinput.process(telegram_on))

        self.assertEqual(binaryinput.state, True)

        telegram_off = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(0)),
        )
        self.loop.run_until_complete(binaryinput.process(telegram_off))
        self.assertEqual(binaryinput.state, False)

        binaryinput2 = BinarySensor(xknx, "TestInput", "1/2/4")
        self.assertEqual(binaryinput2.state, None)

        telegram_off2 = Telegram(
            destination_address=GroupAddress("1/2/4"),
            payload=GroupValueWrite(DPTBinary(0)),
        )
        self.loop.run_until_complete(binaryinput2.process(telegram_off2))
        self.assertEqual(binaryinput2.state, False)
 def test_set_operation_mode(self):
     """Test setting value."""
     xknx = XKNX()
     remote_value = RemoteValueOperationMode(
         xknx,
         group_address=GroupAddress("1/2/3"),
         climate_mode_type=RemoteValueOperationMode.ClimateModeType.
         HVAC_MODE,
     )
     self.loop.run_until_complete(remote_value.set(HVACOperationMode.NIGHT))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(DPTArray((0x03, ))),
         ),
     )
     self.loop.run_until_complete(
         remote_value.set(HVACOperationMode.FROST_PROTECTION))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(DPTArray((0x04, ))),
         ),
     )
示例#10
0
    async def test_process_raising(self, process_mock, logging_exception_mock):
        """Test unexpected exception handling in telegram queues."""
        xknx = XKNX()
        telegram_in = Telegram(
            destination_address=GroupAddress("1/2/3"),
            direction=TelegramDirection.INCOMING,
            payload=GroupValueWrite(DPTBinary(1)),
        )
        # InternalGroupAddress to avoid CommunicationError for missing knxip_interface
        telegram_out = Telegram(
            destination_address=InternalGroupAddress("i-outgoing"),
            direction=TelegramDirection.OUTGOING,
            payload=GroupValueWrite(DPTBinary(0)),
        )
        xknx.telegrams.put_nowait(telegram_in)
        xknx.telegrams.put_nowait(telegram_out)

        await xknx.telegram_queue.start()
        await xknx.telegram_queue.stop()

        log_calls = [
            call(
                "Unexpected error while processing incoming telegram %s",
                telegram_in,
            ),
            call(
                "Unexpected error while processing outgoing telegram %s",
                telegram_out,
            ),
        ]
        logging_exception_mock.assert_has_calls(log_calls)
示例#11
0
 def test_set(self):
     """Test setting value."""
     xknx = XKNX()
     remote_value = RemoteValueUpDown(xknx,
                                      group_address=GroupAddress("1/2/3"))
     self.loop.run_until_complete(remote_value.down())
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(DPTBinary(1)),
         ),
     )
     self.loop.run_until_complete(remote_value.up())
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(DPTBinary(0)),
         ),
     )
示例#12
0
    async def test_process_speed(self):
        """Test process / reading telegrams from telegram queue. Test if speed is processed."""
        xknx = XKNX()
        fan = Fan(xknx, name="TestFan", group_address_speed="1/2/3")
        assert fan.is_on is False
        assert fan.current_speed is None

        # 140 is 55% as byte (0...255)
        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTArray(140)),
        )
        await fan.process(telegram)
        # Setting a speed for a fan that has no dedicated switch GA,
        # should turn on the fan.
        assert fan.is_on is True
        assert fan.current_speed == 55

        # Now set a speed of zero which should turn off the fan.
        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTArray(0)),
        )
        await fan.process(telegram)
        assert fan.is_on is False
        assert fan.current_speed == 0
示例#13
0
    def test_process_all_telegrams(self, process_telegram_incoming_mock,
                                   process_telegram_outgoing_mock):
        """Test _process_all_telegrams for clearing the queue."""
        # pylint: disable=no-self-use
        xknx = XKNX()

        async_process_mock = asyncio.Future()
        async_process_mock.set_result(None)
        process_telegram_incoming_mock.return_value = async_process_mock
        process_telegram_outgoing_mock.return_value = async_process_mock

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

        xknx.telegrams.put_nowait(telegram_in)
        xknx.telegrams.put_nowait(telegram_out)
        res = self.loop.run_until_complete(
            xknx.telegram_queue._process_all_telegrams())

        self.assertIsNone(res)
        self.assertEqual(process_telegram_incoming_mock.call_count, 1)
        self.assertEqual(process_telegram_outgoing_mock.call_count, 1)
示例#14
0
    async def test_process(self):
        """Test process / reading telegrams from telegram queue."""
        xknx = XKNX()
        binaryinput = BinarySensor(xknx, "TestInput", "1/2/3")

        assert binaryinput.state is None

        telegram_on = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        await binaryinput.process(telegram_on)
        assert binaryinput.state is True

        telegram_off = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTBinary(0)),
        )
        await binaryinput.process(telegram_off)
        assert binaryinput.state is False

        binaryinput2 = BinarySensor(xknx, "TestInput", "1/2/4")
        assert binaryinput2.state is None

        telegram_off2 = Telegram(
            destination_address=GroupAddress("1/2/4"),
            payload=GroupValueWrite(DPTBinary(0)),
        )
        await binaryinput2.process(telegram_off2)
        assert binaryinput2.last_telegram == telegram_off2
        assert binaryinput2.state is False
 def test_set_controller_mode(self):
     """Test setting value."""
     xknx = XKNX()
     remote_value = RemoteValueControllerMode(
         xknx,
         group_address=GroupAddress("1/2/3"),
     )
     self.loop.run_until_complete(remote_value.set(HVACControllerMode.COOL))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(DPTArray((0x03, ))),
         ),
     )
     self.loop.run_until_complete(
         remote_value.set(HVACControllerMode.NIGHT_PURGE))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(DPTArray((0x04, ))),
         ),
     )
示例#16
0
    def test_set_invert(self):
        """Test switching on/off inverted switch."""
        xknx = XKNX()
        switch = Switch(xknx, "TestOutlet", group_address="1/2/3", invert=True)

        self.loop.run_until_complete(switch.set_on())
        self.assertEqual(xknx.telegrams.qsize(), 1)
        telegram = xknx.telegrams.get_nowait()
        self.assertEqual(
            telegram,
            Telegram(
                destination_address=GroupAddress("1/2/3"),
                payload=GroupValueWrite(DPTBinary(0)),
            ),
        )

        self.loop.run_until_complete(switch.set_off())
        self.assertEqual(xknx.telegrams.qsize(), 1)
        telegram = xknx.telegrams.get_nowait()
        self.assertEqual(
            telegram,
            Telegram(
                destination_address=GroupAddress("1/2/3"),
                payload=GroupValueWrite(DPTBinary(1)),
            ),
        )
 def test_set_binary(self):
     """Test setting value."""
     xknx = XKNX()
     remote_value = RemoteValueBinaryOperationMode(
         xknx,
         group_address=GroupAddress("1/2/3"),
         operation_mode=HVACOperationMode.STANDBY,
     )
     self.loop.run_until_complete(
         remote_value.set(HVACOperationMode.STANDBY))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(DPTBinary(True)),
         ),
     )
     self.loop.run_until_complete(
         remote_value.set(HVACOperationMode.FROST_PROTECTION))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(DPTBinary(False)),
         ),
     )
示例#18
0
    def test_connect_request(self):
        """Test parsing and streaming connection tunneling request KNX/IP packet."""
        raw = bytes.fromhex(
            "06 10 04 20 00 15 04 01 17 00 11 00 BC E0 00 00 48 08 01 00 81")
        knxipframe = KNXIPFrame()
        knxipframe.from_knx(raw)

        assert isinstance(knxipframe.body, TunnellingRequest)
        assert knxipframe.body.communication_channel_id == 1
        assert knxipframe.body.sequence_counter == 23
        assert isinstance(knxipframe.body.cemi, CEMIFrame)

        assert knxipframe.body.cemi.telegram == Telegram(
            destination_address=GroupAddress("9/0/8"),
            payload=GroupValueWrite(DPTBinary(1)),
        )

        cemi = CEMIFrame(code=CEMIMessageCode.L_DATA_REQ)
        cemi.telegram = Telegram(
            destination_address=GroupAddress("9/0/8"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        tunnelling_request = TunnellingRequest(
            communication_channel_id=1,
            sequence_counter=23,
            cemi=cemi,
        )
        knxipframe2 = KNXIPFrame.init_from_body(tunnelling_request)

        assert knxipframe2.to_knx() == raw
示例#19
0
 def test_to_process_error(self):
     """Test process errornous telegram."""
     xknx = XKNX()
     remote_value = RemoteValueDptValue1Ucount(
         xknx, group_address=GroupAddress("1/2/3")
     )
     with self.assertRaises(CouldNotParseTelegram):
         telegram = Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(DPTBinary(1)),
         )
         self.loop.run_until_complete(remote_value.process(telegram))
     with self.assertRaises(CouldNotParseTelegram):
         telegram = Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(
                 DPTArray(
                     (
                         0x64,
                         0x65,
                     )
                 )
             ),
         )
         self.loop.run_until_complete(remote_value.process(telegram))
示例#20
0
 def test_set(self):
     """Test setting value."""
     xknx = XKNX()
     remote_value = RemoteValueDptValue1Ucount(
         xknx, group_address=GroupAddress("1/2/3")
     )
     self.loop.run_until_complete(remote_value.set(10))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(DPTArray((0x0A,))),
         ),
     )
     self.loop.run_until_complete(remote_value.set(11))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(DPTArray((0x0B,))),
         ),
     )
示例#21
0
    async def test_process(self):
        """Test process telegram."""
        xknx = XKNX()
        rv_0 = RemoteValueRaw(xknx, payload_length=0, group_address="1/0/0")
        rv_1 = RemoteValueRaw(xknx, payload_length=1, group_address="1/1/1")
        rv_2 = RemoteValueRaw(xknx, payload_length=2, group_address="1/2/2")

        telegram = Telegram(
            destination_address=GroupAddress("1/0/0"),
            payload=GroupValueWrite(DPTBinary(0)),
        )
        await rv_0.process(telegram)
        assert rv_0.value == 0

        telegram = Telegram(
            destination_address=GroupAddress("1/1/1"),
            payload=GroupValueWrite(DPTArray((0x64, ))),
        )
        await rv_1.process(telegram)
        assert rv_1.value == 100

        telegram = Telegram(
            destination_address=GroupAddress("1/2/2"),
            payload=GroupValueWrite(DPTArray((0x12, 0x34))),
        )
        await rv_2.process(telegram)
        assert rv_2.value == 4660
示例#22
0
 async def test_position_without_binary(self):
     """Test moving cover - with no binary positioning supported."""
     xknx = XKNX()
     cover = Cover(
         xknx,
         "TestCover",
         group_address_position="1/2/3",
     )
     await cover.set_down()
     assert xknx.telegrams.qsize() == 1
     telegram = xknx.telegrams.get_nowait()
     assert telegram == Telegram(
         destination_address=GroupAddress("1/2/3"),
         payload=GroupValueWrite(DPTArray(0xFF)),
     )
     await cover.set_position(50)
     assert xknx.telegrams.qsize() == 1
     telegram = xknx.telegrams.get_nowait()
     assert telegram == Telegram(
         destination_address=GroupAddress("1/2/3"),
         payload=GroupValueWrite(DPTArray(0x80)),
     )
     await cover.set_up()
     assert xknx.telegrams.qsize() == 1
     telegram = xknx.telegrams.get_nowait()
     assert telegram == Telegram(
         destination_address=GroupAddress("1/2/3"),
         payload=GroupValueWrite(DPTArray(0x00)),
     )
示例#23
0
 def test_set(self):
     """Test setting value."""
     xknx = XKNX()
     remote_value = RemoteValueString(xknx, group_address=GroupAddress("1/2/3"))
     self.loop.run_until_complete(remote_value.set("asdf"))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(
                 DPTArray((97, 115, 100, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
             ),
         ),
     )
     self.loop.run_until_complete(remote_value.set("ASDF"))
     self.assertEqual(xknx.telegrams.qsize(), 1)
     telegram = xknx.telegrams.get_nowait()
     self.assertEqual(
         telegram,
         Telegram(
             destination_address=GroupAddress("1/2/3"),
             payload=GroupValueWrite(
                 DPTArray((65, 83, 68, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
             ),
         ),
     )
示例#24
0
    async def test_process_switch(self):
        """Test process / reading telegrams from telegram queue. Test if switch is handled correctly."""
        xknx = XKNX()
        fan = Fan(
            xknx,
            name="TestFan",
            group_address_speed="1/2/3",
            group_address_switch="4/5/6",
        )
        assert fan.is_on is False
        assert fan.current_speed is None

        # 140 is 55% as byte (0...255)
        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTArray(140)),
        )
        await fan.process(telegram)
        # Setting a speed for a fan with dedicated switch GA,
        # should not turn on the fan
        assert fan.is_on is False
        assert fan.current_speed == 55

        # Now turn on the fan via its switch GA
        telegram = Telegram(
            destination_address=GroupAddress("4/5/6"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        await fan.process(telegram)
        assert fan.is_on is True
        assert fan.current_speed == 55

        # Setting a speed of 0 should not turn off the fan
        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTArray(0)),
        )
        await fan.process(telegram)
        assert fan.is_on is True
        assert fan.current_speed == 0

        # Set the speed again so we can verify that switching off the fan does not
        # modify the set speed
        telegram = Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTArray(140)),
        )
        await fan.process(telegram)
        assert fan.is_on is True
        assert fan.current_speed == 55

        # Now turn off the fan via the dedicated switch GA
        telegram = Telegram(
            destination_address=GroupAddress("4/5/6"),
            payload=GroupValueWrite(DPTBinary(0)),
        )
        await fan.process(telegram)
        assert fan.is_on is False
        assert fan.current_speed == 55
示例#25
0
文件: cover_test.py 项目: XKNX/xknx
    async def test_periodic_update(self, time_travel):
        """Test periodic update functionality."""
        xknx = XKNX()
        callback_mock = AsyncMock()
        cover = Cover(
            xknx,
            "TestCoverPeriodicUpdate",
            group_address_long="1/2/1",
            group_address_stop="1/2/2",
            group_address_position="1/2/3",
            group_address_position_state="1/2/4",
            travel_time_down=10,
            travel_time_up=10,
            device_updated_cb=callback_mock,
        )
        with patch("time.time") as mock_time:
            mock_time.return_value = 1517000000.0
            # state telegram updates current position - we are not moving so this is new state - not moving
            telegram = Telegram(GroupAddress("1/2/4"),
                                payload=GroupValueWrite(DPTArray(0)))
            await cover.process(telegram)
            assert (
                callback_mock.call_count == 2
            )  # 1 additional form _stop_position_update because previous state was None
            callback_mock.reset_mock()
            # move to 50%
            telegram = Telegram(GroupAddress("1/2/3"),
                                payload=GroupValueWrite(DPTArray(125)))
            await cover.process(telegram)
            await time_travel(0)
            assert callback_mock.call_count == 1

            mock_time.return_value = 1517000001.0
            await time_travel(1)
            assert callback_mock.call_count == 2

            # state telegram from bus too early
            mock_time.return_value = 1517000001.6
            await time_travel(0.6)
            assert callback_mock.call_count == 2
            telegram = Telegram(GroupAddress("1/2/4"),
                                payload=GroupValueWrite(DPTArray(42)))
            await cover.process(telegram)
            assert callback_mock.call_count == 3
            # next update 1 second after last received state telegram
            mock_time.return_value = 1517000002.0
            await time_travel(0.4)
            assert callback_mock.call_count == 3
            mock_time.return_value = 1517000002.6
            await time_travel(0.6)
            assert callback_mock.call_count == 4
            # last callback - auto updater is removed
            mock_time.return_value = 1517000005.0
            await time_travel(2.4)
            assert callback_mock.call_count == 5
            assert cover.position_reached()
            assert cover._periodic_update_task is None
示例#26
0
    async def test_stop_angle(self):
        """Test stopping cover during angle move / tilting."""
        xknx = XKNX()
        cover_short_stop = Cover(
            xknx,
            "TestCover",
            group_address_long="1/2/1",
            group_address_short="1/2/2",
            group_address_angle="1/2/5",
            group_address_angle_state="1/2/6",
        )
        # Attempt stopping while not actually tilting
        await cover_short_stop.stop()
        assert xknx.telegrams.qsize() == 0

        # Set cover tilt to a dummy start value, since otherwise we cannot
        # determine later on a tilt direction and without it, stopping the
        # til process has no effect.
        await cover_short_stop.angle.process(
            Telegram(
                destination_address=GroupAddress("1/2/5"),
                payload=GroupValueWrite(DPTArray(0xAA)),
            ))

        # Attempt stopping while tilting down
        await cover_short_stop.set_angle(100)
        await cover_short_stop.stop()
        assert xknx.telegrams.qsize() == 2
        telegram = xknx.telegrams.get_nowait()
        print(telegram)
        assert telegram == Telegram(
            destination_address=GroupAddress("1/2/5"),
            payload=GroupValueWrite(DPTArray(0xFF)),
        )
        telegram = xknx.telegrams.get_nowait()
        print(telegram)
        assert telegram == Telegram(
            destination_address=GroupAddress("1/2/2"),
            payload=GroupValueWrite(DPTBinary(1)),
        )

        # Attempt stopping while tilting up
        await cover_short_stop.set_angle(0)
        await cover_short_stop.stop()
        assert xknx.telegrams.qsize() == 2
        telegram = xknx.telegrams.get_nowait()
        print(telegram)
        assert telegram == Telegram(
            destination_address=GroupAddress("1/2/5"),
            payload=GroupValueWrite(DPTArray(0x00)),
        )
        telegram = xknx.telegrams.get_nowait()
        print(telegram)
        assert telegram == Telegram(
            destination_address=GroupAddress("1/2/2"),
            payload=GroupValueWrite(DPTBinary(0)),
        )
示例#27
0
    async def test_set_speed(self):
        """Test setting the speed of a Fan."""
        xknx = XKNX()
        fan = Fan(xknx, name="TestFan", group_address_speed="1/2/3")
        await fan.set_speed(55)
        assert xknx.telegrams.qsize() == 1
        telegram = xknx.telegrams.get_nowait()
        # 140 is 55% as byte (0...255)
        assert telegram == Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTArray(140)),
        )
        await fan.process(telegram)
        assert fan.is_on == True

        # A speed of 0 will turn off the fan implicitly if there is no
        # dedicated switch GA
        await fan.set_speed(0)
        assert xknx.telegrams.qsize() == 1
        telegram = xknx.telegrams.get_nowait()
        # 140 is 55% as byte (0...255)
        assert telegram == Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTArray(0)),
        )
        await fan.process(telegram)
        assert fan.is_on == False

        fan_with_switch = Fan(
            xknx,
            name="TestFan",
            group_address_speed="1/2/3",
            group_address_switch="4/5/6",
        )
        await fan_with_switch.turn_on()
        assert xknx.telegrams.qsize() == 1
        telegram = xknx.telegrams.get_nowait()
        assert telegram == Telegram(
            destination_address=GroupAddress("4/5/6"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        await fan_with_switch.process(telegram)
        assert fan_with_switch.is_on == True

        # A speed of 0 will not turn off the fan implicitly if there is a
        # dedicated switch GA defined. So we only expect a speed change telegram,
        # but no state switch one.
        await fan_with_switch.set_speed(0)
        assert xknx.telegrams.qsize() == 1
        telegram = xknx.telegrams.get_nowait()
        assert telegram == Telegram(
            destination_address=GroupAddress("1/2/3"),
            payload=GroupValueWrite(DPTArray(0)),
        )
        await fan_with_switch.process(telegram)
        assert fan_with_switch.is_on == True
示例#28
0
    async def test_brightness(self):
        """Test resolve state for brightness east, west and south."""
        xknx = XKNX()
        weather: Weather = Weather(
            name="weather",
            xknx=xknx,
            group_address_brightness_east="1/3/5",
            group_address_brightness_south="1/3/6",
            group_address_brightness_west="1/3/7",
            group_address_brightness_north="1/3/8",
            group_address_temperature="1/4/4",
        )

        await weather.process(
            Telegram(
                destination_address=GroupAddress("1/3/5"),
                payload=GroupValueWrite(value=DPTArray((0x7C, 0x5E))),
            )
        )

        await weather.process(
            Telegram(
                destination_address=GroupAddress("1/3/7"),
                payload=GroupValueWrite(value=DPTArray((0x7C, 0x5C))),
            )
        )

        await weather.process(
            Telegram(
                destination_address=GroupAddress("1/3/6"),
                payload=GroupValueWrite(value=DPTArray((0x7C, 0x5A))),
            )
        )

        await weather.process(
            Telegram(
                destination_address=GroupAddress("1/3/8"),
                payload=GroupValueWrite(value=DPTArray((0x7C, 0x5A))),
            )
        )

        assert weather.brightness_east == 366346.24
        assert weather._brightness_east.unit_of_measurement == "lx"
        assert weather._brightness_east.ha_device_class == "illuminance"

        assert weather.brightness_west == 365690.88
        assert weather._brightness_west.unit_of_measurement == "lx"
        assert weather._brightness_west.ha_device_class == "illuminance"

        assert weather.brightness_south == 365035.52
        assert weather._brightness_south.unit_of_measurement == "lx"
        assert weather._brightness_south.ha_device_class == "illuminance"

        assert weather.brightness_north == 365035.52
        assert weather._brightness_north.unit_of_measurement == "lx"
        assert weather._brightness_north.ha_device_class == "illuminance"
示例#29
0
    def test_connect_request(self):
        """Test parsing and streaming connection tunneling request KNX/IP packet."""
        raw = (
            0x06,
            0x10,
            0x04,
            0x20,
            0x00,
            0x15,
            0x04,
            0x01,
            0x17,
            0x00,
            0x11,
            0x00,
            0xBC,
            0xE0,
            0x00,
            0x00,
            0x48,
            0x08,
            0x01,
            0x00,
            0x81,
        )
        xknx = XKNX()
        knxipframe = KNXIPFrame(xknx)
        knxipframe.from_knx(raw)

        self.assertTrue(isinstance(knxipframe.body, TunnellingRequest))
        self.assertEqual(knxipframe.body.communication_channel_id, 1)
        self.assertEqual(knxipframe.body.sequence_counter, 23)
        self.assertTrue(isinstance(knxipframe.body.cemi, CEMIFrame))

        self.assertEqual(
            knxipframe.body.cemi.telegram,
            Telegram(
                destination_address=GroupAddress("9/0/8"),
                payload=GroupValueWrite(DPTBinary(1)),
            ),
        )

        cemi = CEMIFrame(xknx, code=CEMIMessageCode.L_Data_REQ)
        cemi.telegram = Telegram(
            destination_address=GroupAddress("9/0/8"),
            payload=GroupValueWrite(DPTBinary(1)),
        )
        tunnelling_request = TunnellingRequest(xknx,
                                               communication_channel_id=1,
                                               sequence_counter=23,
                                               cemi=cemi)
        knxipframe2 = KNXIPFrame.init_from_body(tunnelling_request)

        self.assertEqual(knxipframe2.to_knx(), list(raw))
示例#30
0
    def test_process_callback(self):
        """Test process / reading telegrams from telegram queue. Test if callback is executed."""
        # pylint: disable=no-self-use
        xknx = XKNX()
        cover = Cover(
            xknx,
            "TestCover",
            group_address_long="1/2/1",
            group_address_short="1/2/2",
            group_address_stop="1/2/3",
            group_address_position="1/2/4",
            group_address_position_state="1/2/5",
            group_address_angle="1/2/6",
            group_address_angle_state="1/2/7",
        )

        after_update_callback = Mock()

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

        cover.register_device_updated_cb(async_after_update_callback)
        for address, payload, feature in [
            ("1/2/1", DPTBinary(1), "long"),
            ("1/2/2", DPTBinary(1), "short"),
            ("1/2/4", DPTArray(42), "position"),
            ("1/2/5", DPTArray(42), "position state"),
            ("1/2/6", DPTArray(42), "angle"),
            ("1/2/7", DPTArray(51), "angle state"),
        ]:
            with self.subTest(address=address, feature=feature):
                telegram = Telegram(
                    destination_address=GroupAddress(address),
                    payload=GroupValueWrite(payload),
                )
                self.loop.run_until_complete(cover.process(telegram))
                after_update_callback.assert_called_with(cover)
                after_update_callback.reset_mock()
        # Stop only when cover is travelling
        telegram = Telegram(
            GroupAddress("1/2/3"), payload=GroupValueWrite(DPTBinary(1))
        )
        self.loop.run_until_complete(cover.process(telegram))
        after_update_callback.assert_not_called()
        self.loop.run_until_complete(cover.set_down())
        self.loop.run_until_complete(cover.process(telegram))
        after_update_callback.assert_called_with(cover)