GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add klap protocol by sdb9696 · Pull Request #509 · python-kasa/python-kasa

Conversation

@sdb9696

@sdb9696 sdb9696 commented Sep 14, 2023 •
edited by rytilahti
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This pull request is a new copy of PR #477 which was broken into multiple smaller PRs (#488 & #507) to prepare the way for supporting the kasa klap protocol and potentially other methods of authentication. PR 477 built on the work done by @SimonWilkinson #117.

Background

KASA introduced the new klap protocol in Nov 2020 #115 via an automatic firmware update that largely affected HS100 models in the UK (and perhaps Italy). Whilst tplink stopped the rollout due to complaints from home assistant users it should be noted that tplink recently rolled out an update to klap v2 to some of their TAPO devices which stopped them working in a similar fashion.

Notes

Tests will be added once the PR is broadly agreed

Discovery responses

kasa klap:

{'result': {'ip': '192.168.123.123', 'mac': '12-34-56-78-9A-BC', 'device_id': '123456789ABCDEF123456789ABCDEF12', 'owner': 'fedcba987654321fedcba987654321fe', 'device_type': 'IOT.SMARTPLUGSWITCH', 'device_model': 'HS100(UK)', 'hw_ver': '4.1', 'factory_default': True, 'mgt_encrypt_schm': {'is_support_https': False, 'encrypt_type': 'KLAP', 'http_port': 80}}, 'error_code': 0}

TAPO klap:

 {'result': {'device_id': '****', 'owner': '****', 'device_type': 'SMART.TAPOBULB', 'device_model': 'L530E(EU)', 'ip': '****', 'mac': '*****', 'is_support_iot_cloud': True, 'obd_src': 'tplink', 'factory_default': False, 'mgt_encrypt_schm': {'is_support_https': False, 'encrypt_type': 'KLAP', 'http_port': 80, 'lv': 2}}, 'error_code': 0}

N.B.the discovery response for a kasa klap device doesn't have 'lv: 2.

Fixes #115

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@codecov

codecov Bot commented Sep 14, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

Attention: 45 lines in your changes are missing coverage. Please review.

Comparison is base (bde07d1) 80.46% compared to head (f5d8041) 81.63%.

Files Patch % Lines
kasa/klapprotocol.py 92.20% 5 Missing and 13 partials ⚠️
kasa/cli.py 13.33% 12 Missing and 1 partial ⚠️
kasa/discover.py 83.11% 8 Missing and 5 partials ⚠️
kasa/smartdevice.py 94.11% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #509      +/-   ##
==========================================
+ Coverage   80.46%   81.63%   +1.16%     
==========================================
  Files          27       28       +1     
  Lines        2104     2417     +313     
  Branches      639      683      +44     
==========================================
+ Hits         1693     1973     +280     
- Misses        366      383      +17     
- Partials       45       61      +16     

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sdb9696 sdb9696 mentioned this pull request Sep 14, 2023
Adding support for the new encryption protocol (updated version 2023) #477
Closed
bdraco
bdraco reviewed Sep 14, 2023
View reviewed changes
Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

bdraco
bdraco reviewed Sep 14, 2023
View reviewed changes
Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

bdraco
bdraco reviewed Sep 14, 2023
View reviewed changes
Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

bdraco
bdraco reviewed Sep 14, 2023
View reviewed changes
Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

bdraco
bdraco reviewed Sep 14, 2023
View reviewed changes
Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

bdraco
bdraco reviewed Sep 14, 2023
View reviewed changes
Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@sdb9696
sdb9696 force-pushed the add_klap_protocol branch from 9ec0799 to 1549299 Compare September 15, 2023 14:42
rytilahti
rytilahti requested changes Sep 15, 2023
View reviewed changes

@rytilahti rytilahti left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Great work on those other PRs, this PR is now so much easier to review! :-)

I added some comments, but could you please add some initial tests? Even initializing the klapprotocol class without any real tests would make it much easier to read as codecov would not warn on every code block.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread kasa/discover.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/discover.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/discover.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@sdb9696
sdb9696 requested a review from rytilahti September 18, 2023 20:50
@sdb9696

sdb9696 commented Sep 23, 2023

Copy link
Copy Markdown
Contributor Author

Gentle nudge @rytilahti

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Oct 4, 2023
Make timeout adjustable #494
Merged
Add a connect_single method to Discover to avoid the need for UDP #528
Merged
rytilahti
rytilahti reviewed Oct 8, 2023
View reviewed changes

@rytilahti rytilahti left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Sorry for keep you waiting @sdb9696, I was traveling and too busy with other things but I finally sat down and took a look into the klapprotocol, and added plenty of comments on how to make the code cleaner and easier to understand (and to maintain, I hope :-)).

