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 (
MediaPlayerEntity,
MediaPlayerEntityFeature,
MediaPlayerState,
MediaType,
)
from homeassistant.components.media_player.const import MEDIA_TYPE_APP, MEDIA_TYPE_GAME
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.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -44,12 +44,12 @@ SUPPORT_XBOX = (
)
XBOX_STATE_MAP = {
PlaybackState.Playing: STATE_PLAYING,
PlaybackState.Paused: STATE_PAUSED,
PowerState.On: STATE_ON,
PowerState.SystemUpdate: STATE_OFF,
PowerState.ConnectedStandby: STATE_OFF,
PowerState.Off: STATE_OFF,
PlaybackState.Playing: MediaPlayerState.PLAYING,
PlaybackState.Paused: MediaPlayerState.PAUSED,
PowerState.On: MediaPlayerState.ON,
PowerState.SystemUpdate: MediaPlayerState.OFF,
PowerState.ConnectedStandby: MediaPlayerState.OFF,
PowerState.Off: MediaPlayerState.OFF,
PowerState.Unknown: None,
}
@ -109,7 +109,7 @@ class XboxMediaPlayer(CoordinatorEntity[XboxUpdateCoordinator], MediaPlayerEntit
@property
def supported_features(self):
"""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 (
SUPPORT_XBOX
& ~MediaPlayerEntityFeature.NEXT_TRACK
@ -122,8 +122,8 @@ class XboxMediaPlayer(CoordinatorEntity[XboxUpdateCoordinator], MediaPlayerEntit
"""Media content type."""
app_details = self.data.app_details
if app_details and app_details.product_family == "Games":
return MEDIA_TYPE_GAME
return MEDIA_TYPE_APP
return MediaType.GAME
return MediaType.APP
@property
def media_title(self):

View File

@ -10,8 +10,9 @@ from homeassistant.components.media_player import (
PLATFORM_SCHEMA,
MediaPlayerEntity,
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
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -70,7 +71,7 @@ class XiaomiTV(MediaPlayerEntity):
self._tv = pymitv.TV(ip)
# Default name value, only to be overridden by user.
self._name = name
self._state = STATE_OFF
self._state = MediaPlayerState.OFF
@property
def name(self):
@ -95,17 +96,17 @@ class XiaomiTV(MediaPlayerEntity):
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.
"""
if self._state != STATE_OFF:
if self._state != MediaPlayerState.OFF:
self._tv.sleep()
self._state = STATE_OFF
self._state = MediaPlayerState.OFF
def turn_on(self) -> None:
"""Wake the TV back up from sleep."""
if self._state != STATE_ON:
if self._state != MediaPlayerState.ON:
self._tv.wake()
self._state = STATE_ON
self._state = MediaPlayerState.ON
def volume_up(self) -> None:
"""Increase volume by one."""

View File

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

View File

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

View File

@ -11,20 +11,15 @@ from aiomusiccast.features import ZoneFeature
from homeassistant.components import media_source
from homeassistant.components.media_player import (
BrowseMedia,
MediaClass,
MediaPlayerEntity,
MediaPlayerEntityFeature,
)
from homeassistant.components.media_player.browse_media import (
MediaPlayerState,
MediaType,
RepeatMode,
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.const import STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity import Entity
@ -80,11 +75,12 @@ async def async_setup_entry(
class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"""The musiccast media player."""
_attr_media_content_type = MediaType.MUSIC
_attr_should_poll = False
def __init__(self, zone_id, name, entry_id, coordinator):
"""Initialize the musiccast device."""
self._player_state = STATE_PLAYING
self._player_state = MediaPlayerState.PLAYING
self._volume_muted = False
self._shuffle = False
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._cur_track = 0
self._repeat = REPEAT_MODE_OFF
self._repeat = RepeatMode.OFF
async def async_added_to_hass(self) -> None:
"""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 None
@property
def media_content_type(self):
"""Return the content type of current playing media."""
return MEDIA_TYPE_MUSIC
@property
def state(self):
"""Return the state of the player."""
if self.coordinator.data.zones[self._zone_id].power == "on":
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":
return STATE_IDLE
return STATE_PLAYING
return STATE_OFF
return MediaPlayerState.IDLE
return MediaPlayerState.PLAYING
return MediaPlayerState.OFF
@property
def volume_level(self):
@ -281,7 +272,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
)
media_id = play_item.url
if self.state == STATE_OFF:
if self.state == MediaPlayerState.OFF:
await self.async_turn_on()
if media_id:
@ -324,7 +315,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
),
)
if self.state == STATE_OFF:
if self.state == MediaPlayerState.OFF:
raise HomeAssistantError(
"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):
if item.can_play:
return MEDIA_CLASS_TRACK
return MEDIA_CLASS_DIRECTORY
return MediaClass.TRACK
return MediaClass.DIRECTORY
children = [
BrowseMedia(
@ -429,7 +420,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
return (
MC_REPEAT_MODE_TO_HA_MAPPING.get(self.coordinator.data.netusb_repeat)
if self._is_netusb
else REPEAT_MODE_OFF
else RepeatMode.OFF
)
@property
@ -459,7 +450,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
supported_features |= MediaPlayerEntityFeature.PLAY
supported_features |= MediaPlayerEntityFeature.STOP
if self.state != STATE_OFF:
if self.state != MediaPlayerState.OFF:
supported_features |= MediaPlayerEntityFeature.BROWSE_MEDIA
return supported_features
@ -486,7 +477,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"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."""
if self._is_netusb:
await self.coordinator.musiccast.netusb_repeat(
@ -705,7 +696,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
if entity.entity_id in group_members
]
if self.state == STATE_OFF:
if self.state == MediaPlayerState.OFF:
await self.async_turn_on()
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.
_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()
if self.ip_address == server.ip_address:
if server.zone == DEFAULT_ZONE:

View File

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