Skip to content

Copy unit_of_measurement onto energy inverted power sensor#169427

Merged
MartinHjelmare merged 1 commit into
home-assistant:devfrom
MindFreeze:energy-inverted-power-sensor-unit
Apr 29, 2026
Merged

Copy unit_of_measurement onto energy inverted power sensor#169427
MartinHjelmare merged 1 commit into
home-assistant:devfrom
MindFreeze:energy-inverted-power-sensor-unit

Conversation

@MindFreeze
Copy link
Copy Markdown
Member

@MindFreeze MindFreeze commented Apr 29, 2026

Proposed change

When the Energy dashboard's invert option is enabled on a battery power source, core auto-creates a sensor.<source>_inverted entity. Today the inverted sensor reads its unit from RegistryEntry.unit_of_measurement, which only holds user overrides — for almost every integration that field is None, so the inverted sensor ended up with no unit_of_measurement at all. The Energy graph then mis-scaled the value, and users had to work around it with homeassistant.customize.

This copies the unit from the source state's ATTR_UNIT_OF_MEASUREMENT inside _update_state, so it works regardless of whether the source is registry-customized and tracks unit changes on the source. Combined-mode behaviour is unchanged (still pinned to W, since it converts heterogeneous source units internally).

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:

The inverted sensor used to read the unit from the source's entity
registry entry, which only stores user overrides, so in the common case
the inverted sensor was created without a unit at all. Read it from the
source state attributes instead, in _update_state, so unit changes track
the source.
Copilot AI review requested due to automatic review settings April 29, 2026 06:53
@home-assistant home-assistant Bot added bugfix cla-signed has-tests integration: energy 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 Top 50 Integration is ranked within the top 50 by usage Quality Scale: internal labels Apr 29, 2026
@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (energy) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of energy 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 energy 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.

@MindFreeze MindFreeze marked this pull request as ready for review April 29, 2026 06:53
@MindFreeze MindFreeze requested a review from a team as a code owner April 29, 2026 06:53
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 the Energy dashboard’s auto-created inverted battery power sensors missing unit_of_measurement by copying the unit from the source entity’s state attributes, ensuring correct graph scaling without relying on entity registry overrides.

Changes:

  • Copy ATTR_UNIT_OF_MEASUREMENT from the source state into the inverted power sensor during _update_state.
  • Keep combined-mode behavior unchanged (still emits W due to internal conversion).
  • Add tests to verify unit propagation, unit changes, and the no-unit case.

Reviewed changes

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

File Description
homeassistant/components/energy/sensor.py Copies unit from the source state for inverted sensors; clarifies combined-mode unit behavior.
tests/components/energy/test_sensor.py Adds assertions and new tests to validate inverted sensor unit propagation behavior.

@frenck frenck added this to the 2026.5.0 milestone Apr 29, 2026
@epenet epenet modified the milestones: 2026.5.0, 2026.5.0b0 Apr 29, 2026
@MartinHjelmare
Copy link
Copy Markdown
Member

Is this solving the same problem as #168910 is trying to solve?

@MindFreeze
Copy link
Copy Markdown
Member Author

Is this solving the same problem as #168910 is trying to solve?

Looks like it. I hadn't seen that one.

manager = await async_get_manager(hass)
manager.data = manager.default_preferences()

hass.states.async_set("sensor.battery_power", "100.0")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Side note: It's weird that we allow sources without unit of measurement as that source doesn't mean anything when we're tracking a specific quantity like battery usage.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks like we only validate that on stat_rate but not on inverted and combined sensors. They are still filtered on frontend though.

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

I think this solution looks correct. I don't think the other PR looks correct.

@MartinHjelmare MartinHjelmare removed this from the 2026.5.0 milestone Apr 29, 2026
@MartinHjelmare MartinHjelmare merged commit 284242b into home-assistant:dev Apr 29, 2026
52 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bugfix cla-signed has-tests integration: energy Quality Scale: internal small-pr PRs with less than 30 lines. Top 50 Integration is ranked within the top 50 by usage 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.

5 participants