Skip to content

SslAesTransport: handshake again when a camera answers 401 to passthrough - #1771

Open
freeKC wants to merge 2 commits into
python-kasa:masterfrom
freeKC:sslaes-retry-on-401
Open

freeKC wants to merge 2 commits into
python-kasa:masterfrom
freeKC:sslaes-retry-on-401

Conversation

@freeKC

@freeKC freeKC commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Related to #1739 and home-assistant/core#179553.

Some cameras (C220 and C100 on the 1.4.4 line in those reports) drop the session about every ten minutes and answer the next securePassthrough with HTTP 401 and -40421 in the body. The transport currently turns that into a plain KasaException, which SmartProtocol._query does not retry, so Home Assistant logs an error and the request is lost. The following poll works because the transport state was reset in between.

With this change a 401 on passthrough marks the transport as needing a handshake and raises _RetryableError, so the protocol logs in again and retries the same request. That matches what already happens when the device reports SESSION_EXPIRED inside a 200 response, it just covers the case where the camera says it with the status code.

Test added: after a successful handshake, a 401 on the passthrough raises the retryable error and leaves the transport in HANDSHAKE_REQUIRED.

@codecov

codecov Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.31%. Comparing base (b7b41f2) to head (27c0880).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1771   +/-   ##
=======================================
  Coverage   93.31%   93.31%           
=======================================
  Files         158      158           
  Lines        9977     9985    +8     
  Branches     1025     1028    +3     
=======================================
+ Hits         9310     9318    +8     
  Misses        472      472           
  Partials      195      195           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rytilahti rytilahti left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after making the comment more generic and moving it up, thanks for the PR!

Comment thread kasa/transports/sslaestransport.py Outdated
Comment on lines +278 to +279
# The camera dropped the session (a C220 on 1.4.4 does this about
# every ten minutes): handshake again and retry the request.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this comment above the if condition, and remove the exact model/version numbers, and just state that some devices return this when requiring a reauth.

@freeKC

freeKC commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Done: the comment is now above the if and just says that some devices answer 401 when the session expired and they need a new handshake. Thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants