Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(message): improve debug log for send and received msg with attr… #294

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions midealocal/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,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

Expand Down Expand Up @@ -169,19 +177,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):
Expand Down
Loading