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 device fixture for P316M(US) by TheLinuxGuy · Pull Request #1561 · python-kasa/python-kasa

Conversation

@TheLinuxGuy

@TheLinuxGuy TheLinuxGuy commented Jul 23, 2025

Copy link
Copy Markdown
Contributor

Add P316M support to python-kasa.

Sorry, something went wrong.

Uh oh!

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

All reactions
komodo
Update documentation and add new device fixture for P316M(US)
0d98aa2
@TheLinuxGuy TheLinuxGuy mentioned this pull request Jul 23, 2025
Support for Tapo P316M energy monitoring power strip #1558
Closed
komodo added 3 commits July 23, 2025 22:43
komodo
Update powerprotection.py to handle different key names for enabled s…
18ddad8
…tate
komodo
Fix powerprotection tests to handle both enabled and protection_enabl…
df15a91
…ed keys

The P316M device uses 'protection_enabled' instead of 'enabled' in the
protection power configuration. Update tests to check which key is used
by each device and assert the correct parameters in test calls.
komodo
Fix whitespace.
29c2aa0
@codecov

codecov Bot commented Jul 24, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.66%. Comparing base (e21ab90) to head (a79f27a).
⚠️ Report is 96 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1561   +/-   ##
=======================================
  Coverage   92.66%   92.66%           
=======================================
  Files         150      150           
  Lines        9538     9544    +6     
  Branches      974      976    +2     
=======================================
+ Hits         8838     8844    +6     
  Misses        499      499           
  Partials      201      201           

☔ 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.
All reactions

Sorry, something went wrong.

Uh oh!

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

@TheLinuxGuy

TheLinuxGuy commented Jul 24, 2025

Copy link
Copy Markdown
Contributor Author

I'm a bit stuck and could use some guidance.

Shouldn't this device report its power emeter metrics via CLI? I have tried both these commands and they seem to fail. I don't know what needs to be changed.

uv run kasa --username x --password y --host 192.168.66.240 --debug energy

Error Device has no energy module.

uv run kasa --username x --password y --host 192.168.66.240 --debug usage

Error KeyError: 'usage'
kasa-energy.txt
kasa-usage.txt

All reactions

Sorry, something went wrong.

Uh oh!

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

komodo
Add special handling for P316M powerstrip in
72151f1
energy module to avoid KeyError.
@TheLinuxGuy

TheLinuxGuy commented Jul 24, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

While I think I may have found and fixed the bug about energy - I don't think its the correct fix... because this power strip has 6 outlets with energy information. Which function is the correct one for hardware that has many children of energy?

root@kasapi:~/try2/python-kasa# uv run kasa --username xx --password x --host 192.168.66.240 energy
Discovering device 192.168.66.240 for 10 seconds
== Energy ==
Current: 0.0 A
Voltage: 122.359 V
Power: 0.0 W
Total consumption: None kWh
Today: 0.0 kWh
This month: 0.0 kWh
All reactions

Sorry, something went wrong.

Uh oh!

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

komodo added 2 commits July 23, 2025 23:45
komodo
Fix. kasa/smart/modules/energy.py:168:9: SIM102
48b9262
Use a single `if` statement instead of nested `if` statements
komodo
Fix alignment.
a79f27a
@TheLinuxGuy

TheLinuxGuy commented Aug 4, 2025

Copy link
Copy Markdown
Contributor Author

@rytilahti Hi I know you are busy, do you think you could help share a few pointers on what code may need to be changed to make this device work?

My PR has the fixture files, but IOT powerstrip which has e-meter is not reporting Energy() and usage of each independent power outlet of the powerstrip. I believe this is a new function that may need to be added, perhaps I am wrong and this exists but I do not have any older allegedly supported HS300 power strip to see how it reports emeter data.

All reactions

Sorry, something went wrong.

Uh oh!

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

rytilahti
rytilahti requested changes Aug 10, 2025
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.

