FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(smart): add historical energy stats via get_energy_data by floatsmyboat · Pull Request #1735 · python-kasa/python-kasa · GitHub

feat(smart): add historical energy stats via get_energy_data - #1735

Open
floatsmyboat wants to merge 2 commits into
python-kasa:masterfrom
floatsmyboat:feat/smart-energy-history
Open

feat(smart): add historical energy stats via get_energy_data#1735
floatsmyboat wants to merge 2 commits into
python-kasa:masterfrom
floatsmyboat:feat/smart-energy-history

Conversation

Copy link
Copy Markdown

Addresses #1734.

What

Implements historical energy statistics for SMART/KLAP energy-monitoring devices. get_daily_stats and get_monthly_stats were previously stubbed to raise KasaException("Device does not support periodic statistics") for all SMART devices, even though the firmware supports the get_energy_data method the library never called.

They now query get_energy_data and map the result into the existing Energy interface contract ({day: energy} / {month: energy}, honouring kwh).

Details

  • get_monthly_stats(year) — one get_energy_data call aligned to Jan 1 (interval 43200); maps the 12 Wh buckets to {month: energy}.
  • get_daily_stats(year, month) — queries the quarter containing the month (interval 1440) and returns that month's {day: energy}.
  • _query_energy_data() follows the firmware's continuation cursor: when a response's end_timestamp precedes the requested end, it re-queries from there and concatenates the data arrays (the app does the same).
  • PERIODIC_STATS is now advertised for energy-monitoring v2 devices.
  • erase_stats' message corrected — it referenced periodic-stats support it never had anything to do with.

Hardware-verified against a KP125M(US) (fw 1.2.5 Build 241213): pulled a full year of monthly totals plus ~3 months of daily data, and daily-vs-monthly sums cross-check cleanly.

Testing

get_energy_data is parameterized (timestamp window + interval), which the static SMART fixtures can't represent, so FakeSmartTransport now generates a calendar-aligned response for it — the same approach already used for get_on_off_gradually_info. New tests cover the monthly mapping, the daily mapping + kwh scaling, and the continuation-cursor pager; the CLI emeter test now keys off PERIODIC_STATS support instead of IotDevice. Full suite green locally (22750 passed), ruff + mypy clean on changed files.

Open question for maintainers 🚩 (why this is a draft)

Capability detection. I gate PERIODIC_STATS on energy-monitoring v2, but I've only hardware-verified get_energy_data on the KP125M. There are ~15 v2 energy fixtures (P110 / P115 / EP25 / P210M / P304M / P316M / the S515D dimmer …) and I can't confirm the dimmer or the others actually implement get_energy_data.

Options I'd appreciate your steer on:

  1. Keep the v2 gate (assume energy v2 ⇒ get_energy_data) and rely on reports for exceptions.
  2. Narrow to a more specific model/component signal.
  3. Detect at runtime — probe once, or lazily degrade to the existing exception on PARAMS_ERROR / UNKNOWN_METHOD.

Happy to adjust to whichever you prefer. I'm also glad to capture a real get_energy_data fixture from the KP125M if you can point me at the preferred shape for parameterized-call fixtures.


🤖 Generated with Claude Code

Implement get_daily_stats/get_monthly_stats for SMART energy-monitoring
devices, which were previously stubbed to raise. They now call the
firmware's get_energy_data and map the result into the Energy interface
contract ({day/month: energy}, honouring kwh), following the response
continuation cursor for paging. PERIODIC_STATS is advertised for energy
monitoring v2 devices.

Hardware-verified on a KP125M(US). get_energy_data is parameterized, so
FakeSmartTransport generates a calendar-aligned response for it; adds
tests for the monthly/daily mapping and the continuation pager, and
updates the CLI emeter test to key off PERIODIC_STATS support.

Addresses python-kasa#1734.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

codecov Bot commented Aug 5, 2026
edited
Loading

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.35%. Comparing base (a29d061) to head (6d18a49).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1735      +/-   ##
==========================================
+ Coverage   93.29%   93.35%   +0.05%     
==========================================
  Files         157      157              
  Lines        9932     9972      +40     
  Branches     1022     1031       +9     
==========================================
+ Hits         9266     9309      +43     
+ Misses        471      468       -3     
  Partials      195      195              

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

Add tests for the year/month defaults, Q4 quarter boundary, the
get_energy_data continuation pager's no-progress guard and zero-width
window, and erase_stats — bringing patch coverage on the new code to 100%.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
floatsmyboat marked this pull request as ready for review August 6, 2026 16:51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

1 participant


Back | FazBrowse Home | New Git URL