Skip to content

Fallback to get_current_power if get_energy_usage does not provide current_power #1186

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

Merged
merged 2 commits into from
Oct 25, 2024

Conversation

Fulch36
Copy link
Contributor

@Fulch36 Fulch36 commented Oct 22, 2024

Second PR as requested in #1166

On some newer devices using the "smart" protocol (e.g. P304M), the get_energy_usage response does not include a value for current_power. This means that the value must instead come from the response to an explicit get_current_power request.

@Fulch36 Fulch36 marked this pull request as draft October 22, 2024 20:42
"""Additional check to see if the module is supported by the device."""
# Energy module is not supported on P304M parent device
return not (
self._device.model == "P304M"
Copy link
Member

Choose a reason for hiding this comment

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

I hope we can find a way to avoid hardcoding any models information. If there's no _check_supported at all, does it still work (albeit with an error code for the query)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Completely understand not wanting to use the model here, this was the "least bad" option I could come up with.

If I remove the _check_supported implementation, the result is a warning with an error 1001 from the parent (generic error code?) but the child devices do correctly report their energy usage.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So we have this issue with the KS240 as well which reports the child components at the parent level. The solution after many iterations was to look for a related key in the device_info as part of the _check_supported. The Brightness and Fan modules are examples of this. I'd suggest the device_on could be a suitable key although worth checking the other fixtures to make sure that key is always present when there is a get_energy_usage response.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had a look @sdb9696 and you're absolutely correct, based on the current fixtures then device_on is always present on devices that include energy monitoring. I had only considered fields were directly related to energy monitoring but as of right now, TP-Link/Tapo only seem to offer that on devices that are also switches.

I've tested things locally with your suggested change and all looks good to me! Tests for all existing fixtures continue to pass and querying the P304M from the kasa CLI doesn't throw errors. Many thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed with suggested changes.

Copy link
Member

Choose a reason for hiding this comment

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

Awesome! 🎉 Feel free to remove the fixture and related changes from this PR, and we can merge this followed by the second fixture PR just after that.

@rytilahti rytilahti added the enhancement New feature or request label Oct 23, 2024
@Fulch36 Fulch36 changed the title Draft: Add P304M support Fallback to get_current_power if get_energy_usage does not provide current_power Oct 23, 2024
@rytilahti rytilahti added this to the 0.7.6 milestone Oct 24, 2024
@sdb9696
Copy link
Collaborator

sdb9696 commented Oct 24, 2024

Btw HA beta release is next Wednesday so we should crack on with this if we want to get it in. I’ll help you as much as I can, i.e. guidance with the tests etc.

@sdb9696
Copy link
Collaborator

sdb9696 commented Oct 24, 2024

FYI I’ve merged the latest master into this branch so git pull before doing more work on it.

@Fulch36 Fulch36 force-pushed the feat/add_p304m_support branch from 5ea87e8 to 3dcc9e8 Compare October 25, 2024 17:02
Copy link

codecov bot commented Oct 25, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 92.35%. Comparing base (1e0ca79) to head (b6a32a2).
Report is 217 commits behind head on master.

Files with missing lines Patch % Lines
kasa/smart/modules/energy.py 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1186      +/-   ##
==========================================
- Coverage   92.38%   92.35%   -0.03%     
==========================================
  Files          99       99              
  Lines        6525     6529       +4     
  Branches     1613     1614       +1     
==========================================
+ Hits         6028     6030       +2     
- Misses        378      380       +2     
  Partials      119      119              

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

@Fulch36 Fulch36 marked this pull request as ready for review October 25, 2024 17:08
Copy link
Member

@rytilahti rytilahti left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the PR! The coverage issue will get fixed by itself after we merge the fixture PR, so I think this is ready to be merged.

@sdb9696 sdb9696 merged commit 8b95b7d into python-kasa:master Oct 25, 2024
26 of 28 checks passed
@Fulch36 Fulch36 deleted the feat/add_p304m_support branch October 26, 2024 08:00
@sdb9696 sdb9696 mentioned this pull request Oct 29, 2024
sdb9696 added a commit that referenced this pull request Oct 29, 2024
## [0.7.6](https://github.com/python-kasa/python-kasa/tree/0.7.6) (2024-10-29)

[Full Changelog](0.7.5...0.7.6)

**Release summary:**

- Experimental support for Tapo cameras and the Tapo H200 hub which uses the same protocol.
- Better timestamp support across all devices.
- Support for new devices P304M, S200D and S200B (see README.md for note on the S200 support).
- Various other fixes and minor features.

**Implemented enhancements:**

- Add support for setting the timezone [\#436](#436)
- Add stream\_rtsp\_url to camera module [\#1197](#1197) (@sdb9696)
- Try default logon credentials in SslAesTransport [\#1195](#1195) (@sdb9696)
- Allow enabling experimental devices from environment variable [\#1194](#1194) (@sdb9696)
- Add core device, child and camera modules to smartcamera [\#1193](#1193) (@sdb9696)
- Fallback to get\_current\_power if get\_energy\_usage does not provide current\_power [\#1186](#1186) (@Fulch36)
- Add https parameter to device class factory [\#1184](#1184) (@sdb9696)
- Add discovery list command to cli [\#1183](#1183) (@sdb9696)
- Add Time module to SmartCamera devices [\#1182](#1182) (@sdb9696)
- Add try\_connect\_all to allow initialisation without udp broadcast [\#1171](#1171) (@sdb9696)
- Update dump\_devinfo for smart camera protocol [\#1169](#1169) (@sdb9696)
- Enable newer encrypted discovery protocol [\#1168](#1168) (@sdb9696)
- Initial TapoCamera support [\#1165](#1165) (@sdb9696)
- Add waterleak alert timestamp [\#1162](#1162) (@rytilahti)
- Create common Time module and add time set cli command [\#1157](#1157) (@sdb9696)

**Fixed bugs:**

- Only send 20002 discovery request with key included [\#1207](#1207) (@sdb9696)
- Fix SslAesTransport default login and add tests [\#1202](#1202) (@sdb9696)
- Fix device\_config serialisation of https value [\#1196](#1196) (@sdb9696)

**Added support for devices:**

- Add S200B\(EU\) fw 1.11.0 fixture [\#1205](#1205) (@sdb9696)
- Add TC65 fixture [\#1200](#1200) (@rytilahti)
- Add P304M\(UK\) test fixture [\#1185](#1185) (@Fulch36)
- Add H200 experimental fixture [\#1180](#1180) (@sdb9696)
- Add S200D button fixtures [\#1161](#1161) (@rytilahti)

**Project maintenance:**

- Fix mypy errors in parse_pcap_klap [\#1214](#1214) (@sdb9696)
- Make HSV NamedTuple creation more efficient [\#1211](#1211) (@sdb9696)
- dump\_devinfo: query get\_current\_brt for iot dimmers [\#1209](#1209) (@rytilahti)
- Add trigger\_logs and double\_click to dump\_devinfo helper [\#1208](#1208) (@sdb9696)
- Fix smartcamera childdevice module [\#1206](#1206) (@sdb9696)
- Add H200\(EU\) fw 1.3.2 fixture [\#1204](#1204) (@sdb9696)
- Do not pass None as timeout to http requests [\#1203](#1203) (@sdb9696)
- Update SMART test framework to use fake child protocols [\#1199](#1199) (@sdb9696)
- Allow passing an aiohttp client session during discover try\_connect\_all [\#1198](#1198) (@sdb9696)
- Add test framework for smartcamera [\#1192](#1192) (@sdb9696)
- Rename experimental fixtures folder to smartcamera [\#1191](#1191) (@sdb9696)
- Combine smartcamera error codes into SmartErrorCode [\#1190](#1190) (@sdb9696)
- Allow deriving from SmartModule without being registered [\#1189](#1189) (@sdb9696)
- Improve supported module checks for hub children [\#1188](#1188) (@sdb9696)
- Update smartcamera to support single get/set/do requests [\#1187](#1187) (@sdb9696)
- Add S200B\(US\) fw 1.12.0 fixture [\#1181](#1181) (@sdb9696)
- Add T110\(US\), T310\(US\) and T315\(US\) sensor fixtures [\#1179](#1179) (@sdb9696)
- Enforce EOLs for \*.rst and \*.md [\#1178](#1178) (@rytilahti)
- Convert fixtures to use unix newlines [\#1177](#1177) (@rytilahti)
- Add motion sensor to known categories [\#1176](#1176) (@rytilahti)
- Drop urllib3 dependency and create ssl context in executor thread [\#1175](#1175) (@sdb9696)
- Expose smart child device map as a class constant [\#1173](#1173) (@sdb9696)
rytilahti added a commit that referenced this pull request Nov 11, 2024
The get_energy_usage query may be failing, but we can fallback to use get_current_power like done in #1186
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.

3 participants