Copy unit_of_measurement onto energy inverted power sensor#169427
Conversation
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.
|
Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
There was a problem hiding this comment.
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_MEASUREMENTfrom the source state into the inverted power sensor during_update_state. - Keep combined-mode behavior unchanged (still emits
Wdue 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. |
|
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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Looks like we only validate that on stat_rate but not on inverted and combined sensors. They are still filtered on frontend though.
MartinHjelmare
left a comment
There was a problem hiding this comment.
I think this solution looks correct. I don't think the other PR looks correct.
Proposed change
When the Energy dashboard's invert option is enabled on a battery power source, core auto-creates a
sensor.<source>_invertedentity. Today the inverted sensor reads its unit fromRegistryEntry.unit_of_measurement, which only holds user overrides — for almost every integration that field isNone, so the inverted sensor ended up with nounit_of_measurementat all. The Energy graph then mis-scaled the value, and users had to work around it withhomeassistant.customize.This copies the unit from the source state's
ATTR_UNIT_OF_MEASUREMENTinside_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
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: