mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Always update unit of measurement of the utility_meter on state change (#99102)
This commit is contained in:
parent
5e81499855
commit
553cdfbf99
@ -17,6 +17,7 @@ from homeassistant.components.sensor import (
|
|||||||
SensorExtraStoredData,
|
SensorExtraStoredData,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
|
from homeassistant.components.sensor.recorder import _suggest_report_issue
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_UNIT_OF_MEASUREMENT,
|
ATTR_UNIT_OF_MEASUREMENT,
|
||||||
@ -484,6 +485,12 @@ class UtilityMeterSensor(RestoreSensor):
|
|||||||
DATA_TARIFF_SENSORS
|
DATA_TARIFF_SENSORS
|
||||||
]:
|
]:
|
||||||
sensor.start(new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT))
|
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 (
|
if (
|
||||||
adjustment := self.calculate_adjustment(old_state, new_state)
|
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
|
# 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._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._last_valid_state = new_state_val
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
@ -1460,6 +1460,39 @@ def test_calculate_adjustment_invalid_new_state(
|
|||||||
assert "Invalid state unknown" in caplog.text
|
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:
|
async def test_device_id(hass: HomeAssistant) -> None:
|
||||||
"""Test for source entity device for Utility Meter."""
|
"""Test for source entity device for Utility Meter."""
|
||||||
device_registry = dr.async_get(hass)
|
device_registry = dr.async_get(hass)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user