Exemple #1
0
    def test_process_callback_updated_via_telegram(self):
        """Test if after_update_callback is called after update of Climate object."""
        # pylint: disable=no-self-use
        xknx = XKNX(loop=self.loop)
        climate = Climate(xknx,
                          'TestClimate',
                          group_address_temperature='1/2/1',
                          group_address_target_temperature='1/2/2',
                          group_address_setpoint_shift='1/2/3')

        after_update_callback = Mock()

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

        climate.register_device_updated_cb(async_after_update_callback)

        telegram = Telegram(GroupAddress('1/2/1'),
                            payload=DPTArray(DPTTemperature.to_knx(23)))
        self.loop.run_until_complete(asyncio.Task(climate.process(telegram)))
        after_update_callback.assert_called_with(climate)
        after_update_callback.reset_mock()

        telegram = Telegram(GroupAddress('1/2/2'),
                            payload=DPTArray(DPTTemperature.to_knx(23)))
        self.loop.run_until_complete(asyncio.Task(climate.process(telegram)))
        after_update_callback.assert_called_with(climate)
        after_update_callback.reset_mock()

        telegram = Telegram(GroupAddress('1/2/3'),
                            payload=DPTArray(DPTValue1Count.to_knx(-4)))
        self.loop.run_until_complete(asyncio.Task(climate.process(telegram)))
        after_update_callback.assert_called_with(climate)
        after_update_callback.reset_mock()
    def test_payload_valid_preassigned_mode(self):
        """Test if setpoint_shift_mode is assigned properly by payload length."""
        xknx = XKNX()
        remote_value_6 = RemoteValueSetpointShift(
            xknx=xknx, setpoint_shift_mode=SetpointShiftMode.DPT6010
        )
        remote_value_9 = RemoteValueSetpointShift(
            xknx=xknx, setpoint_shift_mode=SetpointShiftMode.DPT9002
        )
        dpt_6_payload = DPTArray(DPTValue1Count.to_knx(1))
        dpt_9_payload = DPTArray(DPTTemperature.to_knx(1))

        assert remote_value_6.dpt_class == DPTValue1Count
        assert remote_value_6.payload_valid(None) is None
        assert remote_value_6.payload_valid(dpt_9_payload) is None
        assert remote_value_6.payload_valid(DPTArray((1, 2, 3, 4))) is None
        assert remote_value_6.payload_valid(DPTBinary(1)) is None
        assert remote_value_6.payload_valid(dpt_6_payload) == dpt_6_payload

        assert remote_value_9.dpt_class == DPTTemperature
        assert remote_value_9.payload_valid(None) is None
        assert remote_value_9.payload_valid(dpt_6_payload) is None
        assert remote_value_9.payload_valid(DPTArray((1, 2, 3))) is None
        assert remote_value_9.payload_valid(DPTBinary(1)) is None
        assert remote_value_9.payload_valid(dpt_9_payload) == dpt_9_payload
    def test_EndTOEnd_group_write_2bytes(self):
        """Test parsing and streaming CEMIFrame KNX/IP packet, setting value of thermostat."""
        # Incoming Temperature from thermostat
        raw = bytes.fromhex("0610053000132900BCD01402080103008007C1")
        xknx = XKNX()
        knxipframe = KNXIPFrame(xknx)
        knxipframe.from_knx(raw)
        telegram = knxipframe.body.cemi.telegram
        self.assertEqual(
            telegram,
            Telegram(
                destination_address=GroupAddress("2049"),
                payload=GroupValueWrite(
                    DPTArray(DPTTemperature().to_knx(19.85))),
                source_address=IndividualAddress("1.4.2"),
            ),
        )

        cemi = CEMIFrame(xknx, src_addr=IndividualAddress("1.4.2"))
        cemi.telegram = telegram
        cemi.set_hops(5)
        routing_indication = RoutingIndication(xknx, cemi=cemi)
        knxipframe2 = KNXIPFrame.init_from_body(routing_indication)

        self.assertEqual(knxipframe2.header.to_knx(), list(raw[0:6]))
        self.assertEqual(knxipframe2.body.to_knx(), list(raw[6:]))
        self.assertEqual(knxipframe2.to_knx(), list(raw))