I'll do another review with fresh eyes later, but let's start getting it cleaned for the next release :-)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread kasa/cli.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/discover.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/protocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/protocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/tests/test_discovery.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@rytilahti rytilahti added this to the 0.5.4 milestone Oct 8, 2023
@rytilahti rytilahti added the enhancement New feature or request label Oct 14, 2023
@rytilahti rytilahti mentioned this pull request Oct 18, 2023
Radiator support (KE100) #422
Closed
@insomniacslk insomniacslk mentioned this pull request Oct 19, 2023
Implement KLAP protocol insomniacslk/tapo#1
Closed
@rytilahti rytilahti mentioned this pull request Oct 29, 2023
contributing KasaCam support #530
Closed
@rytilahti rytilahti modified the milestones: 0.5.4, 0.5.5 Oct 29, 2023
@rytilahti rytilahti mentioned this pull request Oct 31, 2023
[WIP] Kasa Cam support #537
Closed
@sdb9696
sdb9696 force-pushed the add_klap_protocol branch from c8b6cc8 to 04fec50 Compare November 1, 2023 12:05
@sdb9696

sdb9696 commented Nov 1, 2023

Copy link
Copy Markdown
Contributor Author

Sorry for keep you waiting @sdb9696, I was traveling and too busy with other things but I finally sat down and took a look into the klapprotocol, and added plenty of comments on how to make the code cleaner and easier to understand (and to maintain, I hope :-)).

I'll do another review with fresh eyes later, but let's start getting it cleaned for the next release :-)

Hi @rytilahti, apologies for the delay. I've integrated nearly all of your comments so far. There are a couple still open with replies.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sdb9696
sdb9696 force-pushed the add_klap_protocol branch from 04fec50 to fc337e5 Compare November 1, 2023 14:58
@sdb9696
sdb9696 requested a review from rytilahti November 1, 2023 17:28
SimonWilkinson and others added 2 commits November 7, 2023 07:00
@SimonWilkinson @sdb9696
Add support for the new encryption protocol
7ba5e34
This adds support for the new TP-Link discovery and encryption
protocols. It is currently incomplete - only devices without
username and password are current supported, and single device
discovery is not implemented.

Discovery should find both old and new devices. When accessing
a device by IP the --klap option can be specified on the command
line to active the new connection protocol.

sdb9696 - This commit also contains 16 later commits from Simon Wilkinson
squashed into the original
@sdb9696
Update klap changes 2023 to fix encryption, deal with kasa credential…
fb14abd
… switching and work with new discovery changes
@sdb9696
sdb9696 force-pushed the add_klap_protocol branch from fc337e5 to c6eefc3 Compare November 7, 2023 07:09
@sdb9696 sdb9696 mentioned this pull request Nov 7, 2023
Do not do update() in discover_single #542
Merged
rytilahti
rytilahti reviewed Nov 8, 2023
View reviewed changes

@rytilahti rytilahti left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Some more comments. I thought I wrote at some point about cleaning up the session handling to avoid constructing a session object for every _execute_query call and passing it around, but maybe that got lost during a rebase?

The code would become immediately much simpler, if the session would be initialized only when one does not exist (or errors) and stored in self.session to be used. This would follow the same, much more optimized approach like the regular protocol does, i.e., keep the connection to the device open to avoid the set-up overhead. Basically, using a pattern like done in the current protocol: have _connect that performs protocol initialization and implementing close() which resets the state when necessary (e.g., on errors) so that the connect will reinitialize it again.

Maybe that would also simplify the state handling, at the moment there are way, IMO, too many state instance variables which makes following the code quite hard and likely prone to errors. If you want, I could try to give that a go over the weekend and create a PR for you to test on a real device?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread .github/workflows/ci.yml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/smartdevice.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/discover.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@sdb9696
sdb9696 force-pushed the add_klap_protocol branch from c6eefc3 to 1306827 Compare November 8, 2023 19:16
github-advanced-security[bot]
github-advanced-security AI found potential problems Nov 8, 2023
View reviewed changes
Comment thread kasa/klapprotocol.py Fixed

Uh oh!

There was an error while loading. Please reload this page.

@sdb9696
sdb9696 requested a review from rytilahti November 8, 2023 19:30
@sdb9696
sdb9696 force-pushed the add_klap_protocol branch from 1306827 to 0e0e76a Compare November 9, 2023 15:44
github-advanced-security[bot]
github-advanced-security AI found potential problems Nov 9, 2023
View reviewed changes
Comment thread kasa/klapprotocol.py Fixed

