1 """Denon HEOS Media Player."""
3 from __future__
import annotations
5 from collections.abc
import Awaitable, Callable, Coroutine
6 from functools
import reduce, wraps
8 from operator
import ior
11 from pyheos
import HeosError, const
as heos_const
16 DOMAIN
as MEDIA_PLAYER_DOMAIN,
20 MediaPlayerEntityFeature,
23 async_process_play_media_url,
29 async_dispatcher_connect,
30 async_dispatcher_send,
39 DOMAIN
as HEOS_DOMAIN,
40 SIGNAL_HEOS_PLAYER_ADDED,
44 BASE_SUPPORTED_FEATURES = (
45 MediaPlayerEntityFeature.VOLUME_MUTE
46 | MediaPlayerEntityFeature.VOLUME_SET
47 | MediaPlayerEntityFeature.VOLUME_STEP
48 | MediaPlayerEntityFeature.CLEAR_PLAYLIST
49 | MediaPlayerEntityFeature.SHUFFLE_SET
50 | MediaPlayerEntityFeature.SELECT_SOURCE
51 | MediaPlayerEntityFeature.PLAY_MEDIA
52 | MediaPlayerEntityFeature.GROUPING
53 | MediaPlayerEntityFeature.BROWSE_MEDIA
54 | MediaPlayerEntityFeature.MEDIA_ENQUEUE
57 PLAY_STATE_TO_STATE = {
58 heos_const.PLAY_STATE_PLAY: MediaPlayerState.PLAYING,
59 heos_const.PLAY_STATE_STOP: MediaPlayerState.IDLE,
60 heos_const.PLAY_STATE_PAUSE: MediaPlayerState.PAUSED,
63 CONTROL_TO_SUPPORT = {
64 heos_const.CONTROL_PLAY: MediaPlayerEntityFeature.PLAY,
65 heos_const.CONTROL_PAUSE: MediaPlayerEntityFeature.PAUSE,
66 heos_const.CONTROL_STOP: MediaPlayerEntityFeature.STOP,
67 heos_const.CONTROL_PLAY_PREVIOUS: MediaPlayerEntityFeature.PREVIOUS_TRACK,
68 heos_const.CONTROL_PLAY_NEXT: MediaPlayerEntityFeature.NEXT_TRACK,
71 HA_HEOS_ENQUEUE_MAP = {
72 None: heos_const.ADD_QUEUE_REPLACE_AND_PLAY,
73 MediaPlayerEnqueue.ADD: heos_const.ADD_QUEUE_ADD_TO_END,
74 MediaPlayerEnqueue.REPLACE: heos_const.ADD_QUEUE_REPLACE_AND_PLAY,
75 MediaPlayerEnqueue.NEXT: heos_const.ADD_QUEUE_PLAY_NEXT,
76 MediaPlayerEnqueue.PLAY: heos_const.ADD_QUEUE_PLAY_NOW,
79 _LOGGER = logging.getLogger(__name__)
83 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
85 """Add media players for a config entry."""
86 players = hass.data[HEOS_DOMAIN][MEDIA_PLAYER_DOMAIN]
91 type _FuncType[**_P] = Callable[_P, Awaitable[Any]]
92 type _ReturnFuncType[**_P] = Callable[_P, Coroutine[Any, Any,
None]]
95 def log_command_error[**_P](
97 ) -> Callable[[_FuncType[_P]], _ReturnFuncType[_P]]:
98 """Return decorator that logs command failure."""
100 def decorator(func: _FuncType[_P]) -> _ReturnFuncType[_P]:
102 async
def wrapper(*args: _P.args, **kwargs: _P.kwargs) ->
None:
104 await func(*args, **kwargs)
105 except (HeosError, ValueError)
as ex:
106 _LOGGER.error(
"Unable to %s: %s", command, ex)
114 """The HEOS player."""
116 _attr_media_content_type = MediaType.MUSIC
117 _attr_should_poll =
False
118 _attr_supported_features = BASE_SUPPORTED_FEATURES
119 _attr_media_image_remotely_accessible =
True
120 _attr_has_entity_name =
True
132 identifiers={(HEOS_DOMAIN, player.player_id)},
136 sw_version=player.version,
140 """Handle player attribute updated."""
141 if self.
_player_player.player_id != player_id:
143 if event == heos_const.EVENT_PLAYER_NOW_PLAYING_PROGRESS:
148 """Handle sources changed."""
152 """Device added to hass."""
155 self.
_player_player.heos.dispatcher.connect(
164 self.
hasshass.data[HEOS_DOMAIN][DATA_ENTITY_ID_MAP][self.
_player_player.player_id] = (
169 @log_command_error("clear playlist")
171 """Clear players playlist."""
172 await self.
_player_player.clear_queue()
174 @log_command_error("join_players")
176 """Join `group_members` as a player group with the current player."""
179 @log_command_error("pause")
181 """Send pause command."""
182 await self.
_player_player.pause()
184 @log_command_error("play")
186 """Send play command."""
187 await self.
_player_player.play()
189 @log_command_error("move to previous track")
191 """Send previous track command."""
192 await self.
_player_player.play_previous()
194 @log_command_error("move to next track")
196 """Send next track command."""
197 await self.
_player_player.play_next()
199 @log_command_error("stop")
201 """Send stop command."""
202 await self.
_player_player.stop()
204 @log_command_error("set mute")
206 """Mute the volume."""
207 await self.
_player_player.set_mute(mute)
209 @log_command_error("play media")
211 self, media_type: MediaType | str, media_id: str, **kwargs: Any
213 """Play a piece of media."""
214 if media_source.is_media_source_id(media_id):
215 media_type = MediaType.URL
216 play_item = await media_source.async_resolve_media(
219 media_id = play_item.url
221 if media_type
in {MediaType.URL, MediaType.MUSIC}:
224 await self.
_player_player.play_url(media_id)
227 if media_type ==
"quick_select":
229 selects = await self.
_player_player.get_quick_selects()
231 index: int |
None =
int(media_id)
235 (index
for index, select
in selects.items()
if select == media_id),
239 raise ValueError(f
"Invalid quick select '{media_id}'")
240 await self.
_player_player.play_quick_select(index)
243 if media_type == MediaType.PLAYLIST:
244 playlists = await self.
_player_player.heos.get_playlists()
245 playlist = next((p
for p
in playlists
if p.name == media_id),
None)
247 raise ValueError(f
"Invalid playlist '{media_id}'")
248 add_queue_option = HA_HEOS_ENQUEUE_MAP.get(kwargs.get(ATTR_MEDIA_ENQUEUE))
250 await self.
_player_player.add_to_queue(playlist, add_queue_option)
253 if media_type ==
"favorite":
256 index =
int(media_id)
262 for index, favorite
in self.
_source_manager_source_manager.favorites.items()
263 if favorite.name == media_id
268 raise ValueError(f
"Invalid favorite '{media_id}'")
269 await self.
_player_player.play_favorite(index)
272 raise ValueError(f
"Unsupported media type '{media_type}'")
274 @log_command_error("select source")
276 """Select input source."""
279 @log_command_error("set shuffle")
281 """Enable/disable shuffle mode."""
282 await self.
_player_player.set_play_mode(self.
_player_player.repeat, shuffle)
284 @log_command_error("set volume level")
286 """Set volume level, range 0..1."""
287 await self.
_player_player.set_volume(
int(volume * 100))
290 """Update supported features of the player."""
291 controls = self.
_player_player.now_playing_media.supported_controls
292 current_support = [CONTROL_TO_SUPPORT[control]
for control
in controls]
294 ior, current_support, BASE_SUPPORTED_FEATURES
298 self.
_group_manager_group_manager = self.
hasshass.data[HEOS_DOMAIN][DATA_GROUP_MANAGER]
303 @log_command_error("unjoin_player")
305 """Remove this player from any group."""
309 """Disconnect the device when removed."""
310 for signal_remove
in self.
_signals_signals:
316 """Return True if the device is available."""
317 return self.
_player_player.available
321 """Get additional attribute about the state."""
323 "media_album_id": self.
_player_player.now_playing_media.album_id,
324 "media_queue_id": self.
_player_player.now_playing_media.queue_id,
325 "media_source_id": self.
_player_player.now_playing_media.source_id,
326 "media_station": self.
_player_player.now_playing_media.station,
327 "media_type": self.
_player_player.now_playing_media.type,
332 """List of players which are grouped together."""
337 """Boolean if volume is currently muted."""
338 return self.
_player_player.is_muted
342 """Album name of current playing media, music track only."""
343 return self.
_player_player.now_playing_media.album
347 """Artist of current playing media, music track only."""
348 return self.
_player_player.now_playing_media.artist
352 """Content ID of current playing media."""
353 return self.
_player_player.now_playing_media.media_id
357 """Duration of current playing media in seconds."""
358 duration = self.
_player_player.now_playing_media.duration
359 if isinstance(duration, int):
360 return duration / 1000
365 """Position of current playing media in seconds."""
367 if not self.
_player_player.now_playing_media.duration:
369 return self.
_player_player.now_playing_media.current_position / 1000
373 """When was the position of the current playing media valid."""
375 if not self.
_player_player.now_playing_media.duration:
381 """Image url of current playing media."""
383 image_url = self.
_player_player.now_playing_media.image_url
384 return image_url
if image_url
else None
388 """Title of current playing media."""
389 return self.
_player_player.now_playing_media.song
393 """Boolean if shuffle is enabled."""
394 return self.
_player_player.shuffle
398 """Name of the current input source."""
403 """List of available input sources."""
407 def state(self) -> MediaPlayerState:
408 """State of the player."""
409 return PLAY_STATE_TO_STATE[self.
_player_player.state]
413 """Volume level of the media player (0..1)."""
414 return self.
_player_player.volume / 100
418 media_content_type: MediaType | str |
None =
None,
419 media_content_id: str |
None =
None,
421 """Implement the websocket media browsing helper."""
422 return await media_source.async_browse_media(
425 content_filter=
lambda item: item.media_content_type.startswith(
"audio/"),
None async_update_ha_state(self, bool force_refresh=False)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)