1
mirror of https://github.com/home-assistant/core synced 2024-09-28 03:04:04 +02:00

Reduce overhead to convert history to float states (#109526)

This commit is contained in:
J. Nick Koston 2024-02-04 04:37:14 -06:00 committed by GitHub
parent 9fbf00bdd3
commit 7d9935d24b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -146,31 +146,22 @@ def _equivalent_units(units: set[str | None]) -> bool:
return len(units) == 1
def _parse_float(state: str) -> float:
"""Parse a float string, throw on inf or nan."""
fstate = float(state)
if not math.isfinite(fstate):
raise ValueError
return fstate
def _float_or_none(state: str) -> float | None:
"""Return a float or None."""
try:
return _parse_float(state)
except (ValueError, TypeError):
return None
def _entity_history_to_float_and_state(
entity_history: Iterable[State],
) -> list[tuple[float, State]]:
"""Return a list of (float, state) tuples for the given entity."""
return [
(fstate, state)
for state in entity_history
if (fstate := _float_or_none(state.state)) is not None
]
float_states: list[tuple[float, State]] = []
append = float_states.append
isfinite = math.isfinite
for state in entity_history:
try:
if (float_state := float(state.state)) is not None and isfinite(
float_state
):
append((float_state, state))
except (ValueError, TypeError):
pass
return float_states
def _normalize_states(