From 8e51fa7d31751c83c11e7979d50a3906341454a0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 12 Jan 2024 15:33:24 -1000 Subject: [PATCH] Add additional debug logging --- kasa/protocol.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/kasa/protocol.py b/kasa/protocol.py index 7ab2c47f..1b53a680 100755 --- a/kasa/protocol.py +++ b/kasa/protocol.py @@ -80,10 +80,10 @@ class TPLinkSmartHomeProtocol: assert self.writer is not None # noqa: S101 assert self.reader is not None # noqa: S101 debug_log = _LOGGER.isEnabledFor(logging.DEBUG) - + encrypted = TPLinkSmartHomeProtocol.encrypt(request) if debug_log: - _LOGGER.debug("%s >> %s", self.host, request) - self.writer.write(TPLinkSmartHomeProtocol.encrypt(request)) + _LOGGER.debug("%s >> (%s) %s", self.host, encrypted.hex(), request) + self.writer.write(encrypted) await self.writer.drain() packed_block_size = await self.reader.readexactly(self.BLOCK_SIZE) @@ -93,7 +93,7 @@ class TPLinkSmartHomeProtocol: response = TPLinkSmartHomeProtocol.decrypt(buffer) json_payload = json_loads(response) if debug_log: - _LOGGER.debug("%s << %s", self.host, pf(json_payload)) + _LOGGER.debug("%s << (%s) %s", self.host, buffer.hex(), pf(json_payload)) return json_payload @@ -101,10 +101,15 @@ class TPLinkSmartHomeProtocol: """Close the connection.""" writer = self.writer self.reader = self.writer = None + debug_log = _LOGGER.isEnabledFor(logging.DEBUG) if writer: + if debug_log: + _LOGGER.debug("%s: closing connection", self.host) writer.close() with contextlib.suppress(Exception): await writer.wait_closed() + elif debug_log: + _LOGGER.debug("%s: connection already closed", self.host) def _reset(self) -> None: """Clear any varibles that should not survive between loops."""