mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 07:07:28 +00:00
Introduce new MediaPlayerState StrEnum (#77941)
* Adjust media-player checks in pylint plugin * Adjust media-player definitions * Adjust cast signatures * Adjust play_media signature * Introduce MediaPlayerState * Fix cast implementations * Revert cast changes * Update hass_enforce_type_hints.py * Use set Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> * Fix tests * Keep unused constants * Fix test * Revert tests Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
This commit is contained in:
parent
951047d94e
commit
645f5e5ac1
@ -30,7 +30,7 @@ from homeassistant.components.websocket_api.const import (
|
|||||||
ERR_UNKNOWN_ERROR,
|
ERR_UNKNOWN_ERROR,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import ( # noqa: F401
|
||||||
SERVICE_MEDIA_NEXT_TRACK,
|
SERVICE_MEDIA_NEXT_TRACK,
|
||||||
SERVICE_MEDIA_PAUSE,
|
SERVICE_MEDIA_PAUSE,
|
||||||
SERVICE_MEDIA_PLAY,
|
SERVICE_MEDIA_PLAY,
|
||||||
@ -128,6 +128,8 @@ from .const import ( # noqa: F401
|
|||||||
SUPPORT_VOLUME_SET,
|
SUPPORT_VOLUME_SET,
|
||||||
SUPPORT_VOLUME_STEP,
|
SUPPORT_VOLUME_STEP,
|
||||||
MediaPlayerEntityFeature,
|
MediaPlayerEntityFeature,
|
||||||
|
MediaPlayerState,
|
||||||
|
MediaType,
|
||||||
RepeatMode,
|
RepeatMode,
|
||||||
)
|
)
|
||||||
from .errors import BrowseError
|
from .errors import BrowseError
|
||||||
@ -226,7 +228,8 @@ def is_on(hass, entity_id=None):
|
|||||||
"""
|
"""
|
||||||
entity_ids = [entity_id] if entity_id else hass.states.entity_ids(DOMAIN)
|
entity_ids = [entity_id] if entity_id else hass.states.entity_ids(DOMAIN)
|
||||||
return any(
|
return any(
|
||||||
not hass.states.is_state(entity_id, STATE_OFF) for entity_id in entity_ids
|
not hass.states.is_state(entity_id, MediaPlayerState.OFF)
|
||||||
|
for entity_id in entity_ids
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -454,7 +457,7 @@ class MediaPlayerEntity(Entity):
|
|||||||
_attr_media_artist: str | None = None
|
_attr_media_artist: str | None = None
|
||||||
_attr_media_channel: str | None = None
|
_attr_media_channel: str | None = None
|
||||||
_attr_media_content_id: str | None = None
|
_attr_media_content_id: str | None = None
|
||||||
_attr_media_content_type: str | None = None
|
_attr_media_content_type: MediaType | str | None = None
|
||||||
_attr_media_duration: int | None = None
|
_attr_media_duration: int | None = None
|
||||||
_attr_media_episode: str | None = None
|
_attr_media_episode: str | None = None
|
||||||
_attr_media_image_hash: str | None
|
_attr_media_image_hash: str | None
|
||||||
@ -467,13 +470,13 @@ class MediaPlayerEntity(Entity):
|
|||||||
_attr_media_series_title: str | None = None
|
_attr_media_series_title: str | None = None
|
||||||
_attr_media_title: str | None = None
|
_attr_media_title: str | None = None
|
||||||
_attr_media_track: int | None = None
|
_attr_media_track: int | None = None
|
||||||
_attr_repeat: str | None = None
|
_attr_repeat: RepeatMode | str | None = None
|
||||||
_attr_shuffle: bool | None = None
|
_attr_shuffle: bool | None = None
|
||||||
_attr_sound_mode_list: list[str] | None = None
|
_attr_sound_mode_list: list[str] | None = None
|
||||||
_attr_sound_mode: str | None = None
|
_attr_sound_mode: str | None = None
|
||||||
_attr_source_list: list[str] | None = None
|
_attr_source_list: list[str] | None = None
|
||||||
_attr_source: str | None = None
|
_attr_source: str | None = None
|
||||||
_attr_state: str | None = None
|
_attr_state: MediaPlayerState | str | None = None
|
||||||
_attr_supported_features: int = 0
|
_attr_supported_features: int = 0
|
||||||
_attr_volume_level: float | None = None
|
_attr_volume_level: float | None = None
|
||||||
|
|
||||||
@ -488,7 +491,7 @@ class MediaPlayerEntity(Entity):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> str | None:
|
def state(self) -> MediaPlayerState | str | None:
|
||||||
"""State of the player."""
|
"""State of the player."""
|
||||||
return self._attr_state
|
return self._attr_state
|
||||||
|
|
||||||
@ -515,7 +518,7 @@ class MediaPlayerEntity(Entity):
|
|||||||
return self._attr_media_content_id
|
return self._attr_media_content_id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def media_content_type(self) -> str | None:
|
def media_content_type(self) -> MediaType | str | None:
|
||||||
"""Content type of current playing media."""
|
"""Content type of current playing media."""
|
||||||
return self._attr_media_content_type
|
return self._attr_media_content_type
|
||||||
|
|
||||||
@ -664,7 +667,7 @@ class MediaPlayerEntity(Entity):
|
|||||||
return self._attr_shuffle
|
return self._attr_shuffle
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def repeat(self) -> str | None:
|
def repeat(self) -> RepeatMode | str | None:
|
||||||
"""Return current repeat mode."""
|
"""Return current repeat mode."""
|
||||||
return self._attr_repeat
|
return self._attr_repeat
|
||||||
|
|
||||||
@ -758,12 +761,14 @@ class MediaPlayerEntity(Entity):
|
|||||||
"""Send seek command."""
|
"""Send seek command."""
|
||||||
await self.hass.async_add_executor_job(self.media_seek, position)
|
await self.hass.async_add_executor_job(self.media_seek, position)
|
||||||
|
|
||||||
def play_media(self, media_type: str, media_id: str, **kwargs: Any) -> None:
|
def play_media(
|
||||||
|
self, media_type: MediaType | str, media_id: str, **kwargs: Any
|
||||||
|
) -> None:
|
||||||
"""Play a piece of media."""
|
"""Play a piece of media."""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
async def async_play_media(
|
async def async_play_media(
|
||||||
self, media_type: str, media_id: str, **kwargs: Any
|
self, media_type: MediaType | str, media_id: str, **kwargs: Any
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Play a piece of media."""
|
"""Play a piece of media."""
|
||||||
await self.hass.async_add_executor_job(
|
await self.hass.async_add_executor_job(
|
||||||
@ -905,7 +910,11 @@ class MediaPlayerEntity(Entity):
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.state in (STATE_OFF, STATE_IDLE, STATE_STANDBY):
|
if self.state in {
|
||||||
|
MediaPlayerState.OFF,
|
||||||
|
MediaPlayerState.IDLE,
|
||||||
|
MediaPlayerState.STANDBY,
|
||||||
|
}:
|
||||||
await self.async_turn_on()
|
await self.async_turn_on()
|
||||||
else:
|
else:
|
||||||
await self.async_turn_off()
|
await self.async_turn_off()
|
||||||
@ -954,7 +963,7 @@ class MediaPlayerEntity(Entity):
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.state == STATE_PLAYING:
|
if self.state == MediaPlayerState.PLAYING:
|
||||||
await self.async_media_pause()
|
await self.async_media_pause()
|
||||||
else:
|
else:
|
||||||
await self.async_media_play()
|
await self.async_media_play()
|
||||||
@ -962,7 +971,7 @@ class MediaPlayerEntity(Entity):
|
|||||||
@property
|
@property
|
||||||
def entity_picture(self) -> str | None:
|
def entity_picture(self) -> str | None:
|
||||||
"""Return image of the media playing."""
|
"""Return image of the media playing."""
|
||||||
if self.state == STATE_OFF:
|
if self.state == MediaPlayerState.OFF:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if self.media_image_remotely_accessible:
|
if self.media_image_remotely_accessible:
|
||||||
@ -1008,7 +1017,7 @@ class MediaPlayerEntity(Entity):
|
|||||||
if self.support_grouping:
|
if self.support_grouping:
|
||||||
state_attr[ATTR_GROUP_MEMBERS] = self.group_members
|
state_attr[ATTR_GROUP_MEMBERS] = self.group_members
|
||||||
|
|
||||||
if self.state == STATE_OFF:
|
if self.state == MediaPlayerState.OFF:
|
||||||
return state_attr
|
return state_attr
|
||||||
|
|
||||||
for attr in ATTR_TO_PROPERTY:
|
for attr in ATTR_TO_PROPERTY:
|
||||||
|
@ -41,6 +41,18 @@ ATTR_SOUND_MODE_LIST = "sound_mode_list"
|
|||||||
DOMAIN = "media_player"
|
DOMAIN = "media_player"
|
||||||
|
|
||||||
|
|
||||||
|
class MediaPlayerState(StrEnum):
|
||||||
|
"""State of media player entities."""
|
||||||
|
|
||||||
|
OFF = "off"
|
||||||
|
ON = "on"
|
||||||
|
IDLE = "idle"
|
||||||
|
PLAYING = "playing"
|
||||||
|
PAUSED = "paused"
|
||||||
|
STANDBY = "standby"
|
||||||
|
BUFFERING = "buffering"
|
||||||
|
|
||||||
|
|
||||||
class MediaClass(StrEnum):
|
class MediaClass(StrEnum):
|
||||||
"""Media class for media player entities."""
|
"""Media class for media player entities."""
|
||||||
|
|
||||||
|
@ -1514,7 +1514,7 @@ _INHERITANCE_MATCH: dict[str, list[ClassTypeHintMatch]] = {
|
|||||||
),
|
),
|
||||||
TypeHintMatch(
|
TypeHintMatch(
|
||||||
function_name="state",
|
function_name="state",
|
||||||
return_type=["str", None],
|
return_type=["MediaPlayerState", None],
|
||||||
),
|
),
|
||||||
TypeHintMatch(
|
TypeHintMatch(
|
||||||
function_name="access_token",
|
function_name="access_token",
|
||||||
@ -1534,7 +1534,7 @@ _INHERITANCE_MATCH: dict[str, list[ClassTypeHintMatch]] = {
|
|||||||
),
|
),
|
||||||
TypeHintMatch(
|
TypeHintMatch(
|
||||||
function_name="media_content_type",
|
function_name="media_content_type",
|
||||||
return_type=["str", None],
|
return_type=["MediaType", "str", None],
|
||||||
),
|
),
|
||||||
TypeHintMatch(
|
TypeHintMatch(
|
||||||
function_name="media_duration",
|
function_name="media_duration",
|
||||||
@ -1643,7 +1643,7 @@ _INHERITANCE_MATCH: dict[str, list[ClassTypeHintMatch]] = {
|
|||||||
),
|
),
|
||||||
TypeHintMatch(
|
TypeHintMatch(
|
||||||
function_name="repeat",
|
function_name="repeat",
|
||||||
return_type=["str", None],
|
return_type=["RepeatMode", None],
|
||||||
),
|
),
|
||||||
TypeHintMatch(
|
TypeHintMatch(
|
||||||
function_name="group_members",
|
function_name="group_members",
|
||||||
@ -1711,7 +1711,7 @@ _INHERITANCE_MATCH: dict[str, list[ClassTypeHintMatch]] = {
|
|||||||
TypeHintMatch(
|
TypeHintMatch(
|
||||||
function_name="play_media",
|
function_name="play_media",
|
||||||
arg_types={
|
arg_types={
|
||||||
1: "str",
|
1: "MediaType | str",
|
||||||
2: "str",
|
2: "str",
|
||||||
},
|
},
|
||||||
kwargs_type="Any",
|
kwargs_type="Any",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user