mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Reduce overhead to convert history to float states (#109526)
This commit is contained in:
parent
9fbf00bdd3
commit
7d9935d24b
@ -146,31 +146,22 @@ def _equivalent_units(units: set[str | None]) -> bool:
|
|||||||
return len(units) == 1
|
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(
|
def _entity_history_to_float_and_state(
|
||||||
entity_history: Iterable[State],
|
entity_history: Iterable[State],
|
||||||
) -> list[tuple[float, State]]:
|
) -> list[tuple[float, State]]:
|
||||||
"""Return a list of (float, state) tuples for the given entity."""
|
"""Return a list of (float, state) tuples for the given entity."""
|
||||||
return [
|
float_states: list[tuple[float, State]] = []
|
||||||
(fstate, state)
|
append = float_states.append
|
||||||
for state in entity_history
|
isfinite = math.isfinite
|
||||||
if (fstate := _float_or_none(state.state)) is not None
|
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(
|
def _normalize_states(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user