From 4fc656e110f86c30a9ab8d7b6f55c46b390a4909 Mon Sep 17 00:00:00 2001 From: Hello World Date: Thu, 12 Sep 2024 14:54:25 +0800 Subject: [PATCH] =?UTF-8?q?chore(message):=20improve=20debug=20log=20for?= =?UTF-8?q?=20send=20and=20received=20msg=20with=20attr=E2=80=A6=20(#294)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. print all attributes and value to debug log in send and received msg 2. update message_type to the expected type and not the value 3. add self object to debug log 4. add `_format_attribute` func to format all the bytes/bytearray attributes. 5. add `get_key_from_value` in `MessageType` ## Summary by CodeRabbit - **New Features** - Introduced a method to retrieve keys from integer values in message types, enhancing user interaction with message classifications. - Added a formatting method for message attributes, improving the representation of various data types, including hexadecimal conversion for byte values. - **Improvements** - Enhanced the string representation of message requests for better clarity and usability, making it easier for users to understand message details. --- midealocal/message.py | 55 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/midealocal/message.py b/midealocal/message.py index 6173acfc..9ec80ae8 100644 --- a/midealocal/message.py +++ b/midealocal/message.py @@ -103,6 +103,14 @@ class MessageType(IntEnum): exception2 = (0x0A,) query_appliance = 0xA0 + @classmethod + def get_key_from_value(cls, value: int) -> str: + """Return the key corresponding to the given value.""" + for key, val in cls.__members__.items(): + if val == value: + return key + return "Unknown" + ZERO_VALUE = 0x00 @@ -170,19 +178,46 @@ def protocol_version(self) -> int: def protocol_version(self, protocol_version: int) -> None: self._protocol_version = protocol_version + def _format_attribute( + self, + value: bytes | bytearray | int | str | bool | object, + ) -> int | str | bool | object: + """Format value as a hex string if it's bytes or bytearray. + + Args: + ---- + value (bytes | bytearray | int | str | bool): value to be formatted. + + Returns: + ------- + int | str | bool: The formatted result. + + """ + if isinstance(value, bytes | bytearray): + return value.hex() + return value + def __str__(self) -> str: """Parse to string.""" - output = { - "header": self.header.hex(), - "body": self.body.hex(), - "message type": f".{f'{self.message_type:02x}'}", - "body type": ( - f".{f'{self._body_type:02x}'}" - if self._body_type is not None - else "None" - ), + # get attributes and value + attributes = { + key: self._format_attribute(value) for key, value in self.__dict__.items() } - return str(output) + + # update some attributes + attributes.update( + { + "header": self._format_attribute(self.header), + "body": self._format_attribute(self.body), + "message_type": MessageType.get_key_from_value(self.message_type), + "body_type": ( + f"{self._body_type:02x}" if self._body_type is not None else "None" + ), + "self": self, + }, + ) + + return str(attributes) class MessageRequest(MessageBase):