Thanks for the PR, @TheLinuxGuy! I added some comments inline, but it is generally looking good. I would suggest that we try to keep this PR as minimum (as it's about adding a fixture) & tackle other issues (like the cli emeter support) in separate PRs.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread kasa/smart/modules/powerprotection.py
"""Return True if child protection is enabled."""
return self.data["get_protection_power"]["enabled"]
gp = self.data["get_protection_power"]
return gp.get("enabled") or gp.get("protection_enabled")

@rytilahti rytilahti Aug 10, 2025

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.

Suggested change
return gp.get("enabled") or gp.get("protection_enabled")
return gp.get("enabled", gp.get("protection_enabled"))

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread kasa/smart/modules/powerprotection.py
Comment on lines 77 to +81
params = {**self.data["get_protection_power"], "enabled": enabled}
# If "enabled" is not used by the device, use "protection_enabled"
if "protection_enabled" in self.data["get_protection_power"]:
params["protection_enabled"] = enabled
params.pop("enabled", None)

@rytilahti rytilahti Aug 10, 2025

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.

Suggested change
params = {**self.data["get_protection_power"], "enabled": enabled}
# If "enabled" is not used by the device, use "protection_enabled"
if "protection_enabled" in self.data["get_protection_power"]:
params["protection_enabled"] = enabled
params.pop("enabled", None)
enabled_key = next(k for k in self.data["get_protection_power"].keys() if "enabled" in k)
params = {**self.data["get_protection_power"], enabled_key: enabled}

How about something like this?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread tests/smart/modules/test_powerprotection.py
if "protection_enabled" in protection_data:
params = {
"protection_enabled": True,
"protection_power": mocker.ANY,

@rytilahti rytilahti Aug 10, 2025

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.

I'd do similar trick here as above in the implementation to make it cleaner.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread tests/smart/modules/test_powerprotection.py
Comment on lines +74 to +82
if "protection_enabled" in protection_data:
params = {
"protection_enabled": True,
"protection_power": int(powerprot._max_power / 2),
}
else:
params = {
"enabled": True,
"protection_power": int(powerprot._max_power / 2),

@rytilahti rytilahti Aug 10, 2025

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.

See my comment above about extracting the key & re-using it to avoid branching here and below.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread kasa/smart/modules/energy.py
Comment on lines 166 to 173
# Energy module is not supported on P304M parent device
# P316M powerstrip does not have 'device_on' key in sys_info.
if (
"device_on" not in self._device.sys_info
and self._device.sys_info.get("model") == "P316M"
):
return True
return "device_on" in self._device.sys_info

@rytilahti rytilahti Aug 10, 2025

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.

So this check is only done to avoid initializing energy module for the main device on P304M as it only reports this information on the child sockets. I think this was an easy solution as the main device cannot be switched on and off, but this check should to my understanding also work for your P316M.

Maybe it's just the cli that needs adjustment for devices that have individual sockets with emeter?
In any case, I would suggest that we get this PR first merged without changes to emeter & tackle that issue in a separate PR.

Sorry, something went wrong.

Uh oh!

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

All reactions
@rytilahti

rytilahti commented Aug 10, 2025

Copy link
Copy Markdown
Member

My PR has the fixture files, but IOT powerstrip which has e-meter is not reporting Energy() and usage of each independent power outlet of the powerstrip. I believe this is a new function that may need to be added, perhaps I am wrong and this exists but I do not have any older allegedly supported HS300 power strip to see how it reports emeter data.

IOT powerstrips are structured very differently, so it does not make much sense to take example for them. The emeter support for devices like P304M and P316M should already be there (using the energy_monitoring component), but the cli tool may need some adjustments to expose this information nicely.

If you execute kasa on the device without a command, does it print the device state for the child sockets? Is the emeter information also shown there?

All reactions

Sorry, something went wrong.

Uh oh!

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

@rytilahti rytilahti added the new device New device supported due to fixture being added label Aug 10, 2025
@rytilahti rytilahti changed the title Update documentation and add new device fixture for P316M(US) Add device fixture for P316M(US) Aug 10, 2025
@TheLinuxGuy TheLinuxGuy mentioned this pull request Aug 24, 2025
Add device fixture for P316M(US) #1568
Merged
@TheLinuxGuy

TheLinuxGuy commented Aug 24, 2025

Copy link
Copy Markdown
Contributor Author

New PR created as requested, with only the fixtures. #1568

rytilahti reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

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

@github-actions

github-actions Bot commented Nov 23, 2025

Copy link
Copy Markdown

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions github-actions Bot added stale and removed stale labels Nov 23, 2025
@github-actions

github-actions Bot commented Apr 14, 2026

Copy link
Copy Markdown

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions github-actions Bot added stale and removed stale labels Apr 14, 2026
@github-actions

github-actions Bot commented Jul 15, 2026

Copy link
Copy Markdown

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions github-actions Bot added stale and removed stale labels Jul 15, 2026
@github-actions

github-actions Bot commented Oct 14, 2026

Copy link
Copy Markdown

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions github-actions Bot added stale and removed stale labels Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@rytilahti rytilahti rytilahti requested changes

Requested changes must be addressed to merge this pull request.

Uh oh!

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

Assignees

No one assigned

Labels

new device New device supported due to fixture being added

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

2 participants

@TheLinuxGuy @rytilahti
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.