Always update unit of measurement of the utility_meter on state change (#99102)

This commit is contained in:
Diogo Gomes 2023-09-10 14:29:38 +01:00 committed by GitHub
parent 5e81499855
commit 553cdfbf99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 0 deletions

View File

@ -17,6 +17,7 @@ from homeassistant.components.sensor import (
SensorExtraStoredData,
SensorStateClass,
)
from homeassistant.components.sensor.recorder import _suggest_report_issue
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
@ -484,6 +485,12 @@ class UtilityMeterSensor(RestoreSensor):
DATA_TARIFF_SENSORS
]:
sensor.start(new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT))
if self._unit_of_measurement is None:
_LOGGER.warning(
"Source sensor %s has no unit of measurement. Please %s",
self._sensor_source_id,
_suggest_report_issue(self.hass, self._sensor_source_id),
)
if (
adjustment := self.calculate_adjustment(old_state, new_state)
@ -491,6 +498,7 @@ class UtilityMeterSensor(RestoreSensor):
# If net_consumption is off, the adjustment must be non-negative
self._state += adjustment # type: ignore[operator] # self._state will be set to by the start function if it is None, therefore it always has a valid Decimal value at this line
self._unit_of_measurement = new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
self._last_valid_state = new_state_val
self.async_write_ha_state()

View File

@ -1460,6 +1460,39 @@ def test_calculate_adjustment_invalid_new_state(
assert "Invalid state unknown" in caplog.text
async def test_unit_of_measurement_missing_invalid_new_state(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that a suggestion is created when new_state is missing unit_of_measurement."""
yaml_config = {
"utility_meter": {
"energy_bill": {
"source": "sensor.energy",
}
}
}
source_entity_id = yaml_config[DOMAIN]["energy_bill"]["source"]
assert await async_setup_component(hass, DOMAIN, yaml_config)
await hass.async_block_till_done()
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
hass.states.async_set(source_entity_id, 4, {ATTR_UNIT_OF_MEASUREMENT: None})
await hass.async_block_till_done()
state = hass.states.get("sensor.energy_bill")
assert state is not None
assert state.state == "0"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
assert (
f"Source sensor {source_entity_id} has no unit of measurement." in caplog.text
)
async def test_device_id(hass: HomeAssistant) -> None:
"""Test for source entity device for Utility Meter."""
device_registry = dr.async_get(hass)