Skip to content

Apply the metering unit scale and fix cubic-feet demand units - #1063

Draft
zigpy-review-bot wants to merge 1 commit into
devfrom
zigpy-bot/audit/apply-metering-unit-scale
Draft

zigpy-review-bot wants to merge 1 commit into
devfrom
zigpy-bot/audit/apply-metering-unit-scale

Conversation

@zigpy-review-bot

Copy link
Copy Markdown
Collaborator

Summary

Severity: medium (the audit's estimate)

Smart Energy meters that report in ccf (unit_of_measure 0x03) or mcf (0x0A) show their summation in cubic feet 100 or 1000 times too small (and the mcf demand in ft³/h 1000 times too small), because the scale the metering entity descriptions carry for those units is never applied. Home Assistant applied it in native_value before ZHA moved into this library; #6 dropped it. The demand units are also wrong: 0x02 (ft³/h per Smart Energy 1.4a, Table D-26) is labelled ft³/min, and 0x03 (ccf/h) shows the unconverted number as m³/h.

Changes

  • SmartEnergyMetering gets a _scale property (the entity description's scale, 1 when the unit has no description), and both metering formatters multiply by it after multiplier and divisor, before the number formatting.
  • The demand units of 0x02 and 0x03 become ft³/h, like 0x0A already is; 0x03 keeps its scale of 100 (ccf/h to ft³/h). The device class stays unset for all three.
  • Tests: the two ccf cases of test_se_summation_uom now expect cubic feet (23.4 and 23.6 instead of 0.23 and 0.24), an mcf case is added, and the new test_se_demand_uom covers the demand of 0x02, 0x03 and 0x0A.

Breaking changes

For existing ccf and mcf meters the summation jumps once to the correct value (100 or 1000 times the old one); it is a total-increasing sensor, so the jump is recorded as consumption in the long-term statistics and the energy dashboard. The demand unit change on 0x02 and 0x03 makes Home Assistant ask to fix the statistics unit.

Open items

#1009 edits the same 0x02 and 0x03 demand entries (it adds the volume flow rate device class); whichever merges second needs a small rebase there, and the unit choice for those two entries should be settled with it.

Testing

  • test_se_summation_uom (three cases) and test_se_demand_uom (three cases) fail at 2da1a5b and pass with the fix.
  • Full suite: 1785 passed. pre-commit (codespell, ruff, ruff format, mypy) passes. No device diagnostics in tests/data/devices/ use these units, so no snapshot changes.

Opened by the scheduled code audit; not requested by a maintainer.

Fixes #1062

The Metering entity descriptions carry a `scale` (100 for ccf, 1000 for
mcf) that converts the meter's unit to the cubic feet the entity shows.
It was applied in Home Assistant's `native_value` before ZHA became a
library, and was lost in the move, so ccf and mcf meters showed 100x and
1000x too little.

Apply it again in both metering formatters, before the number
formatting. Also label the demand of unit 0x02 (ft³/h per Smart Energy)
and 0x03 (ccf/h, scaled to ft³/h) as ft³/h, like 0x0A already is,
instead of ft³/min and m³/h.
@zigpy-review-bot zigpy-review-bot added bot: code audit Found by the bot's scheduled code audit bot: unrequested PR PR: opened by the bot's issue triage, not requested by a maintainer bugfix This PR fixes a bug bot-watch: fix zigpy-review-bot follows this thread and also pushes fixes (review + fix) labels Nov 23, 2026
@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.48%. Comparing base (2da1a5b) to head (75b632e).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #1063   +/-   ##
=======================================
  Coverage   97.48%   97.48%           
=======================================
  Files          61       61           
  Lines       10870    10874    +4     
=======================================
+ Hits        10597    10601    +4     
  Misses        273      273           

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

@zigpy-review-bot

Copy link
Copy Markdown
Collaborator Author

This PR now conflicts with dev: #1009 (merged in e13552b) changed the same two demand entries, 0x02 and 0x03, in zha/application/platforms/sensor/__init__.py. tests/test_sensor.py merges without a conflict, but its tests from both PRs assert different units for these two entries. I haven't merged dev into this branch because the conflict needs a decision on the units, not just a mechanical fix.

What conflicts

You can't just take one side. Keeping dev's 0x03 entry with this PR's scale fix would show a ccf/h demand ×100 (that is, ft³/h) labelled m³/h, about 35 times too high. Keeping this PR's entries drops the device class that #1009 just added, because Home Assistant's UnitOfVolumeFlowRate has no ft³/h unit (it has ft³/min and m³/h).

Options

  1. Keep dev's units and device class, and fix the values with the scale. 0x02 stays ft³/min with a scale of 1/60. 0x03 stays m³/h with a scale of 2.8316846592 (100 ft³ in m³). This needs SmartEnergyMeteringEntityDescription.scale and _scale to accept a float (they are int today).
  2. Keep this PR's ft³/h for 0x02 and 0x03 and remove the device class from those two entries only.

Option 1 keeps what #1009 added and still corrects the values, so I'd go with it. Whichever option you pick, I'll merge dev into this branch, resolve the conflict that way, update the tests from both PRs to match, and update the PR body.

@TheJulianJES, which option should I use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot: code audit Found by the bot's scheduled code audit bot: unrequested PR PR: opened by the bot's issue triage, not requested by a maintainer bot-watch: fix zigpy-review-bot follows this thread and also pushes fixes (review + fix) bugfix This PR fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ccf and mcf metering sensors show 100 or 1000 times too little, and cubic-feet demand units are wrong

1 participant