Enforce MediaPlayerState in group (#78465)

* Enforce MediaPlayerState in group

* Adjust

* Use self._attr_state

* Add warning

* Add group name to logger warning

* Remove logger warning

* Remove duplicate code

* Add type hints to tests

* Improve coverage

* Improve coverage

* suppress ValueError
This commit is contained in:
epenet 2022-11-30 10:13:14 +01:00 committed by GitHub
parent b5aac9a1c7
commit 200f29563a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 17 deletions

View file

@ -1,6 +1,7 @@
"""This platform allows several media players to be grouped into one media player."""
from __future__ import annotations
from contextlib import suppress
from typing import Any
import voluptuous as vol
@ -108,7 +109,6 @@ class MediaPlayerGroup(MediaPlayerEntity):
def __init__(self, unique_id: str | None, name: str, entities: list[str]) -> None:
"""Initialize a Media Group entity."""
self._name = name
self._state: str | None = None
self._attr_unique_id = unique_id
self._entities = entities
@ -206,11 +206,6 @@ class MediaPlayerGroup(MediaPlayerEntity):
"""Return the name of the entity."""
return self._name
@property
def state(self) -> str | None:
"""Return the state of the media group."""
return self._state
@property
def extra_state_attributes(self) -> dict:
"""Return the state attributes for the media group."""
@ -395,15 +390,17 @@ class MediaPlayerGroup(MediaPlayerEntity):
)
if not valid_state:
# Set as unknown if all members are unknown or unavailable
self._state = None
self._attr_state = None
else:
off_values = {MediaPlayerState.OFF, STATE_UNAVAILABLE, STATE_UNKNOWN}
if states.count(states[0]) == len(states):
self._state = states[0]
if states.count(single_state := states[0]) == len(states):
self._attr_state = None
with suppress(ValueError):
self._attr_state = MediaPlayerState(single_state)
elif any(state for state in states if state not in off_values):
self._state = MediaPlayerState.ON
self._attr_state = MediaPlayerState.ON
else:
self._state = MediaPlayerState.OFF
self._attr_state = MediaPlayerState.OFF
supported_features = MediaPlayerEntityFeature(0)
if self._features[KEY_CLEAR_PLAYLIST]: