mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 02:37:08 +00:00
Migrate esphome media_player platform to use _on_static_info_update (#95071)
This commit is contained in:
parent
1cf4a008c3
commit
dd0e6d6481
@ -4,6 +4,7 @@ from __future__ import annotations
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from aioesphomeapi import (
|
from aioesphomeapi import (
|
||||||
|
EntityInfo,
|
||||||
MediaPlayerCommand,
|
MediaPlayerCommand,
|
||||||
MediaPlayerEntityState,
|
MediaPlayerEntityState,
|
||||||
MediaPlayerInfo,
|
MediaPlayerInfo,
|
||||||
@ -21,7 +22,7 @@ from homeassistant.components.media_player import (
|
|||||||
async_process_play_media_url,
|
async_process_play_media_url,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
|
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
|
||||||
@ -61,6 +62,21 @@ class EsphomeMediaPlayer(
|
|||||||
|
|
||||||
_attr_device_class = MediaPlayerDeviceClass.SPEAKER
|
_attr_device_class = MediaPlayerDeviceClass.SPEAKER
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _on_static_info_update(self, static_info: EntityInfo) -> None:
|
||||||
|
"""Set attrs from static info."""
|
||||||
|
super()._on_static_info_update(static_info)
|
||||||
|
flags = (
|
||||||
|
MediaPlayerEntityFeature.PLAY_MEDIA
|
||||||
|
| MediaPlayerEntityFeature.BROWSE_MEDIA
|
||||||
|
| MediaPlayerEntityFeature.STOP
|
||||||
|
| MediaPlayerEntityFeature.VOLUME_SET
|
||||||
|
| MediaPlayerEntityFeature.VOLUME_MUTE
|
||||||
|
)
|
||||||
|
if self._static_info.supports_pause:
|
||||||
|
flags |= MediaPlayerEntityFeature.PAUSE | MediaPlayerEntityFeature.PLAY
|
||||||
|
self._attr_supported_features = flags
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@esphome_state_property
|
@esphome_state_property
|
||||||
def state(self) -> MediaPlayerState | None:
|
def state(self) -> MediaPlayerState | None:
|
||||||
@ -79,20 +95,6 @@ class EsphomeMediaPlayer(
|
|||||||
"""Volume level of the media player (0..1)."""
|
"""Volume level of the media player (0..1)."""
|
||||||
return self._state.volume
|
return self._state.volume
|
||||||
|
|
||||||
@property
|
|
||||||
def supported_features(self) -> MediaPlayerEntityFeature:
|
|
||||||
"""Flag supported features."""
|
|
||||||
flags = (
|
|
||||||
MediaPlayerEntityFeature.PLAY_MEDIA
|
|
||||||
| MediaPlayerEntityFeature.BROWSE_MEDIA
|
|
||||||
| MediaPlayerEntityFeature.STOP
|
|
||||||
| MediaPlayerEntityFeature.VOLUME_SET
|
|
||||||
| MediaPlayerEntityFeature.VOLUME_MUTE
|
|
||||||
)
|
|
||||||
if self._static_info.supports_pause:
|
|
||||||
flags |= MediaPlayerEntityFeature.PAUSE | MediaPlayerEntityFeature.PLAY
|
|
||||||
return flags
|
|
||||||
|
|
||||||
async def async_play_media(
|
async def async_play_media(
|
||||||
self, media_type: MediaType | str, media_id: str, **kwargs: Any
|
self, media_type: MediaType | str, media_id: str, **kwargs: Any
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -106,7 +108,7 @@ class EsphomeMediaPlayer(
|
|||||||
media_id = async_process_play_media_url(self.hass, media_id)
|
media_id = async_process_play_media_url(self.hass, media_id)
|
||||||
|
|
||||||
await self._client.media_player_command(
|
await self._client.media_player_command(
|
||||||
self._static_info.key,
|
self._key,
|
||||||
media_url=media_id,
|
media_url=media_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -124,35 +126,29 @@ class EsphomeMediaPlayer(
|
|||||||
|
|
||||||
async def async_set_volume_level(self, volume: float) -> None:
|
async def async_set_volume_level(self, volume: float) -> None:
|
||||||
"""Set volume level, range 0..1."""
|
"""Set volume level, range 0..1."""
|
||||||
await self._client.media_player_command(
|
await self._client.media_player_command(self._key, volume=volume)
|
||||||
self._static_info.key,
|
|
||||||
volume=volume,
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_media_pause(self) -> None:
|
async def async_media_pause(self) -> None:
|
||||||
"""Send pause command."""
|
"""Send pause command."""
|
||||||
await self._client.media_player_command(
|
await self._client.media_player_command(
|
||||||
self._static_info.key,
|
self._key, command=MediaPlayerCommand.PAUSE
|
||||||
command=MediaPlayerCommand.PAUSE,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_media_play(self) -> None:
|
async def async_media_play(self) -> None:
|
||||||
"""Send play command."""
|
"""Send play command."""
|
||||||
await self._client.media_player_command(
|
await self._client.media_player_command(
|
||||||
self._static_info.key,
|
self._key, command=MediaPlayerCommand.PLAY
|
||||||
command=MediaPlayerCommand.PLAY,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_media_stop(self) -> None:
|
async def async_media_stop(self) -> None:
|
||||||
"""Send stop command."""
|
"""Send stop command."""
|
||||||
await self._client.media_player_command(
|
await self._client.media_player_command(
|
||||||
self._static_info.key,
|
self._key, command=MediaPlayerCommand.STOP
|
||||||
command=MediaPlayerCommand.STOP,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_mute_volume(self, mute: bool) -> None:
|
async def async_mute_volume(self, mute: bool) -> None:
|
||||||
"""Mute the volume."""
|
"""Mute the volume."""
|
||||||
await self._client.media_player_command(
|
await self._client.media_player_command(
|
||||||
self._static_info.key,
|
self._key,
|
||||||
command=MediaPlayerCommand.MUTE if mute else MediaPlayerCommand.UNMUTE,
|
command=MediaPlayerCommand.MUTE if mute else MediaPlayerCommand.UNMUTE,
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user