mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Align code between group platforms (#74057)
This commit is contained in:
parent
fb10853358
commit
cc8170fcfe
@ -88,6 +88,8 @@ async def async_setup_entry(
|
|||||||
class BinarySensorGroup(GroupEntity, BinarySensorEntity):
|
class BinarySensorGroup(GroupEntity, BinarySensorEntity):
|
||||||
"""Representation of a BinarySensorGroup."""
|
"""Representation of a BinarySensorGroup."""
|
||||||
|
|
||||||
|
_attr_available: bool = False
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
unique_id: str | None,
|
unique_id: str | None,
|
||||||
@ -127,27 +129,24 @@ class BinarySensorGroup(GroupEntity, BinarySensorEntity):
|
|||||||
@callback
|
@callback
|
||||||
def async_update_group_state(self) -> None:
|
def async_update_group_state(self) -> None:
|
||||||
"""Query all members and determine the binary sensor group state."""
|
"""Query all members and determine the binary sensor group state."""
|
||||||
all_states = [self.hass.states.get(x) for x in self._entity_ids]
|
states = [
|
||||||
|
state.state
|
||||||
# filtered_states are members currently in the state machine
|
for entity_id in self._entity_ids
|
||||||
filtered_states: list[str] = [x.state for x in all_states if x is not None]
|
if (state := self.hass.states.get(entity_id)) is not None
|
||||||
|
]
|
||||||
|
|
||||||
# Set group as unavailable if all members are unavailable or missing
|
# Set group as unavailable if all members are unavailable or missing
|
||||||
self._attr_available = any(
|
self._attr_available = any(state != STATE_UNAVAILABLE for state in states)
|
||||||
state != STATE_UNAVAILABLE for state in filtered_states
|
|
||||||
)
|
|
||||||
|
|
||||||
valid_state = self.mode(
|
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:
|
if not valid_state:
|
||||||
# Set as unknown if any / all member is not unknown or unavailable
|
# Set as unknown if any / all member is not unknown or unavailable
|
||||||
self._attr_is_on = None
|
self._attr_is_on = None
|
||||||
else:
|
else:
|
||||||
# Set as ON if any / all member is ON
|
# Set as ON if any / all member is ON
|
||||||
states = list(map(lambda x: x == STATE_ON, filtered_states))
|
self._attr_is_on = self.mode(state == STATE_ON for state in states)
|
||||||
state = self.mode(states)
|
|
||||||
self._attr_is_on = state
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self) -> str | None:
|
def device_class(self) -> str | None:
|
||||||
|
@ -46,7 +46,7 @@ from homeassistant.const import (
|
|||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Event, HomeAssistant, State, callback
|
from homeassistant.core import Event, HomeAssistant, callback
|
||||||
from homeassistant.helpers import config_validation as cv, entity_registry as er
|
from homeassistant.helpers import config_validation as cv, entity_registry as er
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.event import async_track_state_change_event
|
from homeassistant.helpers.event import async_track_state_change_event
|
||||||
@ -214,15 +214,15 @@ class LightGroup(GroupEntity, LightEntity):
|
|||||||
@callback
|
@callback
|
||||||
def async_update_group_state(self) -> None:
|
def async_update_group_state(self) -> None:
|
||||||
"""Query all members and determine the light group state."""
|
"""Query all members and determine the light group state."""
|
||||||
all_states = [self.hass.states.get(x) for x in self._entity_ids]
|
states = [
|
||||||
states: list[State] = list(filter(None, all_states))
|
state
|
||||||
|
for entity_id in self._entity_ids
|
||||||
|
if (state := self.hass.states.get(entity_id)) is not None
|
||||||
|
]
|
||||||
on_states = [state for state in states if state.state == STATE_ON]
|
on_states = [state for state in states if state.state == STATE_ON]
|
||||||
|
|
||||||
# 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]
|
|
||||||
|
|
||||||
valid_state = self.mode(
|
valid_state = self.mode(
|
||||||
state not in (STATE_UNKNOWN, STATE_UNAVAILABLE) for state in filtered_states
|
state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE) for state in states
|
||||||
)
|
)
|
||||||
|
|
||||||
if not valid_state:
|
if not valid_state:
|
||||||
@ -230,9 +230,7 @@ class LightGroup(GroupEntity, LightEntity):
|
|||||||
self._attr_is_on = None
|
self._attr_is_on = None
|
||||||
else:
|
else:
|
||||||
# Set as ON if any / all member is ON
|
# Set as ON if any / all member is ON
|
||||||
self._attr_is_on = self.mode(
|
self._attr_is_on = self.mode(state.state == STATE_ON for state in states)
|
||||||
list(map(lambda x: x == STATE_ON, filtered_states))
|
|
||||||
)
|
|
||||||
|
|
||||||
self._attr_available = any(state.state != STATE_UNAVAILABLE for state in states)
|
self._attr_available = any(state.state != STATE_UNAVAILABLE for state in states)
|
||||||
self._attr_brightness = reduce_attribute(on_states, ATTR_BRIGHTNESS)
|
self._attr_brightness = reduce_attribute(on_states, ATTR_BRIGHTNESS)
|
||||||
|
@ -170,4 +170,5 @@ class SwitchGroup(GroupEntity, SwitchEntity):
|
|||||||
# Set as ON if any / all member is ON
|
# Set as ON if any / all member is ON
|
||||||
self._attr_is_on = self.mode(state == STATE_ON for state in states)
|
self._attr_is_on = self.mode(state == STATE_ON for state in states)
|
||||||
|
|
||||||
|
# Set group as unavailable if all members are unavailable or missing
|
||||||
self._attr_available = any(state != STATE_UNAVAILABLE for state in states)
|
self._attr_available = any(state != STATE_UNAVAILABLE for state in states)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user