Use the converter factory in sensor.recorder._normalize_states (#95785)

We have a factory to create converters now which avoids
the overhead of calling convert to create the converter
every time
This commit is contained in:
J. Nick Koston 2023-07-03 13:20:23 -05:00 committed by GitHub
parent 73f90035bb
commit 3f9d5a0192
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,7 @@
from __future__ import annotations
from collections import defaultdict
from collections.abc import Iterable, MutableMapping
from collections.abc import Callable, Iterable, MutableMapping
import datetime
import itertools
import logging
@ -224,6 +224,8 @@ def _normalize_states(
converter = statistics.STATISTIC_UNIT_TO_UNIT_CONVERTER[statistics_unit]
valid_fstates: list[tuple[float, State]] = []
convert: Callable[[float], float]
last_unit: str | None | object = object()
for fstate, state in fstates:
state_unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
@ -247,15 +249,13 @@ 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)
last_unit = state_unit
valid_fstates.append(
(
converter.convert(
fstate, from_unit=state_unit, to_unit=statistics_unit
),
state,
)
)
valid_fstates.append((convert(fstate), state))
return statistics_unit, valid_fstates