mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 10:13:52 -05:00
Apply unit_of_measurement to energy combined power sensor (#170404)
This commit is contained in:
@@ -664,6 +664,12 @@ class EnergyPowerSensor(SensorEntity):
|
||||
self._is_inverted = "stat_rate_inverted" in config
|
||||
self._is_combined = "stat_rate_from" in config and "stat_rate_to" in config
|
||||
|
||||
# Combined mode always emits Watts because _update_state converts
|
||||
# heterogeneous source units to W internally. Inverted mode copies
|
||||
# the source unit in _update_state to track source changes.
|
||||
if self._is_combined:
|
||||
self._attr_native_unit_of_measurement = UnitOfPower.WATT
|
||||
|
||||
# Determine source sensors
|
||||
if self._is_inverted:
|
||||
self._source_sensors = [config["stat_rate_inverted"]]
|
||||
@@ -764,11 +770,6 @@ class EnergyPowerSensor(SensorEntity):
|
||||
# Check first sensor
|
||||
if source_entry := entity_reg.async_get(self._source_sensors[0]):
|
||||
device_id = source_entry.device_id
|
||||
# Combined mode always emits Watts because we convert
|
||||
# heterogeneous source units internally. For inverted mode the
|
||||
# unit is copied from the source state in _update_state.
|
||||
if self._is_combined:
|
||||
self._attr_native_unit_of_measurement = UnitOfPower.WATT
|
||||
# Get source name from registry
|
||||
source_name = source_entry.name or source_entry.original_name
|
||||
# Assign power sensor to same device as source sensor(s)
|
||||
|
||||
@@ -1615,6 +1615,7 @@ async def test_power_sensor_grid_combined(
|
||||
assert state is not None
|
||||
# 500 - 200 = 300 (net import)
|
||||
assert float(state.state) == 300.0
|
||||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfPower.WATT
|
||||
|
||||
|
||||
async def test_power_sensor_device_assignment(
|
||||
@@ -1988,6 +1989,7 @@ async def test_power_sensor_combined_unit_conversion(
|
||||
assert state is not None
|
||||
# 1500 W - 500 W = 1000 W (units are converted to W internally)
|
||||
assert float(state.state) == 1000.0
|
||||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfPower.WATT
|
||||
|
||||
|
||||
async def test_power_sensor_inverted_negative_values(
|
||||
|
||||
Reference in New Issue
Block a user