8000 Align code between group platforms by emontnemery · Pull Request #74057 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Align code between group platforms #74057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions homeassistant/components/group/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -127,27 +129,24 @@ def async_state_changed_listener(event: Event) -> None:
@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:
Expand Down
18 changes: 8 additions & 10 deletions homeassistant/components/group/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
STATE_UNAVAILABLE,
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.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_state_change_event
Expand Down Expand Up @@ -214,25 +214,23 @@ async def async_turn_off(self, **kwargs: Any) -> None:
@callback
def async_update_group_state(self) -> None:
"""Query all members and determine the light group state."""
all_states = [self.hass.states.get(x) for x in self._entity_ids]
states: list[State] = list(filter(None, all_states))
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]

# 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(
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:
# Set as unknown if any / all member is unknown or unavailable
self._attr_is_on = None
else:
# Set as ON if any / all member is ON
self._attr_is_on = self.mode(
list(map(lambda x: x == STATE_ON, filtered_states))
)
self._attr_is_on = self.mode(state.state == STATE_ON 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)
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/group/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,5 @@ def async_update_group_state(self) -> None:
# Set as ON if any / all member is ON
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)
0