Align code between group platforms (#74057)

This commit is contained in:
Erik Montnemery
2022-06-28 08:50:31 +02:00
committed by GitHub
parent fb10853358
commit cc8170fcfe
3 changed files with 19 additions and 21 deletions

View File

@@ -88,6 +88,8 @@ async def async_setup_entry(
class BinarySensorGroup(GroupEntity, BinarySensorEntity):
"""Representation of a BinarySensorGroup."""
_attr_available: bool = False
def __init__(
self,
unique_id: str | None,
@@ -127,27 +129,24 @@ class BinarySensorGroup(GroupEntity, BinarySensorEntity):
@callback
def async_update_group_state(self) -> None:
"""Query all members and determine the binary sensor group state."""
all_states = [self.hass.states.get(x) for x in self._entity_ids]
# filtered_states are members currently in the state machine
filtered_states: list[str] = [x.state for x in all_states if x is not None]
states = [
state.state
for entity_id in self._entity_ids
if (state := self.hass.states.get(entity_id)) is not None
]
# Set group as unavailable if all members are unavailable or missing
self._attr_available = any(
state != STATE_UNAVAILABLE for state in filtered_states
)
self._attr_available = any(state != STATE_UNAVAILABLE for state in states)
valid_state = self.mode(
state not in (STATE_UNKNOWN, STATE_UNAVAILABLE) for state in filtered_states
state not in (STATE_UNKNOWN, STATE_UNAVAILABLE) for state in states
)
if not valid_state:
# Set as unknown if any / all member is not unknown or unavailable
self._attr_is_on = None
else:
# Set as ON if any / all member is ON
states = list(map(lambda x: x == STATE_ON, filtered_states))
state = self.mode(states)
self._attr_is_on = state
self._attr_is_on = self.mode(state == STATE_ON for state in states)
@property
def device_class(self) -> str | None: