Skip to content

Commit

Permalink
Add host information to protocol debug logs (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
rytilahti committed Sep 26, 2021
1 parent 76c1264 commit 3cf549e
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions kasa/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def _execute_query(self, request: str) -> Dict:
debug_log = _LOGGER.isEnabledFor(logging.DEBUG)

if debug_log:
_LOGGER.debug("> (%i) %s", len(request), request)
_LOGGER.debug("%s >> %s", self.host, request)
self.writer.write(TPLinkSmartHomeProtocol.encrypt(request))
await self.writer.drain()

Expand All @@ -104,7 +104,8 @@ async def _execute_query(self, request: str) -> Dict:
response = TPLinkSmartHomeProtocol.decrypt(buffer)
json_payload = json.loads(response)
if debug_log:
_LOGGER.debug("< (%i) %s", len(response), pf(json_payload))
_LOGGER.debug("%s << %s", self.host, pf(json_payload))

return json_payload

async def close(self):
Expand All @@ -129,7 +130,7 @@ async def _query(self, request: str, retry_count: int, timeout: int) -> Dict:
if not await self._connect(timeout):
await self.close()
if retry >= retry_count:
_LOGGER.debug("Giving up after %s retries", retry)
_LOGGER.debug("Giving up on %s after %s retries", self.host, retry)
raise SmartDeviceException(
f"Unable to connect to the device: {self.host}"
)
Expand All @@ -144,12 +145,14 @@ async def _query(self, request: str, retry_count: int, timeout: int) -> Dict:
except Exception as ex:
await self.close()
if retry >= retry_count:
_LOGGER.debug("Giving up after %s retries", retry)
_LOGGER.debug("Giving up on %s after %s retries", self.host, retry)
raise SmartDeviceException(
f"Unable to query the device: {ex}"
f"Unable to query the device {self.host}: {ex}"
) from ex

_LOGGER.debug("Unable to query the device, retrying: %s", ex)
_LOGGER.debug(
"Unable to query the device %s, retrying: %s", self.host, ex
)

# make mypy happy, this should never be reached..
await self.close()
Expand Down

0 comments on commit 3cf549e

Please sign in to comment.