Skip to content

tplink: add support for kasa smart plugs #1693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

threexc
Copy link
Contributor

@threexc threexc commented Jul 11, 2025

Description

  • Extend the tplink resource definition to support smart plug devices like the HS103, in addition to the existing IotStrip functionality.
  • Since these only have one port, make the index argument for NetworkPowerPort optional and default to 0 if not provided.
  • Also update the docs to indicate that index is now optional.

Tested locally using both a KP303 strip (already supported) and an HS103. Here's the latter's kasa discover data:

(labgrid-venv) tgamblin@ecogrid:~/labgrid$ kasa discover                                                                                                                                           
Discovering devices on 255.255.255.255 for 10 seconds                                            
== workspace - HS103 ==                         
Host: 192.168.40.218                            
Port: 9999                                      
Device state: True                                                                               
Time:         2025-07-11 13:26:06-05:00 (tz: EST)
Hardware:     5.0 (US)                
Firmware:     1.0.13 Build 240117 Rel.162355                                                     
MAC (rssi):   98:25:4A:08:63:35 (-60)           

== Primary features ==
State (state): True

== Information ==
On since (on_since): 2025-07-11 13:13:31-05:00
Cloud connection (cloud_connection): True

== Configuration ==
LED (led): True                                                                                                                                                                                    

== Debug ==
RSSI (rssi): -60 dBm
Reboot (reboot): <Action>

And here is the sample exporter config:

thinkcentre:                                                                                                                                                                                       
  location: ecogrid                                                                                                                                                                                
  thinkcentre-kasa-plug:                                                                                                                                                                           
    cls: 'NetworkPowerPort'                                                                                                                                                                        
    model: 'tplink'                                                                                                                                                                                
    host: '192.168.40.218'

Checklist

  • Documentation for the feature
  • Tests for the feature - I think the existing tests may be adequate, but review would be good.
  • The arguments and description in doc/configuration.rst have been updated
  • PR has been tested

@threexc threexc force-pushed the tgamblin/kasa-driver branch from e17fbb1 to 4edf37e Compare July 11, 2025 17:38
@threexc
Copy link
Contributor Author

threexc commented Jul 11, 2025

Pushed an update because I realized that the doc update didn't clarify that tplink now works for smart plugs too.

Copy link

codecov bot commented Jul 11, 2025

Codecov Report

❌ Patch coverage is 14.81481% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 55.6%. Comparing base (0ba0f75) to head (64eef0a).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
labgrid/driver/power/tplink.py 11.5% 23 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master   #1693     +/-   ##
========================================
- Coverage    55.6%   55.6%   -0.1%     
========================================
  Files         172     172             
  Lines       13462   13476     +14     
========================================
+ Hits         7491    7493      +2     
- Misses       5971    5983     +12     
Flag Coverage Δ
3.10 55.5% <14.8%> (-0.1%) ⬇️
3.11 55.5% <14.8%> (-0.1%) ⬇️
3.12 55.5% <14.8%> (-0.1%) ⬇️
3.13 55.5% <14.8%> (-0.1%) ⬇️
3.9 55.6% <14.8%> (-0.1%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@threexc
Copy link
Contributor Author

threexc commented Jul 11, 2025

Added a new test case in TestNetworkPowerDriver to test the index and make sure that the NetworkPowerDriver object still gets created OK if no index is provided.

@threexc threexc force-pushed the tgamblin/kasa-driver branch 3 times, most recently from 5c21895 to a48b9a0 Compare July 16, 2025 14:49
@threexc threexc force-pushed the tgamblin/kasa-driver branch from a48b9a0 to d945598 Compare July 21, 2025 16:53
@threexc threexc force-pushed the tgamblin/kasa-driver branch 2 times, most recently from 15d1f49 to e78a072 Compare July 22, 2025 13:21
Copy link
Contributor

@sjg20 sjg20 left a comment

Choose a reason for hiding this comment

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

I'm not sure what can be done about testing, but otherwise this LGTM

@threexc threexc force-pushed the tgamblin/kasa-driver branch 3 times, most recently from 25fd909 to ff6fd02 Compare July 28, 2025 16:05
@threexc
Copy link
Contributor Author

threexc commented Jul 28, 2025

A few rebases recently, no changes to the logic.

- differentiate between 'strip' and 'plug' devices, using simpler logic
  for controlling power on the latter
- create new _power_get() async function to match _power_set()

Signed-off-by: Trevor Gamblin <tgamblin@baylibre.com>
Smart plugs and similar single-port network-controlled devices shouldn't
require the exporter config to include the 'index' argument. Make it
optional by including a default of 0 in NetworkPowerPort.

Signed-off-by: Trevor Gamblin <tgamblin@baylibre.com>
- Document that 'index' is now optional
- Change 'tplink' section to indicate it controls both smart plugs and
  power strips

Signed-off-by: Trevor Gamblin <tgamblin@baylibre.com>
This is similar test_create, but it doesn't pass an index value, testing
that the default index of the NetworkPowerPort object is '0' and also
that the NetworkPowerDriver can still be instantiated correctly.

Signed-off-by: Trevor Gamblin <tgamblin@baylibre.com>
@threexc threexc force-pushed the tgamblin/kasa-driver branch from ff6fd02 to 64eef0a Compare August 11, 2025 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants