1
0
mirror of https://github.com/mjg59/python-broadlink.git synced 2024-11-10 18:00:12 +01:00

Improve repr(device) and str(device) (#550)

This commit is contained in:
Felipe Martins Diel 2021-03-11 02:51:29 -03:00 committed by GitHub
parent 90a43835e8
commit a11b7233c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -98,15 +98,15 @@ class device:
mac: Union[bytes, str], mac: Union[bytes, str],
devtype: int, devtype: int,
timeout: int = 10, timeout: int = 10,
name: str = None, name: str = "",
model: str = None, model: str = "",
manufacturer: str = None, manufacturer: str = "",
is_locked: bool = None, is_locked: bool = False,
) -> None: ) -> None:
"""Initialize the controller.""" """Initialize the controller."""
self.host = host self.host = host
self.mac = bytes.fromhex(mac) if isinstance(mac, str) else mac self.mac = bytes.fromhex(mac) if isinstance(mac, str) else mac
self.devtype = devtype if devtype is not None else 0x272A self.devtype = devtype
self.timeout = timeout self.timeout = timeout
self.name = name self.name = name
self.model = model self.model = model
@ -121,25 +121,40 @@ class device:
self.aes = None self.aes = None
self.update_aes(bytes.fromhex(self.__INIT_KEY)) self.update_aes(bytes.fromhex(self.__INIT_KEY))
def __repr__(self): def __repr__(self) -> str:
return "<%s: %s %s (%s) at %s:%s | %s | %s | %s>" % ( """Return a formal representation of the device."""
type(self).__name__, return (
self.manufacturer, "%s.%s(%s, mac=%r, devtype=%r, timeout=%r, name=%r, "
self.model, "model=%r, manufacturer=%r, is_locked=%r)"
hex(self.devtype), ) % (
self.host[0], self.__class__.__module__,
self.host[1], self.__class__.__qualname__,
":".join(format(x, "02x") for x in self.mac), self.host,
self.mac,
self.devtype,
self.timeout,
self.name, self.name,
"Locked" if self.is_locked else "Unlocked", self.model,
self.manufacturer,
self.is_locked,
) )
def __str__(self): def __str__(self) -> str:
return "%s (%s at %s)" % ( """Return a readable representation of the device."""
self.name, model = []
self.model or hex(self.devtype), if self.manufacturer:
self.host[0], model.append(self.manufacturer)
) if self.model:
model.append(self.model)
model.append(hex(self.devtype))
model = " ".join(model)
info = []
info.append(model)
info.append(f"{self.host[0]}:{self.host[1]}")
info.append(":".join(format(x, "02x") for x in self.mac).upper())
info = " / ".join(info)
return "%s (%s)" % (self.name or "Unknown", info)
def update_aes(self, key: bytes) -> None: def update_aes(self, key: bytes) -> None:
"""Update AES.""" """Update AES."""
@ -225,7 +240,7 @@ class device:
packet = bytearray(4) packet = bytearray(4)
packet += name.encode("utf-8") packet += name.encode("utf-8")
packet += bytearray(0x50 - len(packet)) packet += bytearray(0x50 - len(packet))
packet[0x43] = bool(self.is_locked) packet[0x43] = self.is_locked
response = self.send_packet(0x6A, packet) response = self.send_packet(0x6A, packet)
check_error(response[0x22:0x24]) check_error(response[0x22:0x24])
self.name = name self.name = name