Use new media player enums [r] (#78062)
parent
52b5e1779f
commit
45d0ec7150
|
@ -12,30 +12,18 @@ import yarl
|
|||
|
||||
from homeassistant.components import media_source
|
||||
from homeassistant.components.media_player import (
|
||||
ATTR_MEDIA_EXTRA,
|
||||
BrowseMedia,
|
||||
MediaPlayerDeviceClass,
|
||||
MediaPlayerEntity,
|
||||
MediaPlayerEntityFeature,
|
||||
MediaPlayerState,
|
||||
MediaType,
|
||||
async_process_play_media_url,
|
||||
)
|
||||
from homeassistant.components.media_player.const import (
|
||||
ATTR_MEDIA_EXTRA,
|
||||
MEDIA_TYPE_APP,
|
||||
MEDIA_TYPE_CHANNEL,
|
||||
MEDIA_TYPE_MUSIC,
|
||||
MEDIA_TYPE_URL,
|
||||
MEDIA_TYPE_VIDEO,
|
||||
)
|
||||
from homeassistant.components.stream.const import FORMAT_CONTENT_TYPE, HLS_PROVIDER
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
ATTR_NAME,
|
||||
STATE_IDLE,
|
||||
STATE_ON,
|
||||
STATE_PAUSED,
|
||||
STATE_PLAYING,
|
||||
STATE_STANDBY,
|
||||
)
|
||||
from homeassistant.const import ATTR_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_platform
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
@ -59,16 +47,16 @@ _LOGGER = logging.getLogger(__name__)
|
|||
|
||||
|
||||
STREAM_FORMAT_TO_MEDIA_TYPE = {
|
||||
"dash": MEDIA_TYPE_VIDEO,
|
||||
"hls": MEDIA_TYPE_VIDEO,
|
||||
"ism": MEDIA_TYPE_VIDEO,
|
||||
"m4a": MEDIA_TYPE_MUSIC,
|
||||
"m4v": MEDIA_TYPE_VIDEO,
|
||||
"mka": MEDIA_TYPE_MUSIC,
|
||||
"mkv": MEDIA_TYPE_VIDEO,
|
||||
"mks": MEDIA_TYPE_VIDEO,
|
||||
"mp3": MEDIA_TYPE_MUSIC,
|
||||
"mp4": MEDIA_TYPE_VIDEO,
|
||||
"dash": MediaType.VIDEO,
|
||||
"hls": MediaType.VIDEO,
|
||||
"ism": MediaType.VIDEO,
|
||||
"m4a": MediaType.MUSIC,
|
||||
"m4v": MediaType.VIDEO,
|
||||
"mka": MediaType.MUSIC,
|
||||
"mkv": MediaType.VIDEO,
|
||||
"mks": MediaType.VIDEO,
|
||||
"mp3": MediaType.MUSIC,
|
||||
"mp4": MediaType.VIDEO,
|
||||
}
|
||||
|
||||
ATTRS_TO_LAUNCH_PARAMS = {
|
||||
|
@ -150,10 +138,10 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
|
|||
return MediaPlayerDeviceClass.RECEIVER
|
||||
|
||||
@property
|
||||
def state(self) -> str | None:
|
||||
def state(self) -> MediaPlayerState | None:
|
||||
"""Return the state of the device."""
|
||||
if self.coordinator.data.state.standby:
|
||||
return STATE_STANDBY
|
||||
return MediaPlayerState.STANDBY
|
||||
|
||||
if self.coordinator.data.app is None:
|
||||
return None
|
||||
|
@ -163,28 +151,28 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
|
|||
or self.coordinator.data.app.name == "Roku"
|
||||
or self.coordinator.data.app.screensaver
|
||||
):
|
||||
return STATE_IDLE
|
||||
return MediaPlayerState.IDLE
|
||||
|
||||
if self.coordinator.data.media:
|
||||
if self.coordinator.data.media.paused:
|
||||
return STATE_PAUSED
|
||||
return STATE_PLAYING
|
||||
return MediaPlayerState.PAUSED
|
||||
return MediaPlayerState.PLAYING
|
||||
|
||||
if self.coordinator.data.app.name:
|
||||
return STATE_ON
|
||||
return MediaPlayerState.ON
|
||||
|
||||
return None
|
||||
|
||||
@property
|
||||
def media_content_type(self) -> str | None:
|
||||
def media_content_type(self) -> MediaType | None:
|
||||
"""Content type of current playing media."""
|
||||
if self.app_id is None or self.app_name in ("Power Saver", "Roku"):
|
||||
return None
|
||||
|
||||
if self.app_id == "tvinput.dtv" and self.coordinator.data.channel is not None:
|
||||
return MEDIA_TYPE_CHANNEL
|
||||
return MediaType.CHANNEL
|
||||
|
||||
return MEDIA_TYPE_APP
|
||||
return MediaType.APP
|
||||
|
||||
@property
|
||||
def media_image_url(self) -> str | None:
|
||||
|
@ -282,7 +270,7 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
|
|||
media_image_id: str | None = None,
|
||||
) -> tuple[bytes | None, str | None]:
|
||||
"""Fetch media browser image to serve via proxy."""
|
||||
if media_content_type == MEDIA_TYPE_APP and media_content_id:
|
||||
if media_content_type == MediaType.APP and media_content_id:
|
||||
image_url = self.coordinator.roku.app_icon_url(media_content_id)
|
||||
return await self._async_fetch_image(image_url)
|
||||
|
||||
|
@ -317,21 +305,21 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
|
|||
@roku_exception_handler()
|
||||
async def async_media_pause(self) -> None:
|
||||
"""Send pause command."""
|
||||
if self.state not in (STATE_STANDBY, STATE_PAUSED):
|
||||
if self.state not in {MediaPlayerState.STANDBY, MediaPlayerState.PAUSED}:
|
||||
await self.coordinator.roku.remote("play")
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
@roku_exception_handler()
|
||||
async def async_media_play(self) -> None:
|
||||
"""Send play command."""
|
||||
if self.state not in (STATE_STANDBY, STATE_PLAYING):
|
||||
if self.state not in {MediaPlayerState.STANDBY, MediaPlayerState.PLAYING}:
|
||||
await self.coordinator.roku.remote("play")
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
@roku_exception_handler()
|
||||
async def async_media_play_pause(self) -> None:
|
||||
"""Send play/pause command."""
|
||||
if self.state != STATE_STANDBY:
|
||||
if self.state != MediaPlayerState.STANDBY:
|
||||
await self.coordinator.roku.remote("play")
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
|
@ -380,19 +368,19 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
|
|||
sourced_media = await media_source.async_resolve_media(
|
||||
self.hass, media_id, self.entity_id
|
||||
)
|
||||
media_type = MEDIA_TYPE_URL
|
||||
media_type = MediaType.URL
|
||||
media_id = sourced_media.url
|
||||
mime_type = sourced_media.mime_type
|
||||
stream_name = original_media_id
|
||||
stream_format = guess_stream_format(media_id, mime_type)
|
||||
|
||||
if media_type == FORMAT_CONTENT_TYPE[HLS_PROVIDER]:
|
||||
media_type = MEDIA_TYPE_VIDEO
|
||||
media_type = MediaType.VIDEO
|
||||
mime_type = FORMAT_CONTENT_TYPE[HLS_PROVIDER]
|
||||
stream_name = "Camera Stream"
|
||||
stream_format = "hls"
|
||||
|
||||
if media_type in (MEDIA_TYPE_MUSIC, MEDIA_TYPE_URL, MEDIA_TYPE_VIDEO):
|
||||
if media_type in {MediaType.MUSIC, MediaType.URL, MediaType.VIDEO}:
|
||||
# If media ID is a relative URL, we serve it from HA.
|
||||
media_id = async_process_play_media_url(self.hass, media_id)
|
||||
|
||||
|
@ -417,12 +405,12 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
|
|||
return
|
||||
|
||||
if (
|
||||
media_type == MEDIA_TYPE_URL
|
||||
and STREAM_FORMAT_TO_MEDIA_TYPE[extra[ATTR_FORMAT]] == MEDIA_TYPE_MUSIC
|
||||
media_type == MediaType.URL
|
||||
and STREAM_FORMAT_TO_MEDIA_TYPE[extra[ATTR_FORMAT]] == MediaType.MUSIC
|
||||
):
|
||||
media_type = MEDIA_TYPE_MUSIC
|
||||
media_type = MediaType.MUSIC
|
||||
|
||||
if media_type == MEDIA_TYPE_MUSIC and "tts_proxy" in media_id:
|
||||
if media_type == MediaType.MUSIC and "tts_proxy" in media_id:
|
||||
stream_name = "Text to Speech"
|
||||
elif stream_name is None:
|
||||
if stream_format == "ism":
|
||||
|
@ -433,7 +421,7 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
|
|||
if extra.get(ATTR_NAME) is None:
|
||||
extra[ATTR_NAME] = stream_name
|
||||
|
||||
if media_type == MEDIA_TYPE_APP:
|
||||
if media_type == MediaType.APP:
|
||||
params = {
|
||||
param: extra[attr]
|
||||
for attr, param in ATTRS_TO_LAUNCH_PARAMS.items()
|
||||
|
@ -441,9 +429,9 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
|
|||
}
|
||||
|
||||
await self.coordinator.roku.launch(media_id, params)
|
||||
elif media_type == MEDIA_TYPE_CHANNEL:
|
||||
elif media_type == MediaType.CHANNEL:
|
||||
await self.coordinator.roku.tune(media_id)
|
||||
elif media_type == MEDIA_TYPE_MUSIC:
|
||||
elif media_type == MediaType.MUSIC:
|
||||
if extra.get(ATTR_ARTIST_NAME) is None:
|
||||
extra[ATTR_ARTIST_NAME] = "Home Assistant"
|
||||
|
||||
|
@ -456,7 +444,7 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
|
|||
params = {"t": "a", **params}
|
||||
|
||||
await self.coordinator.roku.play_on_roku(media_id, params)
|
||||
elif media_type in (MEDIA_TYPE_URL, MEDIA_TYPE_VIDEO):
|
||||
elif media_type in {MediaType.URL, MediaType.VIDEO}:
|
||||
params = {
|
||||
param: extra[attr]
|
||||
for (attr, param) in ATTRS_TO_PLAY_ON_ROKU_PARAMS.items()
|
||||
|
|
|
@ -8,18 +8,13 @@ from roonapi import split_media_path
|
|||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.media_player import (
|
||||
BrowseMedia,
|
||||
MediaPlayerEntity,
|
||||
MediaPlayerEntityFeature,
|
||||
MediaPlayerState,
|
||||
)
|
||||
from homeassistant.components.media_player.browse_media import BrowseMedia
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
DEVICE_DEFAULT_NAME,
|
||||
STATE_IDLE,
|
||||
STATE_OFF,
|
||||
STATE_PAUSED,
|
||||
STATE_PLAYING,
|
||||
)
|
||||
from homeassistant.const import DEVICE_DEFAULT_NAME
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import config_validation as cv, entity_platform
|
||||
from homeassistant.helpers.dispatcher import (
|
||||
|
@ -106,7 +101,7 @@ class RoonDevice(MediaPlayerEntity):
|
|||
self._available = True
|
||||
self._last_position_update = None
|
||||
self._supports_standby = False
|
||||
self._state = STATE_IDLE
|
||||
self._state = MediaPlayerState.IDLE
|
||||
self._unique_id = None
|
||||
self._zone_id = None
|
||||
self._output_id = None
|
||||
|
@ -172,12 +167,12 @@ class RoonDevice(MediaPlayerEntity):
|
|||
if not self.player_data["is_available"]:
|
||||
# this player was removed
|
||||
self._available = False
|
||||
self._state = STATE_OFF
|
||||
self._state = MediaPlayerState.OFF
|
||||
else:
|
||||
self._available = True
|
||||
# determine player state
|
||||
self.update_state()
|
||||
if self.state == STATE_PLAYING:
|
||||
if self.state == MediaPlayerState.PLAYING:
|
||||
self._last_position_update = utcnow()
|
||||
|
||||
@classmethod
|
||||
|
@ -254,20 +249,20 @@ class RoonDevice(MediaPlayerEntity):
|
|||
if source["supports_standby"] and source["status"] != "indeterminate":
|
||||
self._supports_standby = True
|
||||
if source["status"] in ["standby", "deselected"]:
|
||||
new_state = STATE_OFF
|
||||
new_state = MediaPlayerState.OFF
|
||||
break
|
||||
# determine player state
|
||||
if not new_state:
|
||||
if self.player_data["state"] == "playing":
|
||||
new_state = STATE_PLAYING
|
||||
new_state = MediaPlayerState.PLAYING
|
||||
elif self.player_data["state"] == "loading":
|
||||
new_state = STATE_PLAYING
|
||||
new_state = MediaPlayerState.PLAYING
|
||||
elif self.player_data["state"] == "stopped":
|
||||
new_state = STATE_IDLE
|
||||
new_state = MediaPlayerState.IDLE
|
||||
elif self.player_data["state"] == "paused":
|
||||
new_state = STATE_PAUSED
|
||||
new_state = MediaPlayerState.PAUSED
|
||||
else:
|
||||
new_state = STATE_IDLE
|
||||
new_state = MediaPlayerState.IDLE
|
||||
self._state = new_state
|
||||
self._unique_id = self.player_data["dev_id"]
|
||||
self._zone_id = self.player_data["zone_id"]
|
||||
|
|
|
@ -8,15 +8,14 @@ from homeassistant.components.media_player import (
|
|||
PLATFORM_SCHEMA,
|
||||
MediaPlayerEntity,
|
||||
MediaPlayerEntityFeature,
|
||||
MediaPlayerState,
|
||||
MediaType,
|
||||
)
|
||||
from homeassistant.components.media_player.const import MEDIA_TYPE_MUSIC
|
||||
from homeassistant.const import (
|
||||
CONF_HOST,
|
||||
CONF_NAME,
|
||||
CONF_PORT,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
@ -70,6 +69,7 @@ async def async_setup_platform(
|
|||
class RussoundZoneDevice(MediaPlayerEntity):
|
||||
"""Representation of a Russound Zone."""
|
||||
|
||||
_attr_media_content_type = MediaType.MUSIC
|
||||
_attr_should_poll = False
|
||||
_attr_supported_features = (
|
||||
MediaPlayerEntityFeature.VOLUME_MUTE
|
||||
|
@ -130,9 +130,9 @@ class RussoundZoneDevice(MediaPlayerEntity):
|
|||
"""Return the state of the device."""
|
||||
status = self._zone_var("status", "OFF")
|
||||
if status == "ON":
|
||||
return STATE_ON
|
||||
return MediaPlayerState.ON
|
||||
if status == "OFF":
|
||||
return STATE_OFF
|
||||
return MediaPlayerState.OFF
|
||||
|
||||
@property
|
||||
def source(self):
|
||||
|
@ -144,11 +144,6 @@ class RussoundZoneDevice(MediaPlayerEntity):
|
|||
"""Return a list of available input sources."""
|
||||
return [x[1] for x in self._sources]
|
||||
|
||||
@property
|
||||
def media_content_type(self):
|
||||
"""Content type of current playing media."""
|
||||
return MEDIA_TYPE_MUSIC
|
||||
|
||||
@property
|
||||
def media_title(self):
|
||||
"""Title of current playing media."""
|
||||
|
|
|
@ -10,8 +10,9 @@ from homeassistant.components.media_player import (
|
|||
PLATFORM_SCHEMA,
|
||||
MediaPlayerEntity,
|
||||
MediaPlayerEntityFeature,
|
||||
MediaPlayerState,
|
||||
)
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, STATE_OFF, STATE_ON
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
|
||||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
@ -100,9 +101,9 @@ class RussoundRNETDevice(MediaPlayerEntity):
|
|||
if ret is not None:
|
||||
_LOGGER.debug("Updating status for zone %s", self._zone_id)
|
||||
if ret[0] == 0:
|
||||
self._state = STATE_OFF
|
||||
self._state = MediaPlayerState.OFF
|
||||
else:
|
||||
self._state = STATE_ON
|
||||
self._state = MediaPlayerState.ON
|
||||
self._volume = ret[2] * 2 / 100.0
|
||||
# Returns 0 based index for source.
|
||||
index = ret[1]
|
||||
|
|
Loading…
Reference in New Issue