Use new media player enums [x-z] (#78068)

pull/78088/head
epenet 2022-09-08 23:25:51 +02:00 committed by GitHub
parent 8bdeb3ca5b
commit a9b5e276bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 92 deletions

View File

@ -17,10 +17,10 @@ from xbox.webapi.api.provider.smartglass.models import (
from homeassistant.components.media_player import ( from homeassistant.components.media_player import (
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature, MediaPlayerEntityFeature,
MediaPlayerState,
MediaType,
) )
from homeassistant.components.media_player.const import MEDIA_TYPE_APP, MEDIA_TYPE_GAME
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_OFF, STATE_ON, STATE_PAUSED, STATE_PLAYING
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -44,12 +44,12 @@ SUPPORT_XBOX = (
) )
XBOX_STATE_MAP = { XBOX_STATE_MAP = {
PlaybackState.Playing: STATE_PLAYING, PlaybackState.Playing: MediaPlayerState.PLAYING,
PlaybackState.Paused: STATE_PAUSED, PlaybackState.Paused: MediaPlayerState.PAUSED,
PowerState.On: STATE_ON, PowerState.On: MediaPlayerState.ON,
PowerState.SystemUpdate: STATE_OFF, PowerState.SystemUpdate: MediaPlayerState.OFF,
PowerState.ConnectedStandby: STATE_OFF, PowerState.ConnectedStandby: MediaPlayerState.OFF,
PowerState.Off: STATE_OFF, PowerState.Off: MediaPlayerState.OFF,
PowerState.Unknown: None, PowerState.Unknown: None,
} }
@ -109,7 +109,7 @@ class XboxMediaPlayer(CoordinatorEntity[XboxUpdateCoordinator], MediaPlayerEntit
@property @property
def supported_features(self): def supported_features(self):
"""Flag media player features that are supported.""" """Flag media player features that are supported."""
if self.state not in [STATE_PLAYING, STATE_PAUSED]: if self.state not in [MediaPlayerState.PLAYING, MediaPlayerState.PAUSED]:
return ( return (
SUPPORT_XBOX SUPPORT_XBOX
& ~MediaPlayerEntityFeature.NEXT_TRACK & ~MediaPlayerEntityFeature.NEXT_TRACK
@ -122,8 +122,8 @@ class XboxMediaPlayer(CoordinatorEntity[XboxUpdateCoordinator], MediaPlayerEntit
"""Media content type.""" """Media content type."""
app_details = self.data.app_details app_details = self.data.app_details
if app_details and app_details.product_family == "Games": if app_details and app_details.product_family == "Games":
return MEDIA_TYPE_GAME return MediaType.GAME
return MEDIA_TYPE_APP return MediaType.APP
@property @property
def media_title(self): def media_title(self):

View File

@ -10,8 +10,9 @@ from homeassistant.components.media_player import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature, MediaPlayerEntityFeature,
MediaPlayerState,
) )
from homeassistant.const import CONF_HOST, CONF_NAME, STATE_OFF, STATE_ON from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -70,7 +71,7 @@ class XiaomiTV(MediaPlayerEntity):
self._tv = pymitv.TV(ip) self._tv = pymitv.TV(ip)
# Default name value, only to be overridden by user. # Default name value, only to be overridden by user.
self._name = name self._name = name
self._state = STATE_OFF self._state = MediaPlayerState.OFF
@property @property
def name(self): def name(self):
@ -95,17 +96,17 @@ class XiaomiTV(MediaPlayerEntity):
because the TV won't accept any input when turned off. Thus, the user because the TV won't accept any input when turned off. Thus, the user
would be unable to turn the TV back on, unless it's done manually. would be unable to turn the TV back on, unless it's done manually.
""" """
if self._state != STATE_OFF: if self._state != MediaPlayerState.OFF:
self._tv.sleep() self._tv.sleep()
self._state = STATE_OFF self._state = MediaPlayerState.OFF
def turn_on(self) -> None: def turn_on(self) -> None:
"""Wake the TV back up from sleep.""" """Wake the TV back up from sleep."""
if self._state != STATE_ON: if self._state != MediaPlayerState.ON:
self._tv.wake() self._tv.wake()
self._state = STATE_ON self._state = MediaPlayerState.ON
def volume_up(self) -> None: def volume_up(self) -> None:
"""Increase volume by one.""" """Increase volume by one."""

View File

@ -12,16 +12,10 @@ from homeassistant.components.media_player import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature, MediaPlayerEntityFeature,
MediaPlayerState,
MediaType,
) )
from homeassistant.components.media_player.const import MEDIA_TYPE_MUSIC from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.const import (
CONF_HOST,
CONF_NAME,
STATE_IDLE,
STATE_OFF,
STATE_ON,
STATE_PLAYING,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -201,7 +195,7 @@ class YamahaDevice(MediaPlayerEntity):
self.receiver = receiver self.receiver = receiver
self._muted = False self._muted = False
self._volume = 0 self._volume = 0
self._pwstate = STATE_OFF self._pwstate = MediaPlayerState.OFF
self._current_source = None self._current_source = None
self._sound_mode = None self._sound_mode = None
self._sound_mode_list = None self._sound_mode_list = None
@ -226,13 +220,13 @@ class YamahaDevice(MediaPlayerEntity):
if self.receiver.on: if self.receiver.on:
if self._play_status is None: if self._play_status is None:
self._pwstate = STATE_ON self._pwstate = MediaPlayerState.ON
elif self._play_status.playing: elif self._play_status.playing:
self._pwstate = STATE_PLAYING self._pwstate = MediaPlayerState.PLAYING
else: else:
self._pwstate = STATE_IDLE self._pwstate = MediaPlayerState.IDLE
else: else:
self._pwstate = STATE_OFF self._pwstate = MediaPlayerState.OFF
self._muted = self.receiver.mute self._muted = self.receiver.mute
self._volume = (self.receiver.volume / 100) + 1 self._volume = (self.receiver.volume / 100) + 1
@ -443,7 +437,7 @@ class YamahaDevice(MediaPlayerEntity):
"""Content type of current playing media.""" """Content type of current playing media."""
# Loose assumption that if playback is supported, we are playing music # Loose assumption that if playback is supported, we are playing music
if self._is_playback_supported: if self._is_playback_supported:
return MEDIA_TYPE_MUSIC return MediaType.MUSIC
return None return None
@property @property

View File

@ -2,13 +2,7 @@
from aiomusiccast.capabilities import EntityType from aiomusiccast.capabilities import EntityType
from homeassistant.components.media_player.const import ( from homeassistant.components.media_player import MediaClass, RepeatMode
MEDIA_CLASS_DIRECTORY,
MEDIA_CLASS_TRACK,
REPEAT_MODE_ALL,
REPEAT_MODE_OFF,
REPEAT_MODE_ONE,
)
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
DOMAIN = "yamaha_musiccast" DOMAIN = "yamaha_musiccast"
@ -27,9 +21,9 @@ CONF_SERIAL = "serial"
DEFAULT_ZONE = "main" DEFAULT_ZONE = "main"
HA_REPEAT_MODE_TO_MC_MAPPING = { HA_REPEAT_MODE_TO_MC_MAPPING = {
REPEAT_MODE_OFF: "off", RepeatMode.OFF: "off",
REPEAT_MODE_ONE: "one", RepeatMode.ONE: "one",
REPEAT_MODE_ALL: "all", RepeatMode.ALL: "all",
} }
NULL_GROUP = "00000000000000000000000000000000" NULL_GROUP = "00000000000000000000000000000000"
@ -40,9 +34,9 @@ MC_REPEAT_MODE_TO_HA_MAPPING = {
} }
MEDIA_CLASS_MAPPING = { MEDIA_CLASS_MAPPING = {
"track": MEDIA_CLASS_TRACK, "track": MediaClass.TRACK,
"directory": MEDIA_CLASS_DIRECTORY, "directory": MediaClass.DIRECTORY,
"categories": MEDIA_CLASS_DIRECTORY, "categories": MediaClass.DIRECTORY,
} }
ENTITY_CATEGORY_MAPPING = { ENTITY_CATEGORY_MAPPING = {

View File

@ -11,20 +11,15 @@ from aiomusiccast.features import ZoneFeature
from homeassistant.components import media_source from homeassistant.components import media_source
from homeassistant.components.media_player import ( from homeassistant.components.media_player import (
BrowseMedia, BrowseMedia,
MediaClass,
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature, MediaPlayerEntityFeature,
) MediaPlayerState,
from homeassistant.components.media_player.browse_media import ( MediaType,
RepeatMode,
async_process_play_media_url, async_process_play_media_url,
) )
from homeassistant.components.media_player.const import (
MEDIA_CLASS_DIRECTORY,
MEDIA_CLASS_TRACK,
MEDIA_TYPE_MUSIC,
REPEAT_MODE_OFF,
)
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
@ -80,11 +75,12 @@ async def async_setup_entry(
class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity): class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"""The musiccast media player.""" """The musiccast media player."""
_attr_media_content_type = MediaType.MUSIC
_attr_should_poll = False _attr_should_poll = False
def __init__(self, zone_id, name, entry_id, coordinator): def __init__(self, zone_id, name, entry_id, coordinator):
"""Initialize the musiccast device.""" """Initialize the musiccast device."""
self._player_state = STATE_PLAYING self._player_state = MediaPlayerState.PLAYING
self._volume_muted = False self._volume_muted = False
self._shuffle = False self._shuffle = False
self._zone_id = zone_id self._zone_id = zone_id
@ -99,7 +95,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
self._volume_max = self.coordinator.data.zones[self._zone_id].max_volume self._volume_max = self.coordinator.data.zones[self._zone_id].max_volume
self._cur_track = 0 self._cur_track = 0
self._repeat = REPEAT_MODE_OFF self._repeat = RepeatMode.OFF
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Run when this Entity has been added to HA.""" """Run when this Entity has been added to HA."""
@ -148,21 +144,16 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"""Return the content ID of current playing media.""" """Return the content ID of current playing media."""
return None return None
@property
def media_content_type(self):
"""Return the content type of current playing media."""
return MEDIA_TYPE_MUSIC
@property @property
def state(self): def state(self):
"""Return the state of the player.""" """Return the state of the player."""
if self.coordinator.data.zones[self._zone_id].power == "on": if self.coordinator.data.zones[self._zone_id].power == "on":
if self._is_netusb and self.coordinator.data.netusb_playback == "pause": if self._is_netusb and self.coordinator.data.netusb_playback == "pause":
return STATE_PAUSED return MediaPlayerState.PAUSED
if self._is_netusb and self.coordinator.data.netusb_playback == "stop": if self._is_netusb and self.coordinator.data.netusb_playback == "stop":
return STATE_IDLE return MediaPlayerState.IDLE
return STATE_PLAYING return MediaPlayerState.PLAYING
return STATE_OFF return MediaPlayerState.OFF
@property @property
def volume_level(self): def volume_level(self):
@ -281,7 +272,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
) )
media_id = play_item.url media_id = play_item.url
if self.state == STATE_OFF: if self.state == MediaPlayerState.OFF:
await self.async_turn_on() await self.async_turn_on()
if media_id: if media_id:
@ -324,7 +315,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
), ),
) )
if self.state == STATE_OFF: if self.state == MediaPlayerState.OFF:
raise HomeAssistantError( raise HomeAssistantError(
"The device has to be turned on to be able to browse media." "The device has to be turned on to be able to browse media."
) )
@ -344,8 +335,8 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
def get_content_type(item): def get_content_type(item):
if item.can_play: if item.can_play:
return MEDIA_CLASS_TRACK return MediaClass.TRACK
return MEDIA_CLASS_DIRECTORY return MediaClass.DIRECTORY
children = [ children = [
BrowseMedia( BrowseMedia(
@ -429,7 +420,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
return ( return (
MC_REPEAT_MODE_TO_HA_MAPPING.get(self.coordinator.data.netusb_repeat) MC_REPEAT_MODE_TO_HA_MAPPING.get(self.coordinator.data.netusb_repeat)
if self._is_netusb if self._is_netusb
else REPEAT_MODE_OFF else RepeatMode.OFF
) )
@property @property
@ -459,7 +450,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
supported_features |= MediaPlayerEntityFeature.PLAY supported_features |= MediaPlayerEntityFeature.PLAY
supported_features |= MediaPlayerEntityFeature.STOP supported_features |= MediaPlayerEntityFeature.STOP
if self.state != STATE_OFF: if self.state != MediaPlayerState.OFF:
supported_features |= MediaPlayerEntityFeature.BROWSE_MEDIA supported_features |= MediaPlayerEntityFeature.BROWSE_MEDIA
return supported_features return supported_features
@ -486,7 +477,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"Service next track is not supported for non NetUSB or Tuner sources." "Service next track is not supported for non NetUSB or Tuner sources."
) )
async def async_set_repeat(self, repeat: str) -> None: async def async_set_repeat(self, repeat: RepeatMode) -> None:
"""Enable/disable repeat mode.""" """Enable/disable repeat mode."""
if self._is_netusb: if self._is_netusb:
await self.coordinator.musiccast.netusb_repeat( await self.coordinator.musiccast.netusb_repeat(
@ -705,7 +696,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
if entity.entity_id in group_members if entity.entity_id in group_members
] ]
if self.state == STATE_OFF: if self.state == MediaPlayerState.OFF:
await self.async_turn_on() await self.async_turn_on()
if not self.is_server and self.musiccast_zone_entity.is_server: if not self.is_server and self.musiccast_zone_entity.is_server:
@ -779,7 +770,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
""" """
# If we should join the group, which is served by the main zone, we can simply select main_sync as input. # If we should join the group, which is served by the main zone, we can simply select main_sync as input.
_LOGGER.debug("%s called service client join", self.entity_id) _LOGGER.debug("%s called service client join", self.entity_id)
if self.state == STATE_OFF: if self.state == MediaPlayerState.OFF:
await self.async_turn_on() await self.async_turn_on()
if self.ip_address == server.ip_address: if self.ip_address == server.ip_address:
if server.zone == DEFAULT_ZONE: if server.zone == DEFAULT_ZONE:

View File

@ -11,14 +11,9 @@ from homeassistant.components.media_player import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature, MediaPlayerEntityFeature,
MediaPlayerState,
) )
from homeassistant.const import ( from homeassistant.const import CONF_HOST, CONF_NAME
CONF_HOST,
CONF_NAME,
STATE_OFF,
STATE_PAUSED,
STATE_PLAYING,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -112,10 +107,10 @@ class ZiggoMediaboxXLDevice(MediaPlayerEntity):
try: try:
if self._mediabox.test_connection(): if self._mediabox.test_connection():
if self._mediabox.turned_on(): if self._mediabox.turned_on():
if self._state != STATE_PAUSED: if self._state != MediaPlayerState.PAUSED:
self._state = STATE_PLAYING self._state = MediaPlayerState.PLAYING
else: else:
self._state = STATE_OFF self._state = MediaPlayerState.OFF
self._available = True self._available = True
else: else:
self._available = False self._available = False
@ -164,30 +159,30 @@ class ZiggoMediaboxXLDevice(MediaPlayerEntity):
def media_play(self) -> None: def media_play(self) -> None:
"""Send play command.""" """Send play command."""
self.send_keys(["PLAY"]) self.send_keys(["PLAY"])
self._state = STATE_PLAYING self._state = MediaPlayerState.PLAYING
def media_pause(self) -> None: def media_pause(self) -> None:
"""Send pause command.""" """Send pause command."""
self.send_keys(["PAUSE"]) self.send_keys(["PAUSE"])
self._state = STATE_PAUSED self._state = MediaPlayerState.PAUSED
def media_play_pause(self) -> None: def media_play_pause(self) -> None:
"""Simulate play pause media player.""" """Simulate play pause media player."""
self.send_keys(["PAUSE"]) self.send_keys(["PAUSE"])
if self._state == STATE_PAUSED: if self._state == MediaPlayerState.PAUSED:
self._state = STATE_PLAYING self._state = MediaPlayerState.PLAYING
else: else:
self._state = STATE_PAUSED self._state = MediaPlayerState.PAUSED
def media_next_track(self) -> None: def media_next_track(self) -> None:
"""Channel up.""" """Channel up."""
self.send_keys(["CHAN_UP"]) self.send_keys(["CHAN_UP"])
self._state = STATE_PLAYING self._state = MediaPlayerState.PLAYING
def media_previous_track(self) -> None: def media_previous_track(self) -> None:
"""Channel down.""" """Channel down."""
self.send_keys(["CHAN_DOWN"]) self.send_keys(["CHAN_DOWN"])
self._state = STATE_PLAYING self._state = MediaPlayerState.PLAYING
def select_source(self, source): def select_source(self, source):
"""Select the channel.""" """Select the channel."""
@ -206,4 +201,4 @@ class ZiggoMediaboxXLDevice(MediaPlayerEntity):
return return
self.send_keys([f"NUM_{digit}" for digit in str(digits)]) self.send_keys([f"NUM_{digit}" for digit in str(digits)])
self._state = STATE_PLAYING self._state = MediaPlayerState.PLAYING