示例#1
0
 def from_knx(self, payload):
     """Convert current payload to value."""
     if payload == DPTBinary(0):
         return self.invert
     if payload == DPTBinary(1):
         return not self.invert
     raise CouldNotParseTelegram("payload invalid",
                                 payload=payload,
                                 device_name=self.device_name)
示例#2
0
文件: climate.py 项目: Karssi/xknx
 async def _process_controller_status(self, telegram):
     """Process incoming telegram for controller status."""
     if not isinstance(telegram.payload, DPTArray) \
             or len(telegram.payload.value) != 1:
         raise CouldNotParseTelegram("invalid payload",
                                     payload=telegram.payload,
                                     device_name=self.name)
     operation_mode = DPTControllerStatus.from_knx(telegram.payload.value)
     await self._set_internal_operation_mode(operation_mode)
示例#3
0
 def from_knx(self, payload):
     """Convert current payload to value."""
     if payload == DPTBinary(0):
         return self.Direction.DECREASE if self.invert else self.Direction.INCREASE
     if payload == DPTBinary(1):
         return self.Direction.INCREASE if self.invert else self.Direction.DECREASE
     raise CouldNotParseTelegram("payload invalid",
                                 payload=payload,
                                 device_name=self.device_name)
示例#4
0
 def from_knx(self, payload):
     """Convert current payload to value."""
     if payload == DPTBinary(1):
         return self.operation_mode
     if payload == DPTBinary(0):
         return None
     raise CouldNotParseTelegram("payload invalid",
                                 payload=payload,
                                 device_name=self.device_name,
                                 feature_name=self.feature_name)
示例#5
0
 def payload_valid(
         self,
         payload: DPTArray | DPTBinary | None) -> DPTArray | DPTBinary:
     """Test if telegram payload may be parsed."""
     if isinstance(payload, DPTBinary) and self.payload_length == 0:
         return payload
     if isinstance(payload, DPTArray) and len(
             payload.value) == self.payload_length:
         return payload
     raise CouldNotParseTelegram("Payload invalid", payload=str(payload))
示例#6
0
 async def process(self,
                   telegram: Telegram,
                   always_callback: bool = False) -> bool:
     """Process incoming or outgoing telegram."""
     if not isinstance(telegram.destination_address,
                       (GroupAddress, InternalGroupAddress
                        )) or not self.has_group_address(
                            telegram.destination_address):
         return False
     if not isinstance(
             telegram.payload,
         (
             GroupValueWrite,
             GroupValueResponse,
         ),
     ):
         raise CouldNotParseTelegram(
             "payload not a GroupValueWrite or GroupValueResponse",
             payload=str(telegram.payload),
             destination_address=str(telegram.destination_address),
             source_address=str(telegram.source_address),
             device_name=self.device_name,
             feature_name=self.feature_name,
         )
     _new_payload = self.payload_valid(telegram.payload.value)
     if _new_payload is None:
         raise CouldNotParseTelegram(
             "payload invalid",
             payload=str(telegram.payload),
             destination_address=str(telegram.destination_address),
             source_address=str(telegram.source_address),
             device_name=self.device_name,
             feature_name=self.feature_name,
         )
     decoded_payload = self.from_knx(_new_payload)
     self.xknx.state_updater.update_received(self)
     if self._value is None or always_callback or self._value != decoded_payload:
         self._value = decoded_payload
         self.telegram = telegram
         if self.after_update_cb is not None:
             await self.after_update_cb()
     return True
示例#7
0
    def process(self, telegram):
        """Process incoming telegram."""
        bit_masq = 1 << (self.significant_bit - 1)
        binary_value = telegram.payload.value & bit_masq != 0

        if binary_value == 0:
            yield from self._set_internal_state(BinarySensorState.OFF)
        elif binary_value == 1:
            yield from self._set_internal_state(BinarySensorState.ON)
        else:
            raise CouldNotParseTelegram()
