handle cancellation

This commit is contained in:
J. Nick Koston 2024-01-12 16:17:02 -10:00
parent 8868c9b70d
commit 84dd2984ea
No known key found for this signature in database

View File

@ -80,10 +80,9 @@ class TPLinkSmartHomeProtocol:
assert self.writer is not None # noqa: S101 assert self.writer is not None # noqa: S101
assert self.reader is not None # noqa: S101 assert self.reader is not None # noqa: S101
debug_log = _LOGGER.isEnabledFor(logging.DEBUG) debug_log = _LOGGER.isEnabledFor(logging.DEBUG)
encrypted = TPLinkSmartHomeProtocol.encrypt(request)
if debug_log: if debug_log:
_LOGGER.debug("%s >> (%s) %s", self.host, encrypted.hex(), request) _LOGGER.debug("%s >> %s", self.host, request)
self.writer.write(encrypted) self.writer.write(TPLinkSmartHomeProtocol.encrypt(request))
await self.writer.drain() await self.writer.drain()
packed_block_size = await self.reader.readexactly(self.BLOCK_SIZE) packed_block_size = await self.reader.readexactly(self.BLOCK_SIZE)
@ -93,7 +92,7 @@ class TPLinkSmartHomeProtocol:
response = TPLinkSmartHomeProtocol.decrypt(buffer) response = TPLinkSmartHomeProtocol.decrypt(buffer)
json_payload = json_loads(response) json_payload = json_loads(response)
if debug_log: 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 return json_payload
@ -109,13 +108,8 @@ class TPLinkSmartHomeProtocol:
"""Close the connection without waiting for the connection to close.""" """Close the connection without waiting for the connection to close."""
writer = self.writer writer = self.writer
self.reader = self.writer = None self.reader = self.writer = None
debug_log = _LOGGER.isEnabledFor(logging.DEBUG)
if writer: if writer:
if debug_log:
_LOGGER.debug("%s: closing connection", self.host)
writer.close() writer.close()
elif debug_log:
_LOGGER.debug("%s: connection already closed", self.host)
def _reset(self) -> None: def _reset(self) -> None:
"""Clear any varibles that should not survive between loops.""" """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 # Likely something cancelled the task so we need to close the connection
self.close_without_wait() self.close_without_wait()
_LOGGER.debug( _LOGGER.debug(
"BaseException during connect, closing connection: %s", "%s: BaseException during connect, closing connection: %s",
self.host, self.host,
ex, ex,
) )
@ -187,7 +181,9 @@ class TPLinkSmartHomeProtocol:
# Likely something cancelled the task so we need to close the connection # Likely something cancelled the task so we need to close the connection
self.close_without_wait() self.close_without_wait()
_LOGGER.debug( _LOGGER.debug(
"BaseException during query, closing connection: %s", self.host, ex "%s: BaseException during query, closing connection: %s",
self.host,
ex,
) )
raise raise