Use new media player enums [i-l] (#78054)

pull/78088/head
epenet 2022-09-08 22:35:53 +02:00 committed by GitHub
parent 7937bfeedb
commit 823e7e8830
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 80 deletions

View File

@ -10,22 +10,10 @@ from homeassistant.components.media_player import (
PLATFORM_SCHEMA,
MediaPlayerEntity,
MediaPlayerEntityFeature,
MediaPlayerState,
MediaType,
)
from homeassistant.components.media_player.const import (
MEDIA_TYPE_MUSIC,
MEDIA_TYPE_PLAYLIST,
)
from homeassistant.const import (
CONF_HOST,
CONF_NAME,
CONF_PORT,
CONF_SSL,
STATE_IDLE,
STATE_OFF,
STATE_ON,
STATE_PAUSED,
STATE_PLAYING,
)
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_SSL
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -195,6 +183,7 @@ def setup_platform(
class ItunesDevice(MediaPlayerEntity):
"""Representation of an iTunes API instance."""
_attr_media_content_type = MediaType.MUSIC
_attr_supported_features = (
MediaPlayerEntityFeature.PAUSE
| MediaPlayerEntityFeature.VOLUME_SET
@ -263,12 +252,12 @@ class ItunesDevice(MediaPlayerEntity):
return "error"
if self.player_state == "stopped":
return STATE_IDLE
return MediaPlayerState.IDLE
if self.player_state == "paused":
return STATE_PAUSED
return MediaPlayerState.PAUSED
return STATE_PLAYING
return MediaPlayerState.PLAYING
def update(self) -> None:
"""Retrieve latest state."""
@ -312,16 +301,16 @@ class ItunesDevice(MediaPlayerEntity):
"""Content ID of current playing media."""
return self.content_id
@property
def media_content_type(self):
"""Content type of current playing media."""
return MEDIA_TYPE_MUSIC
@property
def media_image_url(self):
"""Image url of current playing media."""
if (
self.player_state in (STATE_PLAYING, STATE_IDLE, STATE_PAUSED)
self.player_state
in {
MediaPlayerState.PLAYING,
MediaPlayerState.IDLE,
MediaPlayerState.PAUSED,
}
and self.current_title is not None
):
return f"{self.client.artwork_url()}?id={self.content_id}"
@ -393,7 +382,7 @@ class ItunesDevice(MediaPlayerEntity):
def play_media(self, media_type: str, media_id: str, **kwargs: Any) -> None:
"""Send the play_media command to the media player."""
if media_type == MEDIA_TYPE_PLAYLIST:
if media_type == MediaType.PLAYLIST:
response = self.client.play_playlist(media_id)
self.update_state(response)
@ -406,6 +395,7 @@ class ItunesDevice(MediaPlayerEntity):
class AirPlayDevice(MediaPlayerEntity):
"""Representation an AirPlay device via an iTunes API instance."""
_attr_media_content_type = MediaType.MUSIC
_attr_supported_features = (
MediaPlayerEntityFeature.VOLUME_SET
| MediaPlayerEntityFeature.TURN_ON
@ -466,12 +456,12 @@ class AirPlayDevice(MediaPlayerEntity):
return "mdi:volume-off"
@property
def state(self):
def state(self) -> MediaPlayerState:
"""Return the state of the device."""
if self.selected is True:
return STATE_ON
return MediaPlayerState.ON
return STATE_OFF
return MediaPlayerState.OFF
def update(self) -> None:
"""Retrieve latest state."""
@ -481,11 +471,6 @@ class AirPlayDevice(MediaPlayerEntity):
"""Return the volume."""
return float(self.volume) / 100.0
@property
def media_content_type(self):
"""Flag of media content that is supported."""
return MEDIA_TYPE_MUSIC
def set_volume_level(self, volume: float) -> None:
"""Set volume level, range 0..1."""
volume = int(volume * 100)

View File

@ -9,8 +9,8 @@ from kaleidescape import const as kaleidescape_const
from homeassistant.components.media_player import (
MediaPlayerEntity,
MediaPlayerEntityFeature,
MediaPlayerState,
)
from homeassistant.const import STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING
from homeassistant.util.dt import utcnow
from .const import DOMAIN as KALEIDESCAPE_DOMAIN
@ -86,15 +86,15 @@ class KaleidescapeMediaPlayer(KaleidescapeEntity, MediaPlayerEntity):
await self._device.previous()
@property
def state(self) -> str:
def state(self) -> MediaPlayerState:
"""State of device."""
if self._device.power.state == kaleidescape_const.DEVICE_POWER_STATE_STANDBY:
return STATE_OFF
return MediaPlayerState.OFF
if self._device.movie.play_status in KALEIDESCAPE_PLAYING_STATES:
return STATE_PLAYING
return MediaPlayerState.PLAYING
if self._device.movie.play_status in KALEIDESCAPE_PAUSED_STATES:
return STATE_PAUSED
return STATE_IDLE
return MediaPlayerState.PAUSED
return MediaPlayerState.IDLE
@property
def available(self) -> bool:

View File

@ -15,15 +15,9 @@ from homeassistant.components.media_player import (
PLATFORM_SCHEMA,
MediaPlayerEntity,
MediaPlayerEntityFeature,
MediaPlayerState,
)
from homeassistant.const import (
CONF_HOST,
CONF_NAME,
CONF_PORT,
CONF_TYPE,
STATE_OFF,
STATE_ON,
)
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_TYPE
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers import config_validation as cv, entity_platform
@ -265,7 +259,9 @@ class KefMediaPlayer(MediaPlayerEntity):
) = await self._speaker.get_volume_and_is_muted()
state = await self._speaker.get_state()
self._source = state.source
self._state = STATE_ON if state.is_on else STATE_OFF
self._state = (
MediaPlayerState.ON if state.is_on else MediaPlayerState.OFF
)
if self._dsp is None:
# Only do this when necessary because it is a slow operation
await self.update_dsp()
@ -273,7 +269,7 @@ class KefMediaPlayer(MediaPlayerEntity):
self._muted = None
self._source = None
self._volume = None
self._state = STATE_OFF
self._state = MediaPlayerState.OFF
except (ConnectionError, TimeoutError) as err:
_LOGGER.debug("Error in `update`: %s", err)
self._state = None
@ -367,7 +363,7 @@ class KefMediaPlayer(MediaPlayerEntity):
async def update_dsp(self, _=None) -> None:
"""Update the DSP settings."""
if self._speaker_type == "LS50" and self._state == STATE_OFF:
if self._speaker_type == "LS50" and self._state == MediaPlayerState.OFF:
# The LSX is able to respond when off the LS50 has to be on.
return

View File

@ -13,16 +13,10 @@ from homeassistant.components.media_player import (
MediaPlayerDeviceClass,
MediaPlayerEntity,
MediaPlayerEntityFeature,
MediaPlayerState,
MediaType,
)
from homeassistant.components.media_player.const import MEDIA_TYPE_CHANNEL
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_HOST,
CONF_NAME,
STATE_OFF,
STATE_PAUSED,
STATE_PLAYING,
)
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, CONF_NAME
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -81,6 +75,7 @@ class LgTVDevice(MediaPlayerEntity):
"""Representation of a LG TV."""
_attr_device_class = MediaPlayerDeviceClass.TV
_attr_media_content_type = MediaType.CHANNEL
def __init__(self, client, name, on_action_script):
"""Initialize the LG TV device."""
@ -105,14 +100,14 @@ class LgTVDevice(MediaPlayerEntity):
with self._client as client:
client.send_command(command)
except (LgNetCastError, RequestException):
self._state = STATE_OFF
self._state = MediaPlayerState.OFF
def update(self) -> None:
"""Retrieve the latest data from the LG TV."""
try:
with self._client as client:
self._state = STATE_PLAYING
self._state = MediaPlayerState.PLAYING
self.__update_volume()
@ -147,7 +142,7 @@ class LgTVDevice(MediaPlayerEntity):
)
self._source_names = [n for n, k in sorted_sources]
except (LgNetCastError, RequestException):
self._state = STATE_OFF
self._state = MediaPlayerState.OFF
def __update_volume(self):
volume_info = self._client.get_volume()
@ -191,11 +186,6 @@ class LgTVDevice(MediaPlayerEntity):
"""Content id of current playing media."""
return self._channel_id
@property
def media_content_type(self):
"""Content type of current playing media."""
return MEDIA_TYPE_CHANNEL
@property
def media_channel(self):
"""Channel currently playing."""
@ -259,13 +249,13 @@ class LgTVDevice(MediaPlayerEntity):
def media_play(self) -> None:
"""Send play command."""
self._playing = True
self._state = STATE_PLAYING
self._state = MediaPlayerState.PLAYING
self.send_command(33)
def media_pause(self) -> None:
"""Send media pause command to media player."""
self._playing = False
self._state = STATE_PAUSED
self._state = MediaPlayerState.PAUSED
self.send_command(34)
def media_next_track(self) -> None:
@ -278,7 +268,7 @@ class LgTVDevice(MediaPlayerEntity):
def play_media(self, media_type: str, media_id: str, **kwargs: Any) -> None:
"""Tune to channel."""
if media_type != MEDIA_TYPE_CHANNEL:
if media_type != MediaType.CHANNEL:
raise ValueError(f"Invalid media type: {media_type}")
for name, channel in self._sources.items():

View File

@ -6,9 +6,10 @@ import temescal
from homeassistant.components.media_player import (
MediaPlayerEntity,
MediaPlayerEntityFeature,
MediaPlayerState,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT, STATE_ON
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -34,6 +35,7 @@ class LGDevice(MediaPlayerEntity):
"""Representation of an LG soundbar device."""
_attr_should_poll = False
_attr_state = MediaPlayerState.ON
_attr_supported_features = (
MediaPlayerEntityFeature.VOLUME_SET
| MediaPlayerEntityFeature.VOLUME_MUTE
@ -145,11 +147,6 @@ class LGDevice(MediaPlayerEntity):
"""Boolean if volume is currently muted."""
return self._mute
@property
def state(self):
"""Return the state of the device."""
return STATE_ON
@property
def sound_mode(self):
"""Return the current sound mode."""

View File

@ -9,9 +9,10 @@ from homeassistant.components.media_player import (
MediaPlayerDeviceClass,
MediaPlayerEntity,
MediaPlayerEntityFeature,
MediaPlayerState,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_ON, STATE_STANDBY, Platform
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -112,13 +113,13 @@ class LookinMedia(LookinPowerPushRemoteEntity, MediaPlayerEntity):
async def async_turn_off(self) -> None:
"""Turn the media player off."""
await self._async_send_command(self._power_off_command)
self._attr_state = STATE_STANDBY
self._attr_state = MediaPlayerState.STANDBY
self.async_write_ha_state()
async def async_turn_on(self) -> None:
"""Turn the media player on."""
await self._async_send_command(self._power_on_command)
self._attr_state = STATE_ON
self._attr_state = MediaPlayerState.ON
self.async_write_ha_state()
def _update_from_status(self, status: str) -> None:
@ -135,5 +136,7 @@ class LookinMedia(LookinPowerPushRemoteEntity, MediaPlayerEntity):
state = status[0]
mute = status[2]
self._attr_state = STATE_ON if state == "1" else STATE_STANDBY
self._attr_state = (
MediaPlayerState.ON if state == "1" else MediaPlayerState.STANDBY
)
self._attr_is_volume_muted = mute == "0"