From 84dd2984eaafbb8a2e530b9b87627495ad97f057 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 12 Jan 2024 16:17:02 -1000 Subject: [PATCH] handle cancellation --- kasa/protocol.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/kasa/protocol.py b/kasa/protocol.py index faa1bdd6..23c8edae 100755 --- a/kasa/protocol.py +++ b/kasa/protocol.py @@ -80,10 +80,9 @@ 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) %s", self.host, encrypted.hex(), request) - self.writer.write(encrypted) + _LOGGER.debug("%s >> %s", self.host, request) + self.writer.write(TPLinkSmartHomeProtocol.encrypt(request)) await self.writer.drain() packed_block_size = await self.reader.readexactly(self.BLOCK_SIZE) @@ -93,7 +92,7 @@ class TPLinkSmartHomeProtocol: response = TPLinkSmartHomeProtocol.decrypt(buffer) json_payload = json_loads(response) if debug_log: - _LOGGER.debug("%s << (%s) %s", self.host, buffer.hex(), pf(json_payload)) + _LOGGER.debug("%s << %s", self.host, pf(json_payload)) return json_payload @@ -109,13 +108,8 @@ class TPLinkSmartHomeProtocol: """Close the connection without waiting for the connection to close.""" 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() - elif debug_log: - _LOGGER.debug("%s: connection already closed", self.host) def _reset(self) -> None: """Clear any varibles that should not survive between loops.""" @@ -161,7 +155,7 @@ class TPLinkSmartHomeProtocol: # Likely something cancelled the task so we need to close the connection self.close_without_wait() _LOGGER.debug( - "BaseException during connect, closing connection: %s", + "%s: BaseException during connect, closing connection: %s", self.host, ex, ) @@ -187,7 +181,9 @@ class TPLinkSmartHomeProtocol: # Likely something cancelled the task so we need to close the connection self.close_without_wait() _LOGGER.debug( - "BaseException during query, closing connection: %s", self.host, ex + "%s: BaseException during query, closing connection: %s", + self.host, + ex, ) raise