Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions homeassistant/components/utility_meter/sensor.py
Expand Up @@ -17,6 +17,7 @@
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,
Expand Down Expand Up @@ -484,13 +485,20 @@ def async_reading(self, event: EventType[EventStateChangedData]) -> None:
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)
) is not None and (self._sensor_net_consumption or adjustment >= 0):
# 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)
bdraco marked this conversation as resolved.
Show resolved Hide resolved
self._last_valid_state = new_state_val
self.async_write_ha_state()

Expand Down
33 changes: 33 additions & 0 deletions tests/components/utility_meter/test_sensor.py
Expand Up @@ -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)
Expand Down