示例#8
0
 def process(self, telegram):
     """Process incoming telegram."""
     if not self.has_group_address(telegram.group_address):
         return False
     if not self.payload_valid(telegram.payload):
         raise CouldNotParseTelegram()
     if self.payload != telegram.payload:
         self.payload = telegram.payload
         if self.after_update_cb is not None:
             yield from self.after_update_cb()
     return True
示例#9
0
 def from_knx(self, payload):
     """Convert current payload to value."""
     if payload.value & ~0x0F != 0:  # more than 4-bit
         pass  # raises exception below
     elif payload.value & 0x07 == 0:
         return self.Direction.STOP
     elif payload.value & 0x08 == 0:
         return self.Direction.UP if not self.invert else self.Direction.DOWN
     else:
         return self.Direction.DOWN if not self.invert else self.Direction.UP
     raise CouldNotParseTelegram("payload invalid", payload=payload, device_name=self.device_name)
示例#10
0
 def from_knx(self, payload):
     """Convert current payload to value."""
     if payload == DPTBinary(1):
         return self.operation_mode
     if payload == DPTBinary(0):
         # return the other operation mode
         return next((_op for _op in self.supported_operation_modes()
                      if _op is not self.operation_mode), None)
     raise CouldNotParseTelegram("payload invalid",
                                 payload=payload,
                                 device_name=self.device_name,
                                 feature_name=self.feature_name)
示例#11
0
 def from_knx(self, payload: "DPTPayload") -> Optional[HVACOperationMode]:
     """Convert current payload to value."""
     if payload == DPTBinary(1):
         return self.operation_mode
     if payload == DPTBinary(0):
         return None
     raise CouldNotParseTelegram(
         "payload invalid",
         payload=str(payload),
         device_name=self.device_name,
         feature_name=self.feature_name,
     )
示例#12
0
 def from_knx(self, payload: DPTBinary) -> "RemoteValueUpDown.Direction":
     """Convert current payload to value."""
     if payload == DPTBinary(0):
         return self.Direction.DOWN if self.invert else self.Direction.UP
     if payload == DPTBinary(1):
         return self.Direction.UP if self.invert else self.Direction.DOWN
     raise CouldNotParseTelegram(
         "payload invalid",
         payload=payload,
         device_name=self.device_name,
         feature_name=self.feature_name,
     )
示例#13
0
    async def process_group_write(self, telegram):
        """Process incoming GROUP WRITE telegram."""
        if not isinstance(telegram.payload, DPTBinary):
            raise CouldNotParseTelegram("invalid payload", payload=telegram.payload, device_name=self.name)

        bit_masq = 1 << (self.significant_bit-1)
        if telegram.payload.value & bit_masq == 0:
            await self._set_internal_state(BinarySensorState.OFF)
        else:
            await self._set_internal_state(BinarySensorState.ON)
            if self.reset_after is not None:
                await asyncio.sleep(self.reset_after/1000)
                await self._set_internal_state(BinarySensorState.OFF)
示例#14
0
 async def process(self, telegram):
     """Process incoming telegram."""
     if not self.has_group_address(telegram.group_address):
         return False
     if not self.payload_valid(telegram.payload):
         raise CouldNotParseTelegram("payload invalid",
                                     payload=telegram.payload,
                                     group_address=telegram.group_address,
                                     device_name=self.device_name)
     if self.payload != telegram.payload or self.payload is None:
         self.payload = telegram.payload
         if self.after_update_cb is not None:
             await self.after_update_cb()
     return True
示例#15
0
 def payload_valid(self, payload: DPTArray | DPTBinary | None) -> DPTArray:
     """Test if telegram payload may be parsed."""
     if isinstance(payload, DPTArray):
         payload_length = len(payload.value)
         if self.dpt_class is None:
             if payload_length == DPTTemperature.payload_length:
                 self.dpt_class = DPTTemperature
                 return payload
             if payload_length == DPTValue1Count.payload_length:
                 self.dpt_class = DPTValue1Count
                 return payload
         elif payload_length == self.dpt_class.payload_length:
             return payload
     raise CouldNotParseTelegram("Payload invalid", payload=str(payload))
