From 84c460ce784440e41f313c6f8c26911c8a16a992 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 4 Mar 2022 06:44:57 -0800 Subject: [PATCH] Add media source support to mpd (#67565) --- homeassistant/components/mpd/media_player.py | 23 +++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/mpd/media_player.py b/homeassistant/components/mpd/media_player.py index 4d3df8f00ca..9eda625b039 100644 --- a/homeassistant/components/mpd/media_player.py +++ b/homeassistant/components/mpd/media_player.py @@ -11,13 +11,18 @@ import mpd from mpd.asyncio import MPDClient import voluptuous as vol +from homeassistant.components import media_source from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerEntity +from homeassistant.components.media_player.browse_media import ( + async_process_play_media_url, +) from homeassistant.components.media_player.const import ( MEDIA_TYPE_MUSIC, MEDIA_TYPE_PLAYLIST, REPEAT_MODE_ALL, REPEAT_MODE_OFF, REPEAT_MODE_ONE, + SUPPORT_BROWSE_MEDIA, SUPPORT_CLEAR_PLAYLIST, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, @@ -71,6 +76,7 @@ SUPPORT_MPD = ( | SUPPORT_STOP | SUPPORT_TURN_OFF | SUPPORT_TURN_ON + | SUPPORT_BROWSE_MEDIA ) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( @@ -445,8 +451,13 @@ class MpdDevice(MediaPlayerEntity): async def async_play_media(self, media_type, media_id, **kwargs): """Send the media player the command for playing a playlist.""" - _LOGGER.debug("Playing playlist: %s", media_id) + if media_source.is_media_source_id(media_id): + media_type = MEDIA_TYPE_MUSIC + play_item = await media_source.async_resolve_media(self.hass, media_id) + media_id = play_item.url + if media_type == MEDIA_TYPE_PLAYLIST: + _LOGGER.debug("Playing playlist: %s", media_id) if media_id in self._playlists: self._currentplaylist = media_id else: @@ -456,6 +467,8 @@ class MpdDevice(MediaPlayerEntity): await self._client.load(media_id) await self._client.play() else: + media_id = async_process_play_media_url(self.hass, media_id) + await self._client.clear() self._currentplaylist = None await self._client.add(media_id) @@ -507,3 +520,11 @@ class MpdDevice(MediaPlayerEntity): async def async_media_seek(self, position): """Send seek command.""" await self._client.seekcur(position) + + async def async_browse_media(self, media_content_type=None, media_content_id=None): + """Implement the websocket media browsing helper.""" + return await media_source.async_browse_media( + self.hass, + media_content_id, + content_filter=lambda item: item.media_content_type.startswith("audio/"), + )