mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Use new media player enums [i-l] (#78054)
This commit is contained in:
parent
7937bfeedb
commit
823e7e8830
@ -10,22 +10,10 @@ from homeassistant.components.media_player import (
|
|||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
MediaPlayerEntity,
|
MediaPlayerEntity,
|
||||||
MediaPlayerEntityFeature,
|
MediaPlayerEntityFeature,
|
||||||
|
MediaPlayerState,
|
||||||
|
MediaType,
|
||||||
)
|
)
|
||||||
from homeassistant.components.media_player.const import (
|
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_SSL
|
||||||
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.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
|
||||||
@ -195,6 +183,7 @@ def setup_platform(
|
|||||||
class ItunesDevice(MediaPlayerEntity):
|
class ItunesDevice(MediaPlayerEntity):
|
||||||
"""Representation of an iTunes API instance."""
|
"""Representation of an iTunes API instance."""
|
||||||
|
|
||||||
|
_attr_media_content_type = MediaType.MUSIC
|
||||||
_attr_supported_features = (
|
_attr_supported_features = (
|
||||||
MediaPlayerEntityFeature.PAUSE
|
MediaPlayerEntityFeature.PAUSE
|
||||||
| MediaPlayerEntityFeature.VOLUME_SET
|
| MediaPlayerEntityFeature.VOLUME_SET
|
||||||
@ -263,12 +252,12 @@ class ItunesDevice(MediaPlayerEntity):
|
|||||||
return "error"
|
return "error"
|
||||||
|
|
||||||
if self.player_state == "stopped":
|
if self.player_state == "stopped":
|
||||||
return STATE_IDLE
|
return MediaPlayerState.IDLE
|
||||||
|
|
||||||
if self.player_state == "paused":
|
if self.player_state == "paused":
|
||||||
return STATE_PAUSED
|
return MediaPlayerState.PAUSED
|
||||||
|
|
||||||
return STATE_PLAYING
|
return MediaPlayerState.PLAYING
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Retrieve latest state."""
|
"""Retrieve latest state."""
|
||||||
@ -312,16 +301,16 @@ class ItunesDevice(MediaPlayerEntity):
|
|||||||
"""Content ID of current playing media."""
|
"""Content ID of current playing media."""
|
||||||
return self.content_id
|
return self.content_id
|
||||||
|
|
||||||
@property
|
|
||||||
def media_content_type(self):
|
|
||||||
"""Content type of current playing media."""
|
|
||||||
return MEDIA_TYPE_MUSIC
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def media_image_url(self):
|
def media_image_url(self):
|
||||||
"""Image url of current playing media."""
|
"""Image url of current playing media."""
|
||||||
if (
|
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
|
and self.current_title is not None
|
||||||
):
|
):
|
||||||
return f"{self.client.artwork_url()}?id={self.content_id}"
|
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:
|
def play_media(self, media_type: 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 == MEDIA_TYPE_PLAYLIST:
|
if media_type == MediaType.PLAYLIST:
|
||||||
response = self.client.play_playlist(media_id)
|
response = self.client.play_playlist(media_id)
|
||||||
self.update_state(response)
|
self.update_state(response)
|
||||||
|
|
||||||
@ -406,6 +395,7 @@ class ItunesDevice(MediaPlayerEntity):
|
|||||||
class AirPlayDevice(MediaPlayerEntity):
|
class AirPlayDevice(MediaPlayerEntity):
|
||||||
"""Representation an AirPlay device via an iTunes API instance."""
|
"""Representation an AirPlay device via an iTunes API instance."""
|
||||||
|
|
||||||
|
_attr_media_content_type = MediaType.MUSIC
|
||||||
_attr_supported_features = (
|
_attr_supported_features = (
|
||||||
MediaPlayerEntityFeature.VOLUME_SET
|
MediaPlayerEntityFeature.VOLUME_SET
|
||||||
| MediaPlayerEntityFeature.TURN_ON
|
| MediaPlayerEntityFeature.TURN_ON
|
||||||
@ -466,12 +456,12 @@ class AirPlayDevice(MediaPlayerEntity):
|
|||||||
return "mdi:volume-off"
|
return "mdi:volume-off"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self) -> MediaPlayerState:
|
||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
if self.selected is True:
|
if self.selected is True:
|
||||||
return STATE_ON
|
return MediaPlayerState.ON
|
||||||
|
|
||||||
return STATE_OFF
|
return MediaPlayerState.OFF
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Retrieve latest state."""
|
"""Retrieve latest state."""
|
||||||
@ -481,11 +471,6 @@ class AirPlayDevice(MediaPlayerEntity):
|
|||||||
"""Return the volume."""
|
"""Return the volume."""
|
||||||
return float(self.volume) / 100.0
|
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:
|
def set_volume_level(self, volume: float) -> None:
|
||||||
"""Set volume level, range 0..1."""
|
"""Set volume level, range 0..1."""
|
||||||
volume = int(volume * 100)
|
volume = int(volume * 100)
|
||||||
|
@ -9,8 +9,8 @@ from kaleidescape import const as kaleidescape_const
|
|||||||
from homeassistant.components.media_player import (
|
from homeassistant.components.media_player import (
|
||||||
MediaPlayerEntity,
|
MediaPlayerEntity,
|
||||||
MediaPlayerEntityFeature,
|
MediaPlayerEntityFeature,
|
||||||
|
MediaPlayerState,
|
||||||
)
|
)
|
||||||
from homeassistant.const import STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING
|
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
from .const import DOMAIN as KALEIDESCAPE_DOMAIN
|
from .const import DOMAIN as KALEIDESCAPE_DOMAIN
|
||||||
@ -86,15 +86,15 @@ class KaleidescapeMediaPlayer(KaleidescapeEntity, MediaPlayerEntity):
|
|||||||
await self._device.previous()
|
await self._device.previous()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> str:
|
def state(self) -> MediaPlayerState:
|
||||||
"""State of device."""
|
"""State of device."""
|
||||||
if self._device.power.state == kaleidescape_const.DEVICE_POWER_STATE_STANDBY:
|
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:
|
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:
|
if self._device.movie.play_status in KALEIDESCAPE_PAUSED_STATES:
|
||||||
return STATE_PAUSED
|
return MediaPlayerState.PAUSED
|
||||||
return STATE_IDLE
|
return MediaPlayerState.IDLE
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
|
@ -15,15 +15,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_PORT, CONF_TYPE
|
||||||
CONF_HOST,
|
|
||||||
CONF_NAME,
|
|
||||||
CONF_PORT,
|
|
||||||
CONF_TYPE,
|
|
||||||
STATE_OFF,
|
|
||||||
STATE_ON,
|
|
||||||
)
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import PlatformNotReady
|
from homeassistant.exceptions import PlatformNotReady
|
||||||
from homeassistant.helpers import config_validation as cv, entity_platform
|
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()
|
) = await self._speaker.get_volume_and_is_muted()
|
||||||
state = await self._speaker.get_state()
|
state = await self._speaker.get_state()
|
||||||
self._source = state.source
|
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:
|
if self._dsp is None:
|
||||||
# Only do this when necessary because it is a slow operation
|
# Only do this when necessary because it is a slow operation
|
||||||
await self.update_dsp()
|
await self.update_dsp()
|
||||||
@ -273,7 +269,7 @@ class KefMediaPlayer(MediaPlayerEntity):
|
|||||||
self._muted = None
|
self._muted = None
|
||||||
self._source = None
|
self._source = None
|
||||||
self._volume = None
|
self._volume = None
|
||||||
self._state = STATE_OFF
|
self._state = MediaPlayerState.OFF
|
||||||
except (ConnectionError, TimeoutError) as err:
|
except (ConnectionError, TimeoutError) as err:
|
||||||
_LOGGER.debug("Error in `update`: %s", err)
|
_LOGGER.debug("Error in `update`: %s", err)
|
||||||
self._state = None
|
self._state = None
|
||||||
@ -367,7 +363,7 @@ class KefMediaPlayer(MediaPlayerEntity):
|
|||||||
|
|
||||||
async def update_dsp(self, _=None) -> None:
|
async def update_dsp(self, _=None) -> None:
|
||||||
"""Update the DSP settings."""
|
"""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.
|
# The LSX is able to respond when off the LS50 has to be on.
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -13,16 +13,10 @@ from homeassistant.components.media_player import (
|
|||||||
MediaPlayerDeviceClass,
|
MediaPlayerDeviceClass,
|
||||||
MediaPlayerEntity,
|
MediaPlayerEntity,
|
||||||
MediaPlayerEntityFeature,
|
MediaPlayerEntityFeature,
|
||||||
|
MediaPlayerState,
|
||||||
|
MediaType,
|
||||||
)
|
)
|
||||||
from homeassistant.components.media_player.const import MEDIA_TYPE_CHANNEL
|
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, CONF_NAME
|
||||||
from homeassistant.const import (
|
|
||||||
CONF_ACCESS_TOKEN,
|
|
||||||
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
|
||||||
@ -81,6 +75,7 @@ class LgTVDevice(MediaPlayerEntity):
|
|||||||
"""Representation of a LG TV."""
|
"""Representation of a LG TV."""
|
||||||
|
|
||||||
_attr_device_class = MediaPlayerDeviceClass.TV
|
_attr_device_class = MediaPlayerDeviceClass.TV
|
||||||
|
_attr_media_content_type = MediaType.CHANNEL
|
||||||
|
|
||||||
def __init__(self, client, name, on_action_script):
|
def __init__(self, client, name, on_action_script):
|
||||||
"""Initialize the LG TV device."""
|
"""Initialize the LG TV device."""
|
||||||
@ -105,14 +100,14 @@ class LgTVDevice(MediaPlayerEntity):
|
|||||||
with self._client as client:
|
with self._client as client:
|
||||||
client.send_command(command)
|
client.send_command(command)
|
||||||
except (LgNetCastError, RequestException):
|
except (LgNetCastError, RequestException):
|
||||||
self._state = STATE_OFF
|
self._state = MediaPlayerState.OFF
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Retrieve the latest data from the LG TV."""
|
"""Retrieve the latest data from the LG TV."""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with self._client as client:
|
with self._client as client:
|
||||||
self._state = STATE_PLAYING
|
self._state = MediaPlayerState.PLAYING
|
||||||
|
|
||||||
self.__update_volume()
|
self.__update_volume()
|
||||||
|
|
||||||
@ -147,7 +142,7 @@ class LgTVDevice(MediaPlayerEntity):
|
|||||||
)
|
)
|
||||||
self._source_names = [n for n, k in sorted_sources]
|
self._source_names = [n for n, k in sorted_sources]
|
||||||
except (LgNetCastError, RequestException):
|
except (LgNetCastError, RequestException):
|
||||||
self._state = STATE_OFF
|
self._state = MediaPlayerState.OFF
|
||||||
|
|
||||||
def __update_volume(self):
|
def __update_volume(self):
|
||||||
volume_info = self._client.get_volume()
|
volume_info = self._client.get_volume()
|
||||||
@ -191,11 +186,6 @@ class LgTVDevice(MediaPlayerEntity):
|
|||||||
"""Content id of current playing media."""
|
"""Content id of current playing media."""
|
||||||
return self._channel_id
|
return self._channel_id
|
||||||
|
|
||||||
@property
|
|
||||||
def media_content_type(self):
|
|
||||||
"""Content type of current playing media."""
|
|
||||||
return MEDIA_TYPE_CHANNEL
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def media_channel(self):
|
def media_channel(self):
|
||||||
"""Channel currently playing."""
|
"""Channel currently playing."""
|
||||||
@ -259,13 +249,13 @@ class LgTVDevice(MediaPlayerEntity):
|
|||||||
def media_play(self) -> None:
|
def media_play(self) -> None:
|
||||||
"""Send play command."""
|
"""Send play command."""
|
||||||
self._playing = True
|
self._playing = True
|
||||||
self._state = STATE_PLAYING
|
self._state = MediaPlayerState.PLAYING
|
||||||
self.send_command(33)
|
self.send_command(33)
|
||||||
|
|
||||||
def media_pause(self) -> None:
|
def media_pause(self) -> None:
|
||||||
"""Send media pause command to media player."""
|
"""Send media pause command to media player."""
|
||||||
self._playing = False
|
self._playing = False
|
||||||
self._state = STATE_PAUSED
|
self._state = MediaPlayerState.PAUSED
|
||||||
self.send_command(34)
|
self.send_command(34)
|
||||||
|
|
||||||
def media_next_track(self) -> None:
|
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:
|
def play_media(self, media_type: str, media_id: str, **kwargs: Any) -> None:
|
||||||
"""Tune to channel."""
|
"""Tune to channel."""
|
||||||
if media_type != MEDIA_TYPE_CHANNEL:
|
if media_type != MediaType.CHANNEL:
|
||||||
raise ValueError(f"Invalid media type: {media_type}")
|
raise ValueError(f"Invalid media type: {media_type}")
|
||||||
|
|
||||||
for name, channel in self._sources.items():
|
for name, channel in self._sources.items():
|
||||||
|
@ -6,9 +6,10 @@ import temescal
|
|||||||
from homeassistant.components.media_player import (
|
from homeassistant.components.media_player import (
|
||||||
MediaPlayerEntity,
|
MediaPlayerEntity,
|
||||||
MediaPlayerEntityFeature,
|
MediaPlayerEntityFeature,
|
||||||
|
MediaPlayerState,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
@ -34,6 +35,7 @@ class LGDevice(MediaPlayerEntity):
|
|||||||
"""Representation of an LG soundbar device."""
|
"""Representation of an LG soundbar device."""
|
||||||
|
|
||||||
_attr_should_poll = False
|
_attr_should_poll = False
|
||||||
|
_attr_state = MediaPlayerState.ON
|
||||||
_attr_supported_features = (
|
_attr_supported_features = (
|
||||||
MediaPlayerEntityFeature.VOLUME_SET
|
MediaPlayerEntityFeature.VOLUME_SET
|
||||||
| MediaPlayerEntityFeature.VOLUME_MUTE
|
| MediaPlayerEntityFeature.VOLUME_MUTE
|
||||||
@ -145,11 +147,6 @@ class LGDevice(MediaPlayerEntity):
|
|||||||
"""Boolean if volume is currently muted."""
|
"""Boolean if volume is currently muted."""
|
||||||
return self._mute
|
return self._mute
|
||||||
|
|
||||||
@property
|
|
||||||
def state(self):
|
|
||||||
"""Return the state of the device."""
|
|
||||||
return STATE_ON
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def sound_mode(self):
|
def sound_mode(self):
|
||||||
"""Return the current sound mode."""
|
"""Return the current sound mode."""
|
||||||
|
@ -9,9 +9,10 @@ from homeassistant.components.media_player import (
|
|||||||
MediaPlayerDeviceClass,
|
MediaPlayerDeviceClass,
|
||||||
MediaPlayerEntity,
|
MediaPlayerEntity,
|
||||||
MediaPlayerEntityFeature,
|
MediaPlayerEntityFeature,
|
||||||
|
MediaPlayerState,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
@ -112,13 +113,13 @@ class LookinMedia(LookinPowerPushRemoteEntity, MediaPlayerEntity):
|
|||||||
async def async_turn_off(self) -> None:
|
async def async_turn_off(self) -> None:
|
||||||
"""Turn the media player off."""
|
"""Turn the media player off."""
|
||||||
await self._async_send_command(self._power_off_command)
|
await self._async_send_command(self._power_off_command)
|
||||||
self._attr_state = STATE_STANDBY
|
self._attr_state = MediaPlayerState.STANDBY
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_turn_on(self) -> None:
|
async def async_turn_on(self) -> None:
|
||||||
"""Turn the media player on."""
|
"""Turn the media player on."""
|
||||||
await self._async_send_command(self._power_on_command)
|
await self._async_send_command(self._power_on_command)
|
||||||
self._attr_state = STATE_ON
|
self._attr_state = MediaPlayerState.ON
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
def _update_from_status(self, status: str) -> None:
|
def _update_from_status(self, status: str) -> None:
|
||||||
@ -135,5 +136,7 @@ class LookinMedia(LookinPowerPushRemoteEntity, MediaPlayerEntity):
|
|||||||
state = status[0]
|
state = status[0]
|
||||||
mute = status[2]
|
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"
|
self._attr_is_volume_muted = mute == "0"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user