labgrid icon indicating copy to clipboard operation
labgrid copied to clipboard

tplink: add support for kasa smart plugs

Open threexc opened this issue 8 months ago • 12 comments

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

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

threexc avatar Jul 11 '25 17:07 threexc

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

threexc avatar Jul 11 '25 17:07 threexc

Codecov Report

:x: Patch coverage is 13.79310% with 25 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 45.2%. Comparing base (38d41d5) to head (fcae346). :white_check_mark: All tests successful. No failed tests found.

Files with missing lines Patch % Lines
labgrid/driver/power/tplink.py 10.7% 25 Missing :warning:
Additional details and impacted files
@@           Coverage Diff            @@
##           master   #1693     +/-   ##
========================================
- Coverage    45.2%   45.2%   -0.1%     
========================================
  Files         174     174             
  Lines       13747   13763     +16     
========================================
+ Hits         6221    6223      +2     
- Misses       7526    7540     +14     
Flag Coverage Δ
3.10 45.2% <13.7%> (-0.1%) :arrow_down:
3.11 45.2% <13.7%> (-0.1%) :arrow_down:
3.12 45.2% <13.7%> (-0.1%) :arrow_down:
3.13 45.1% <13.7%> (-0.1%) :arrow_down:
3.14 45.1% <13.7%> (-0.1%) :arrow_down:
3.9 45.2% <13.7%> (-0.1%) :arrow_down:

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

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Jul 11 '25 17:07 codecov[bot]

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 avatar Jul 11 '25 18:07 threexc

A few rebases recently, no changes to the logic.

threexc avatar Jul 28 '25 16:07 threexc

@Bastian-Krause @jluebbe anything holding this one back?

threexc avatar Aug 22 '25 19:08 threexc

Just rebasing again, no new additions.

threexc avatar Sep 15 '25 01:09 threexc

Rebased again.

threexc avatar Sep 22 '25 16:09 threexc

Rebased again.

threexc avatar Oct 10 '25 18:10 threexc

Just another rebase to keep the branch current.

threexc avatar Oct 21 '25 13:10 threexc

Last force push was just a rebase on master.

threexc avatar Nov 24 '25 16:11 threexc

I took a look and it looked reasonable. Can you rebase and test again?

jluebbe avatar Dec 02 '25 16:12 jluebbe

I took a look and it looked reasonable. Can you rebase and test again?

Sorry for the wait. Fixed it up and did another test with my HS103 smart plug just now. I had to fix the default for the index parameter so it was provided as a string instead of int.

threexc avatar Dec 09 '25 19:12 threexc