Uh oh!

There was an error while loading. Please reload this page.

@sdb9696
Move from aiohttp to httpx
ee900f2
@sdb9696
sdb9696 force-pushed the add_klap_protocol branch from 0e0e76a to ee900f2 Compare November 9, 2023 16:05
@sdb9696

sdb9696 commented Nov 9, 2023

Copy link
Copy Markdown
Contributor Author

Some more comments. I thought I wrote at some point about cleaning up the session handling to avoid constructing a session object for every _execute_query call and passing it around, but maybe that got lost during a rebase?

The code would become immediately much simpler, if the session would be initialized only when one does not exist (or errors) and stored in self.session to be used. This would follow the same, much more optimized approach like the regular protocol does, i.e., keep the connection to the device open to avoid the set-up overhead. Basically, using a pattern like done in the current protocol: have _connect that performs protocol initialization and implementing close() which resets the state when necessary (e.g., on errors) so that the connect will reinitialize it again.

I have now implemented this so that the httpx.AsyncClient is a class instance variable and remains connected until protocol.close() or on error. (There is no need with the AsyncClient to explicitly call connect as it handles it internally.)

Also I realised I had missed a bunch of other comments from you on the klapprotocol class. I have now addressed all of these so I'd like to think we're nearly there :)

rytilahti reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sdb9696

sdb9696 commented Nov 18, 2023

Copy link
Copy Markdown
Contributor Author

Hi @rytilahti I hope you are well. Do you think we could get this closed out soon? I agree with your comments on the HA PR and I’m ready to get that sorted once this is merged.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rytilahti
rytilahti requested changes Nov 18, 2023
View reviewed changes

@rytilahti rytilahti left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Hi @sdb9696 and sorry for the delay, I am currently terribly, terribly busy IRL (and will remain so for the time being) and I haven't had free cycles to think on this project for a while...

Anyway, I went through the current state briefly and I think the PR looks now so much better, thanks! So I feel that we should just merge this after the comments I added now are done, and fix whatever comes up in separate PRs.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread kasa/cli.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/discover.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pyproject.toml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/tests/test_klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/discover.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/protocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/discover.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/discover.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@sdb9696
Changes following review comments
f5d8041
github-advanced-security[bot]
github-advanced-security AI found potential problems Nov 19, 2023
View reviewed changes
Comment thread kasa/klapprotocol.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@sdb9696

sdb9696 commented Nov 19, 2023

Copy link
Copy Markdown
Contributor Author

Ok @rytilahti that's all fully done except for the one item above for the reason given.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rytilahti
rytilahti approved these changes Nov 20, 2023
View reviewed changes

@rytilahti rytilahti left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM, thanks a lot @sdb9696! 💯

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@rytilahti
rytilahti merged commit 30f217b into python-kasa:master Nov 20, 2023
This was referenced Nov 20, 2023
Add support for the new encryption protocol #117
Closed
Implement the new protocol (HTTP over 80/tcp, 20002/udp for discovery) #115
Closed
@rytilahti rytilahti mentioned this pull request Nov 28, 2023
KP 125M / support for matter devices #450
Closed
@sdb9696
sdb9696 deleted the add_klap_protocol branch December 6, 2023 19:34
pillarsdotnet pushed a commit to pillarsdotnet/python-kasa that referenced this pull request Oct 22, 2026
@sdb9696 @SimonWilkinson
Add klap protocol (python-kasa#509)
b303c16
* Add support for the new encryption protocol

This adds support for the new TP-Link discovery and encryption
protocols. It is currently incomplete - only devices without
username and password are current supported, and single device
discovery is not implemented.

Discovery should find both old and new devices. When accessing
a device by IP the --klap option can be specified on the command
line to active the new connection protocol.

sdb9696 - This commit also contains 16 later commits from Simon Wilkinson
squashed into the original

* Update klap changes 2023 to fix encryption, deal with kasa credential switching and work with new discovery changes

* Move from aiohttp to httpx

* Changes following review comments

---------

Co-authored-by: Simon Wilkinson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@bdraco bdraco bdraco left review comments
@rytilahti rytilahti rytilahti approved these changes
+1 more reviewer
@Matheus-Garbelini Matheus-Garbelini Matheus-Garbelini left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

enhancement New feature or request

Projects

None yet

Milestone

0.6.0

Development

Successfully merging this pull request may close these issues.

Implement the new protocol (HTTP over 80/tcp, 20002/udp for discovery)

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@sdb9696 @bdraco @rytilahti @Matheus-Garbelini @github-advanced-security @SimonWilkinson
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.