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:
parent
73f90035bb
commit
3f9d5a0192
1 changed files with 9 additions and 9 deletions
|
@ -2,7 +2,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from collections.abc import Iterable, MutableMapping
|
from collections.abc import Callable, Iterable, MutableMapping
|
||||||
import datetime
|
import datetime
|
||||||
import itertools
|
import itertools
|
||||||
import logging
|
import logging
|
||||||
|
@ -224,6 +224,8 @@ def _normalize_states(
|
||||||
|
|
||||||
converter = statistics.STATISTIC_UNIT_TO_UNIT_CONVERTER[statistics_unit]
|
converter = statistics.STATISTIC_UNIT_TO_UNIT_CONVERTER[statistics_unit]
|
||||||
valid_fstates: list[tuple[float, State]] = []
|
valid_fstates: list[tuple[float, State]] = []
|
||||||
|
convert: Callable[[float], float]
|
||||||
|
last_unit: str | None | object = object()
|
||||||
|
|
||||||
for fstate, state in fstates:
|
for fstate, state in fstates:
|
||||||
state_unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
state_unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||||
|
@ -247,15 +249,13 @@ def _normalize_states(
|
||||||
LINK_DEV_STATISTICS,
|
LINK_DEV_STATISTICS,
|
||||||
)
|
)
|
||||||
continue
|
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(
|
valid_fstates.append((convert(fstate), state))
|
||||||
(
|
|
||||||
converter.convert(
|
|
||||||
fstate, from_unit=state_unit, to_unit=statistics_unit
|
|
||||||
),
|
|
||||||
state,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
return statistics_unit, valid_fstates
|
return statistics_unit, valid_fstates
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue