Improve media_player type hints in integrations (#90029)

Fix some media_player type hints
pull/87568/head
epenet 2023-03-21 11:10:12 +01:00 committed by GitHub
parent 04872f72ea
commit 4836404288
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 62 additions and 32 deletions

View File

@ -8,6 +8,7 @@ from homeassistant.components.media_player import (
BrowseMedia, BrowseMedia,
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerState, MediaPlayerState,
MediaType,
async_process_play_media_url, async_process_play_media_url,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -42,7 +43,7 @@ class FullyMediaPlayer(FullyKioskEntity, MediaPlayerEntity):
self._attr_state = MediaPlayerState.IDLE self._attr_state = MediaPlayerState.IDLE
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."""
if media_source.is_media_source_id(media_id): if media_source.is_media_source_id(media_id):

View File

@ -1,6 +1,7 @@
"""Platform allowing several media players to be grouped into one media player.""" """Platform allowing several media players to be grouped into one media player."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Mapping
from contextlib import suppress from contextlib import suppress
from typing import Any from typing import Any
@ -20,6 +21,7 @@ from homeassistant.components.media_player import (
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature, MediaPlayerEntityFeature,
MediaPlayerState, MediaPlayerState,
MediaType,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -207,7 +209,7 @@ class MediaPlayerGroup(MediaPlayerEntity):
return self._name return self._name
@property @property
def extra_state_attributes(self) -> dict: def extra_state_attributes(self) -> Mapping[str, Any]:
"""Return the state attributes for the media group.""" """Return the state attributes for the media group."""
return {ATTR_ENTITY_ID: self._entities} return {ATTR_ENTITY_ID: self._entities}
@ -298,7 +300,7 @@ class MediaPlayerGroup(MediaPlayerEntity):
) )
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."""
data = { data = {

View File

@ -100,7 +100,7 @@ class GstreamerDevice(MediaPlayerEntity):
self._player.volume = volume self._player.volume = volume
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 media.""" """Play media."""
# Handle media_source # Handle media_source

View File

@ -30,6 +30,7 @@ from homeassistant.components.media_player import (
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature, MediaPlayerEntityFeature,
MediaPlayerState, MediaPlayerState,
MediaType,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -105,7 +106,9 @@ class CecPlayerEntity(CecEntity, MediaPlayerEntity):
self.send_keypress(KEY_STOP) self.send_keypress(KEY_STOP)
self._attr_state = MediaPlayerState.IDLE self._attr_state = MediaPlayerState.IDLE
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:
"""Not supported.""" """Not supported."""
raise NotImplementedError() raise NotImplementedError()

View File

@ -195,7 +195,7 @@ class HeosMediaPlayer(MediaPlayerEntity):
@log_command_error("play media") @log_command_error("play media")
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."""
if media_source.is_media_source_id(media_id): if media_source.is_media_source_id(media_id):

View File

@ -142,7 +142,9 @@ class HorizonDevice(MediaPlayerEntity):
else: else:
self._attr_state = MediaPlayerState.PAUSED self._attr_state = MediaPlayerState.PAUSED
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 media / switch to channel.""" """Play media / switch to channel."""
if media_type == MediaType.CHANNEL: if media_type == MediaType.CHANNEL:
try: try:

View File

@ -380,7 +380,9 @@ class ItunesDevice(MediaPlayerEntity):
response = self.client.previous() response = self.client.previous()
self.update_state(response) self.update_state(response)
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:
"""Send the play_media command to the media player.""" """Send the play_media command to the media player."""
if media_type == MediaType.PLAYLIST: if media_type == MediaType.PLAYLIST:
response = self.client.play_playlist(media_id) response = self.client.play_playlist(media_id)

View File

@ -262,7 +262,7 @@ class JellyfinMediaPlayer(JellyfinEntity, MediaPlayerEntity):
self._attr_state = MediaPlayerState.IDLE self._attr_state = MediaPlayerState.IDLE
def play_media( def play_media(
self, media_type: str, media_id: str, **kwargs: dict[str, Any] self, media_type: MediaType | str, media_id: str, **kwargs: Any
) -> None: ) -> None:
"""Play a piece of media.""" """Play a piece of media."""
self.coordinator.api_client.jellyfin.remote_play_media( self.coordinator.api_client.jellyfin.remote_play_media(

View File

@ -260,7 +260,9 @@ class LgTVDevice(MediaPlayerEntity):
"""Send the previous track command.""" """Send the previous track command."""
self.send_command(LG_COMMAND.REWIND) self.send_command(LG_COMMAND.REWIND)
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:
"""Tune to channel.""" """Tune to channel."""
if media_type != MediaType.CHANNEL: if media_type != MediaType.CHANNEL:
raise ValueError(f"Invalid media type: {media_type}") raise ValueError(f"Invalid media type: {media_type}")

View File

@ -435,7 +435,7 @@ class MpdDevice(MediaPlayerEntity):
self._muted = mute self._muted = mute
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:
"""Send the media player the command for playing a playlist.""" """Send the media player the command for playing a playlist."""
if media_source.is_media_source_id(media_id): if media_source.is_media_source_id(media_id):

View File

@ -14,6 +14,7 @@ from homeassistant.components.media_player import (
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature, MediaPlayerEntityFeature,
MediaPlayerState, MediaPlayerState,
MediaType,
) )
from homeassistant.const import ATTR_ENTITY_ID, CONF_HOST, CONF_NAME from homeassistant.const import ATTR_ENTITY_ID, CONF_HOST, CONF_NAME
from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.core import HomeAssistant, ServiceCall
@ -394,7 +395,9 @@ class OnkyoDevice(MediaPlayerEntity):
source = self._reverse_mapping[source] source = self._reverse_mapping[source]
self.command(f"input-selector {source}") self.command(f"input-selector {source}")
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 radio station by preset number.""" """Play radio station by preset number."""
source = self._reverse_mapping[self._attr_source] source = self._reverse_mapping[self._attr_source]
if media_type.lower() == "radio" and source in DEFAULT_PLAYABLE_SOURCES: if media_type.lower() == "radio" and source in DEFAULT_PLAYABLE_SOURCES:

View File

@ -211,7 +211,7 @@ class OpenhomeDevice(MediaPlayerEntity):
@catch_request_errors() @catch_request_errors()
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:
"""Send the play_media command to the media player.""" """Send the play_media command to the media player."""
if media_source.is_media_source_id(media_id): if media_source.is_media_source_id(media_id):

View File

@ -185,7 +185,7 @@ class PanasonicVieraTVEntity(MediaPlayerEntity):
await self._remote.async_send_key(Keys.rewind) await self._remote.async_send_key(Keys.rewind)
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 media.""" """Play media."""
if media_source.is_media_source_id(media_id): if media_source.is_media_source_id(media_id):

View File

@ -479,7 +479,9 @@ class PlexMediaPlayer(MediaPlayerEntity):
if self.device and "playback" in self._device_protocol_capabilities: if self.device and "playback" in self._device_protocol_capabilities:
self.device.skipPrevious(self._active_media_plexapi_type) self.device.skipPrevious(self._active_media_plexapi_type)
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."""
if not (self.device and "playback" in self._device_protocol_capabilities): if not (self.device and "playback" in self._device_protocol_capabilities):
raise HomeAssistantError( raise HomeAssistantError(

View File

@ -252,7 +252,7 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
return None return None
@property @property
def source_list(self) -> list: def source_list(self) -> list[str]:
"""List of available input sources.""" """List of available input sources."""
return ["Home"] + sorted( return ["Home"] + sorted(
app.name for app in self.coordinator.data.apps if app.name is not None app.name for app in self.coordinator.data.apps if app.name is not None
@ -353,7 +353,7 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
@roku_exception_handler() @roku_exception_handler()
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 media from a URL or file, launch an application, or tune to a channel.""" """Play media from a URL or file, launch an application, or tune to a channel."""
extra: dict[str, Any] = kwargs.get(ATTR_MEDIA_EXTRA) or {} extra: dict[str, Any] = kwargs.get(ATTR_MEDIA_EXTRA) or {}

View File

@ -12,6 +12,7 @@ from homeassistant.components.media_player import (
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature, MediaPlayerEntityFeature,
MediaPlayerState, MediaPlayerState,
MediaType,
RepeatMode, RepeatMode,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -394,7 +395,9 @@ class RoonDevice(MediaPlayerEntity):
raise ValueError(f"Unsupported repeat mode: {repeat}") raise ValueError(f"Unsupported repeat mode: {repeat}")
self._server.roonapi.repeat(self.output_id, REPEAT_MODE_MAPPING_TO_ROON[repeat]) self._server.roonapi.repeat(self.output_id, REPEAT_MODE_MAPPING_TO_ROON[repeat])
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:
"""Send the play_media command to the media player.""" """Send the play_media command to the media player."""
_LOGGER.debug("Playback request for %s / %s", media_type, media_id) _LOGGER.debug("Playback request for %s / %s", media_type, media_id)

View File

@ -444,7 +444,7 @@ class SamsungTVDevice(MediaPlayerEntity):
await self._async_send_keys(["KEY_CHDOWN"]) await self._async_send_keys(["KEY_CHDOWN"])
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:
"""Support changing a channel.""" """Support changing a channel."""
if media_type == MediaType.APP: if media_type == MediaType.APP:

View File

@ -15,6 +15,7 @@ from homeassistant.components.media_player import (
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature, MediaPlayerEntityFeature,
MediaPlayerState, MediaPlayerState,
MediaType,
async_process_play_media_url, async_process_play_media_url,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -175,7 +176,7 @@ class SlimProtoPlayer(MediaPlayerEntity):
await self.player.power(False) await self.player.power(False)
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:
"""Send the play_media command to the media player.""" """Send the play_media command to the media player."""
to_send_media_type: str | None = media_type to_send_media_type: str | None = media_type

View File

@ -303,7 +303,7 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
return PLAY_MODES[self.media.play_mode][0] return PLAY_MODES[self.media.play_mode][0]
@property @property
def repeat(self) -> str | None: def repeat(self) -> RepeatMode | None:
"""Return current repeat mode.""" """Return current repeat mode."""
sonos_repeat = PLAY_MODES[self.media.play_mode][1] sonos_repeat = PLAY_MODES[self.media.play_mode][1]
return SONOS_TO_REPEAT[sonos_repeat] return SONOS_TO_REPEAT[sonos_repeat]
@ -493,7 +493,7 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
@soco_error() @soco_error()
def play_media( # noqa: C901 def play_media( # noqa: C901
self, media_type: str, media_id: str, **kwargs: Any self, media_type: MediaType | str, media_id: str, **kwargs: Any
) -> None: ) -> None:
"""Send the play_media command to the media player. """Send the play_media command to the media player.

View File

@ -18,6 +18,7 @@ from homeassistant.components.media_player import (
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature, MediaPlayerEntityFeature,
MediaPlayerState, MediaPlayerState,
MediaType,
async_process_play_media_url, async_process_play_media_url,
) )
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
@ -282,7 +283,7 @@ class SoundTouchMediaPlayer(MediaPlayerEntity):
) )
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."""
if media_source.is_media_source_id(media_id): if media_source.is_media_source_id(media_id):
@ -295,7 +296,9 @@ class SoundTouchMediaPlayer(MediaPlayerEntity):
partial(self.play_media, media_type, media_id, **kwargs) partial(self.play_media, media_type, media_id, **kwargs)
) )
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."""
_LOGGER.debug("Starting media with media_id: %s", media_id) _LOGGER.debug("Starting media with media_id: %s", media_id)
if re.match(r"http?://", str(media_id)): if re.match(r"http?://", str(media_id)):

View File

@ -281,7 +281,7 @@ class SpotifyMediaPlayer(MediaPlayerEntity):
return self._currently_playing.get("shuffle_state") return self._currently_playing.get("shuffle_state")
@property @property
def repeat(self) -> str | None: def repeat(self) -> RepeatMode | None:
"""Return current repeat mode.""" """Return current repeat mode."""
if ( if (
not self._currently_playing not self._currently_playing
@ -321,7 +321,9 @@ class SpotifyMediaPlayer(MediaPlayerEntity):
self.data.client.seek_track(int(position * 1000)) self.data.client.seek_track(int(position * 1000))
@spotify_exception_handler @spotify_exception_handler
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 media.""" """Play media."""
media_type = media_type.removeprefix(MEDIA_PLAYER_PREFIX) media_type = media_type.removeprefix(MEDIA_PLAYER_PREFIX)

View File

@ -469,7 +469,7 @@ class SqueezeBoxEntity(MediaPlayerEntity):
await self._player.async_set_power(True) await self._player.async_set_power(True)
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:
"""Send the play_media command to the media player.""" """Send the play_media command to the media player."""
index = None index = None

View File

@ -44,6 +44,8 @@ from homeassistant.components.media_player import (
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature, MediaPlayerEntityFeature,
MediaPlayerState, MediaPlayerState,
MediaType,
RepeatMode,
) )
from homeassistant.components.media_player.browse_media import BrowseMedia from homeassistant.components.media_player.browse_media import BrowseMedia
from homeassistant.const import ( from homeassistant.const import (
@ -574,7 +576,7 @@ class UniversalMediaPlayer(MediaPlayerEntity):
await self._async_call_service(SERVICE_MEDIA_SEEK, data) await self._async_call_service(SERVICE_MEDIA_SEEK, data)
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."""
data = {ATTR_MEDIA_CONTENT_TYPE: media_type, ATTR_MEDIA_CONTENT_ID: media_id} data = {ATTR_MEDIA_CONTENT_TYPE: media_type, ATTR_MEDIA_CONTENT_ID: media_id}
@ -613,7 +615,7 @@ class UniversalMediaPlayer(MediaPlayerEntity):
data = {ATTR_MEDIA_SHUFFLE: shuffle} data = {ATTR_MEDIA_SHUFFLE: shuffle}
await self._async_call_service(SERVICE_SHUFFLE_SET, data, allow_override=True) await self._async_call_service(SERVICE_SHUFFLE_SET, data, allow_override=True)
async def async_set_repeat(self, repeat: str) -> None: async def async_set_repeat(self, repeat: RepeatMode) -> None:
"""Set repeat mode.""" """Set repeat mode."""
data = {ATTR_MEDIA_REPEAT: repeat} data = {ATTR_MEDIA_REPEAT: repeat}
await self._async_call_service(SERVICE_REPEAT_SET, data, allow_override=True) await self._async_call_service(SERVICE_REPEAT_SET, data, allow_override=True)

View File

@ -205,7 +205,7 @@ class XboxMediaPlayer(CoordinatorEntity[XboxUpdateCoordinator], MediaPlayerEntit
) )
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:
"""Launch an app on the Xbox.""" """Launch an app on the Xbox."""
if media_id == "Home": if media_id == "Home":

View File

@ -347,7 +347,9 @@ class YamahaDevice(MediaPlayerEntity):
"""Select input source.""" """Select input source."""
self.receiver.input = self._reverse_mapping.get(source, source) self.receiver.input = self._reverse_mapping.get(source, source)
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 media from an ID. """Play media from an ID.
This exposes a pass through for various input sources in the This exposes a pass through for various input sources in the

View File

@ -263,7 +263,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
) )
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 media.""" """Play media."""
if media_source.is_media_source_id(media_id): if media_source.is_media_source_id(media_id):