Exemple #4
0
    def test_payload_valid_mode_assignment(self):
        """Test if setpoint_shift_mode is assigned properly by payload length."""
        xknx = XKNX()
        remote_value = RemoteValueSetpointShift(xknx=xknx)
        dpt_6_payload = DPTArray(DPTValue1Count.to_knx(1))
        dpt_9_payload = DPTArray(DPTTemperature.to_knx(1))

        with pytest.raises(CouldNotParseTelegram):
            remote_value.payload_valid(DPTBinary(0))
        with pytest.raises(CouldNotParseTelegram):
            remote_value.payload_valid(DPTArray((0, 1, 2)))

        # DPT 6 - payload_length 1
        assert remote_value.dpt_class is None
        assert remote_value.payload_valid(dpt_6_payload) == dpt_6_payload
        assert remote_value.dpt_class == SetpointShiftMode.DPT6010.value
        with pytest.raises(CouldNotParseTelegram):
            # DPT 9 is invalid now
            remote_value.payload_valid(dpt_9_payload)

        remote_value.dpt_class = None
        # DPT 9 - payload_length 2
        assert remote_value.payload_valid(dpt_9_payload) == dpt_9_payload
        assert remote_value.dpt_class == SetpointShiftMode.DPT9002.value
        with pytest.raises(CouldNotParseTelegram):
            # DPT 6 is invalid now
            remote_value.payload_valid(dpt_6_payload)
 def to_knx(self, value: float) -> DPTArray:
     """Convert value to payload."""
     if self.dpt_class is None:
         raise ConversionError("Setpoint shift DPT not initialized for %s" %
                               self.device_name)
     if self.dpt_class == DPTValue1Count:
         converted_value = int(value / self.setpoint_shift_step)
         return DPTArray(DPTValue1Count.to_knx(converted_value))
     return DPTArray(DPTTemperature.to_knx(value))
Exemple #6
0
    def test_process_temperature(self):
        """Test process / reading telegrams from telegram queue. Test if temperature is processed correctly."""
        xknx = XKNX(loop=self.loop)
        climate = Climate(xknx,
                          'TestClimate',
                          group_address_temperature='1/2/3')

        telegram = Telegram(GroupAddress('1/2/3'))
        telegram.payload = DPTArray(DPTTemperature().to_knx(21.34))
        self.loop.run_until_complete(asyncio.Task(climate.process(telegram)))
        self.assertEqual(climate.temperature.value, 21.34)
Exemple #7
0
    def test_EndTOEnd_group_write_2bytes(self):
        """Test parsing and streaming CEMIFrame KNX/IP packet, setting value of thermostat."""
        # Incoming Temperature from thermostat
        raw = (
            0x06,
            0x10,
            0x05,
            0x30,
            0x00,
            0x13,
            0x29,
            0x00,
            0xBC,
            0xD0,
            0x14,
            0x02,
            0x08,
            0x01,
            0x03,
            0x00,
            0x80,
            0x07,
            0xC1,
        )
        xknx = XKNX(loop=self.loop)
        knxipframe = KNXIPFrame(xknx)
        knxipframe.from_knx(raw)
        telegram = knxipframe.body.cemi.telegram
        self.assertEqual(
            telegram,
            Telegram(GroupAddress("2049"),
                     payload=DPTArray(DPTTemperature().to_knx(19.85))),
        )

        knxipframe2 = KNXIPFrame(xknx)
        knxipframe2.init(KNXIPServiceType.ROUTING_INDICATION)
        knxipframe2.body.cemi.src_addr = PhysicalAddress("1.4.2")
        knxipframe2.body.cemi.telegram = telegram
        knxipframe2.body.cemi.set_hops(5)
        knxipframe2.normalize()

        self.assertEqual(knxipframe2.header.to_knx(), list(raw[0:6]))
        self.assertEqual(knxipframe2.body.to_knx(), list(raw[6:]))
        self.assertEqual(knxipframe2.to_knx(), list(raw))
Exemple #8
0
    def test_process_callback_temp(self):
        """Test process / reading telegrams from telegram queue. Test if callback is executed when receiving temperature."""
        # pylint: disable=no-self-use
        xknx = XKNX(loop=self.loop)
        climate = Climate(xknx,
                          'TestClimate',
                          group_address_temperature='1/2/3')

        after_update_callback = Mock()

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

        climate.register_device_updated_cb(async_after_update_callback)

        telegram = Telegram(GroupAddress('1/2/3'))
        telegram.payload = DPTArray(DPTTemperature().to_knx(21.34))
        self.loop.run_until_complete(asyncio.Task(climate.process(telegram)))
        after_update_callback.assert_called_with(climate)
Exemple #9
0
 def test_temperature_assert_min_exceeded_from_knx(self):
     """Testing parsing of DPTTemperature with wrong value."""
     with pytest.raises(ConversionError):
         DPTTemperature.from_knx((0xB1, 0xE6))  # -1000
Exemple #10
0
 def test_temperature_assert_min_exceeded(self):
     """Testing parsing of DPTTemperature with wrong value."""
     with pytest.raises(ConversionError):
         DPTTemperature.to_knx(-274)
Exemple #11
0
 def test_temperature_settings(self):
     """Test attributes of DPTTemperature."""
     self.assertEqual(DPTTemperature().value_min, -273)
     self.assertEqual(DPTTemperature().value_max, 670760)
     self.assertEqual(DPTTemperature().unit, "°C")
     self.assertEqual(DPTTemperature().resolution, 1)
Exemple #12
0
 def from_knx(self, payload):
     """Convert current payload to value."""
     return DPTTemperature.from_knx(payload.value)
Exemple #13
0
 def to_knx(self, value):
     """Convert value to payload."""
     return DPTArray(DPTTemperature.to_knx(value))
Exemple #14
0
 def from_knx(self, payload: DPTArray) -> float:
     """Convert current payload to value."""
     return DPTTemperature.from_knx(payload.value)