示例#16
0
 async def process(self, telegram, always_callback=False):
     """Process incoming telegram."""
     if not self.has_group_address(telegram.group_address):
         return False
     if not self.payload_valid(telegram.payload):
         raise CouldNotParseTelegram("payload invalid",
                                     payload=telegram.payload,
                                     group_address=telegram.group_address,
                                     device_name=self.device_name,
                                     feature_name=self.feature_name)
     self.xknx.state_updater.update_received(self)
     if self.payload is None or always_callback \
             or self.payload != telegram.payload:
         self.payload = telegram.payload
         if self.after_update_cb is not None:
             await self.after_update_cb()
     return True
示例#17
0
 def from_knx(self, payload: DPTPayloadT) -> HVACControllerMode | None:
     """Convert current payload to value."""
     if payload == DPTBinary(1):
         return self.controller_mode
     if payload == DPTBinary(0):
         # return the other operation mode
         return next(
             (_op for _op in self.supported_operation_modes()
              if _op is not self.controller_mode),
             None,
         )
     raise CouldNotParseTelegram(
         "payload invalid",
         payload=str(payload),
         device_name=self.device_name,
         feature_name=self.feature_name,
     )
示例#18
0
    def test_process_exception(self, process_tg_in_mock, logging_error_mock):
        """Test process_telegram exception handling."""
        # pylint: disable=no-self-use
        xknx = XKNX(loop=self.loop)

        async def process_exception():
            raise CouldNotParseTelegram(
                "Something went wrong when receiving the telegram."
                "")

        process_tg_in_mock.return_value = asyncio.ensure_future(
            process_exception())

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

        self.loop.run_until_complete(
            asyncio.Task(xknx.telegram_queue.process_telegram(telegram)))
        logging_error_mock.assert_called_once_with(
            "Error while processing telegram %s",
            CouldNotParseTelegram(
                "Something went wrong when receiving the telegram."
                ""))
示例#19
0
 def payload_valid(self,
                   payload: DPTArray | DPTBinary | None) -> DPTPayloadT:
     """Return payload if telegram payload may be parsed - to be implemented in derived class."""
     raise CouldNotParseTelegram("Payload invalid", payload=str(payload))
示例#20
0
 def _process_message(self, telegram):
     """Process incoming telegram for on/off state."""
     if not isinstance(telegram.payload, DPTString):
         raise CouldNotParseTelegram()
     yield from self._set_internal_message(telegram.payload.value)
示例#21
0
            ConversionError("desc1"),
            ConversionError("desc1"),
            ConversionError("desc2"),
        ),
        (
            CouldNotParseAddress(123),
            CouldNotParseAddress(123),
            CouldNotParseAddress(321),
        ),
        (
            CouldNotParseKNXIP("desc1"),
            CouldNotParseKNXIP("desc1"),
            CouldNotParseKNXIP("desc2"),
        ),
        (
            CouldNotParseTelegram("desc", arg1=1, arg2=2),
            CouldNotParseTelegram("desc", arg1=1, arg2=2),
            CouldNotParseTelegram("desc", arg1=2, arg2=1),
        ),
        (
            DeviceIllegalValue("value1", "desc"),
            DeviceIllegalValue("value1", "desc"),
            DeviceIllegalValue("value1", "desc2"),
        ),
        (
            XKNXException("desc1"),
            XKNXException("desc1"),
            XKNXException("desc2"),
        ),
    ],
)
示例#22
0
 async def process_exception():
     raise CouldNotParseTelegram(
         "Something went wrong when receiving the telegram."
         "")
示例#23
0
 def test_could_not_parse_telegramn_exception(self):
     """Test string representation of CouldNotParseTelegram exception."""
     exception = CouldNotParseTelegram(description='Fnord')
     self.assertEqual(str(exception),
                      '<CouldNotParseTelegram description="Fnord" />')
示例#24
0
 def payload_valid(self, payload: DPTArray | DPTBinary | None) -> DPTBinary:
     """Test if telegram payload may be parsed."""
     if isinstance(payload, DPTBinary):
         return payload
     raise CouldNotParseTelegram("Payload invalid", payload=str(payload))