From 3db033137850b328eb46ba897bdb5c3c8d5128f1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 4 Feb 2024 08:21:05 -0600 Subject: [PATCH] Avoid converting to same units when compiling stats (#109531) --- homeassistant/components/sensor/recorder.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index 2edd5b0e103..ebf138d39e6 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -222,13 +222,14 @@ def _normalize_states( converter = statistics.STATISTIC_UNIT_TO_UNIT_CONVERTER[statistics_unit] valid_fstates: list[tuple[float, State]] = [] - convert: Callable[[float], float] + convert: Callable[[float], float] | None = None last_unit: str | None | object = object() + valid_units = converter.VALID_UNITS for fstate, state in fstates: state_unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) # Exclude states with unsupported unit from statistics - if state_unit not in converter.VALID_UNITS: + if state_unit not in valid_units: if WARN_UNSUPPORTED_UNIT not in hass.data: hass.data[WARN_UNSUPPORTED_UNIT] = set() if entity_id not in hass.data[WARN_UNSUPPORTED_UNIT]: @@ -247,13 +248,20 @@ def _normalize_states( LINK_DEV_STATISTICS, ) continue + if state_unit != last_unit: # The unit of measurement has changed since the last state change # recreate the converter factory - convert = converter.converter_factory(state_unit, statistics_unit) + if state_unit == statistics_unit: + convert = None + else: + convert = converter.converter_factory(state_unit, statistics_unit) last_unit = state_unit - valid_fstates.append((convert(fstate), state)) + if convert is not None: + fstate = convert(fstate) + + valid_fstates.append((fstate, state)) return statistics_unit, valid_fstates