Skip to content

Use correct state_class for utility meters with device classes that don't support total_increasing#170962

Merged
balloob merged 1 commit into
devfrom
frenck-2026-0558
May 17, 2026
Merged

Use correct state_class for utility meters with device classes that don't support total_increasing#170962
balloob merged 1 commit into
devfrom
frenck-2026-0558

Conversation

@frenck
Copy link
Copy Markdown
Member

@frenck frenck commented May 17, 2026

Breaking change

Proposed change

The utility meter sensor unconditionally uses state_class: total_increasing for non-net-consumption meters. This is invalid for device classes like monetary, which only supports state_class: total. This causes a warning on every state update:

Entity sensor.daily_cost is using state class 'total_increasing' which is
impossible considering device class ('monetary') it is using; expected None
or one of 'total'

The fix looks up DEVICE_CLASS_STATE_CLASSES to check whether the input sensor's device class actually supports total_increasing. If it doesn't, total is used instead. This works generically for any current or future device class constraint, not just monetary.

The existing last_reset handling in extra_state_attributes already supports total state class correctly (it's the same code path used by net_consumption), so no additional changes are needed for long-term statistics.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

…on't support total_increasing

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 17, 2026 10:05
@frenck frenck requested a review from dgomes as a code owner May 17, 2026 10:05
@home-assistant home-assistant Bot added bugfix cla-signed has-tests integration: utility_meter small-pr PRs with less than 30 lines. Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage labels May 17, 2026
@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @dgomes, mind taking a look at this pull request as it has been labeled with an integration (utility_meter) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of utility_meter can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign utility_meter Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

@frenck frenck added this to the 2026.5.3 milestone May 17, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes utility_meter sensor emitting a "state_class 'total_increasing' is impossible for device_class" warning when wrapping sources whose device class (e.g. monetary) does not allow total_increasing. The state_class property now consults DEVICE_CLASS_STATE_CLASSES and falls back to SensorStateClass.TOTAL when TOTAL_INCREASING is not permitted for the input sensor's device class.

Changes:

  • Look up allowed state classes from DEVICE_CLASS_STATE_CLASSES and return TOTAL when TOTAL_INCREASING is not allowed for the source device class.
  • Add a test verifying that a monetary source results in state_class: total on the utility meter.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
homeassistant/components/utility_meter/sensor.py Update state_class property to pick TOTAL when source device class disallows TOTAL_INCREASING.
tests/components/utility_meter/test_sensor.py New test asserting MONETARY source produces SensorStateClass.TOTAL.

Copy link
Copy Markdown
Contributor

@elupus elupus left a comment

Choose a reason for hiding this comment

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

Looks good!

@balloob balloob merged commit 74c918b into dev May 17, 2026
34 checks passed
@balloob balloob deleted the frenck-2026-0558 branch May 17, 2026 14:32
Copilot AI pushed a commit to AlCalzone/homeassistant-core that referenced this pull request May 17, 2026
…on't support total_increasing (home-assistant#170962)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: AlCalzone <17641229+AlCalzone@users.noreply.github.com>
@github-actions github-actions Bot locked and limited conversation to collaborators May 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bugfix cherry-picked cla-signed has-tests integration: utility_meter Quality Scale: internal small-pr PRs with less than 30 lines. Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

device class ('monetary') gives 'total_increasing' error

5 participants