1 """Utility functions to combine state attributes from multiple entities."""
3 from __future__
import annotations
5 from collections.abc
import Callable, Iterator
6 from itertools
import groupby
13 """Find attributes with matching key from states."""
15 if (value := state.attributes.get(key))
is not None:
20 """Find state from states."""
26 """Return the mean of the supplied values."""
27 return int(sum(args) / len(args))
31 """Return the mean values along the columns of the supplied values."""
32 return tuple(sum(x) / len(x)
for x
in zip(*args, strict=
False))
36 """Return True if all attributes found matching key from states are equal.
38 Note: Returns True if no matching attribute is found.
44 """Find attributes with matching key from states."""
46 return max(set(attrs), key=attrs.count)
51 """Return True if all states are equal.
53 Note: Returns True if no matching attribute is found.
59 """Return True if all values are equal.
61 Note: Returns True if no matching attribute is found.
64 return bool(next(grp,
True)
and not next(grp,
False))
70 default: Any |
None =
None,
71 reduce: Callable[..., Any] = mean_int,
73 """Find the first attribute matching key from states.
75 If none are found, return default.
bool attribute_equal(list[State] states, str key)
bool states_equal(list[State] states)
Iterator[Any] find_state_attributes(list[State] states, str key)
tuple[float|Any,...] mean_tuple(*Any args)
Any|None most_frequent_attribute(list[State] states, str key)
bool _values_equal(Iterator[Any] values)
Any reduce_attribute(list[State] states, str key, Any|None default=None, Callable[..., Any] reduce=mean_int)
Iterator[Any] find_state(list[State] states)