Skip to content

Commit

Permalink
Make all klap logging lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
sdb9696 committed Sep 15, 2023
1 parent d759c82 commit 9ec0799
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions kasa/klapprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,14 @@ async def perform_handshake1(
session, url, data=payload
)

cookie = self.jar.filter_cookies(url).get(self.TP_SESSION_COOKIE_NAME)
if _LOGGER.isEnabledFor(logging.DEBUG):
_LOGGER.debug(

Check warning on line 168 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L168

Added line #L168 was not covered by tests
"Handshake1 posted at %s. Host is %s, Response status is %s, Request was %s",
datetime.datetime.now(),
self.host,
response_status,
payload and payload.hex(),
)

if response_status != 200:
raise AuthenticationException(

Check warning on line 177 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L177

Added line #L177 was not covered by tests
Expand All @@ -175,16 +182,16 @@ async def perform_handshake1(
remote_seed = response_data[0:16]
server_hash = response_data[16:]

Check warning on line 183 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L182-L183

Added lines #L182 - L183 were not covered by tests

cookie = self.jar.filter_cookies(url).get(self.TP_SESSION_COOKIE_NAME)
_LOGGER.debug(
f"Handshake1 posted at {datetime.datetime.now()}. Host is {self.host}, Session cookie is {cookie}, Response status is {response_status}, Request was {self.local_auth_hash.hex()}"
)

_LOGGER.debug(
"Server remote_seed is: %s, server hash is: %s",
remote_seed.hex(),
server_hash.hex(),
)
if _LOGGER.isEnabledFor(logging.DEBUG):
cookie = self.jar.filter_cookies(url).get(self.TP_SESSION_COOKIE_NAME)
_LOGGER.debug(

Check warning on line 187 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L186-L187

Added lines #L186 - L187 were not covered by tests
"Handshake1 success at %s. Host is %s, Cookie is %s, Server remote_seed is: %s, server hash is: %s",
datetime.datetime.now(),
self.host,
cookie,
remote_seed.hex(),
server_hash.hex(),
)

local_seed_auth_hash = self._sha256(self._local_seed + self.local_auth_hash)

Check warning on line 196 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L196

Added line #L196 was not covered by tests

Expand Down Expand Up @@ -242,10 +249,15 @@ async def perform_handshake2(self, session, remote_seed, auth_hash) -> None:
session, url, data=payload
)

cookie = self.jar.filter_cookies(url).get(self.TP_SESSION_COOKIE_NAME)
_LOGGER.debug(
f"Handshake2 posted {datetime.datetime.now()}. Host is {self.host}, Session cookie is {cookie}, Response status is {response_status}, Request was {payload!r}"
)
if _LOGGER.isEnabledFor(logging.DEBUG):
_LOGGER.debug(

Check warning on line 253 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L253

Added line #L253 was not covered by tests
"Handshake2 posted %s. Host is %s, Session cookie is %s, Response status is %s, Request was %s",
datetime.datetime.now(),
self.host,
self.jar.filter_cookies(url).get(self.TP_SESSION_COOKIE_NAME),
response_status,
payload.hex(),
)

if response_status != 200:
self.authentication_failed = True
Expand Down Expand Up @@ -336,7 +348,8 @@ async def _execute_query(self, request: str, retry_count: int) -> Dict:

except AuthenticationException as auex:
_LOGGER.debug(

Check warning on line 350 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L349-L350

Added lines #L349 - L350 were not covered by tests
f"Unable to complete handshake for device {self.host}, authentication failed"
"Unable to complete handshake for device %s, authentication failed",
self.host,
)
raise auex

Check warning on line 354 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L354

Added line #L354 was not covered by tests

Expand All @@ -352,10 +365,10 @@ async def _execute_query(self, request: str, retry_count: int) -> Dict:
session, url, params={"seq": seq}, data=payload
)

msg = f"at {datetime.datetime.now()}. Host is {self.host}, Session cookie is {cookie}, Retry count is {retry_count}, Sequence is {seq}, Response status is {response_status}, Request was {request}"

Check warning on line 368 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L368

Added line #L368 was not covered by tests
if response_status != 200:
_LOGGER.error(
f"Query failed after succesful authentication at {datetime.datetime.now()}. Host is {self.host}, Session cookie is {cookie}, Retry count is {retry_count}, Sequence is {seq}, Response status is {response_status}, Request was {request}"
)
if _LOGGER.isEnabledFor(logging.ERROR):
_LOGGER.error("Query failed after succesful authentication " + msg)

Check warning on line 371 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L371

Added line #L371 was not covered by tests
# If we failed with a security error, force a new handshake next time.
if response_status == 403:
self.handshake_done = False
Expand All @@ -369,9 +382,8 @@ async def _execute_query(self, request: str, retry_count: int) -> Dict:
% (self.host, response_status, seq)
)
else:
_LOGGER.debug(
f"Query posted at {datetime.datetime.now()}. Host is {self.host}, Session cookie is {cookie}, Retry count is {retry_count}, Sequence is {seq}, Response status is {response_status}, Request was {request}"
)
if _LOGGER.isEnabledFor(logging.DEBUG):
_LOGGER.debug("Query posted " + msg)

Check warning on line 386 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L386

Added line #L386 was not covered by tests

self.handle_cookies(session, url)

Check warning on line 388 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L388

Added line #L388 was not covered by tests

Expand All @@ -383,7 +395,11 @@ async def _execute_query(self, request: str, retry_count: int) -> Dict:

json_payload = json_loads(decrypted_response)

Check warning on line 396 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L396

Added line #L396 was not covered by tests

_LOGGER.debug("%s << %s", self.host, pf(json_payload))
_LOGGER.debug(

Check warning on line 398 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L398

Added line #L398 was not covered by tests
"%s << %s",
self.host,
_LOGGER.isEnabledFor(logging.DEBUG) and pf(json_payload),
)

return json_payload

Check warning on line 404 in kasa/klapprotocol.py

View check run for this annotation

Codecov / codecov/patch

kasa/klapprotocol.py#L404

Added line #L404 was not covered by tests

Expand Down

0 comments on commit 9ec0799

Please sign in to comment.