From ba43cb909b293a3eecaae01363f6b5dd94fbe089 Mon Sep 17 00:00:00 2001 From: davidlb Date: Fri, 8 Apr 2022 10:09:08 +0200 Subject: [PATCH 1/5] Backport changes from homeassistant repo --- custom_components/owntone_dleb/const.py | 51 ++++++++----------- .../owntone_dleb/media_player.py | 17 +++++++ 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/custom_components/owntone_dleb/const.py b/custom_components/owntone_dleb/const.py index 67a086f..e01631c 100644 --- a/custom_components/owntone_dleb/const.py +++ b/custom_components/owntone_dleb/const.py @@ -1,20 +1,5 @@ """Const for forked-daapd.""" -from homeassistant.components.media_player.const import ( - SUPPORT_CLEAR_PLAYLIST, - SUPPORT_NEXT_TRACK, - SUPPORT_PAUSE, - SUPPORT_PLAY, - SUPPORT_PLAY_MEDIA, - SUPPORT_PREVIOUS_TRACK, - SUPPORT_SEEK, - SUPPORT_SELECT_SOURCE, - SUPPORT_SHUFFLE_SET, - SUPPORT_STOP, - SUPPORT_TURN_OFF, - SUPPORT_TURN_ON, - SUPPORT_VOLUME_MUTE, - SUPPORT_VOLUME_SET, -) +from homeassistant.components.media_player import MediaPlayerEntityFeature CALLBACK_TIMEOUT = 8 # max time between command and callback from forked-daapd server CONF_LIBRESPOT_JAVA_PORT = "librespot_java_port" @@ -60,21 +45,25 @@ "outputs": [], } SUPPORTED_FEATURES = ( - SUPPORT_PLAY - | SUPPORT_PAUSE - | SUPPORT_STOP - | SUPPORT_SEEK - | SUPPORT_VOLUME_SET - | SUPPORT_VOLUME_MUTE - | SUPPORT_PREVIOUS_TRACK - | SUPPORT_NEXT_TRACK - | SUPPORT_CLEAR_PLAYLIST - | SUPPORT_SELECT_SOURCE - | SUPPORT_SHUFFLE_SET - | SUPPORT_TURN_ON - | SUPPORT_TURN_OFF - | SUPPORT_PLAY_MEDIA + MediaPlayerEntityFeature.PLAY + | MediaPlayerEntityFeature.PAUSE + | MediaPlayerEntityFeature.STOP + | MediaPlayerEntityFeature.SEEK + | MediaPlayerEntityFeature.VOLUME_SET + | MediaPlayerEntityFeature.VOLUME_MUTE + | MediaPlayerEntityFeature.PREVIOUS_TRACK + | MediaPlayerEntityFeature.NEXT_TRACK + | MediaPlayerEntityFeature.CLEAR_PLAYLIST + | MediaPlayerEntityFeature.SELECT_SOURCE + | MediaPlayerEntityFeature.SHUFFLE_SET + | MediaPlayerEntityFeature.TURN_ON + | MediaPlayerEntityFeature.TURN_OFF + | MediaPlayerEntityFeature.PLAY_MEDIA + | MediaPlayerEntityFeature.BROWSE_MEDIA ) SUPPORTED_FEATURES_ZONE = ( - SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | SUPPORT_TURN_ON | SUPPORT_TURN_OFF + MediaPlayerEntityFeature.VOLUME_SET + | MediaPlayerEntityFeature.VOLUME_MUTE + | MediaPlayerEntityFeature.TURN_ON + | MediaPlayerEntityFeature.TURN_OFF ) diff --git a/custom_components/owntone_dleb/media_player.py b/custom_components/owntone_dleb/media_player.py index 34a887b..94d4704 100644 --- a/custom_components/owntone_dleb/media_player.py +++ b/custom_components/owntone_dleb/media_player.py @@ -6,7 +6,11 @@ from pyforked_daapd import ForkedDaapdAPI from pylibrespot_java import LibrespotJavaAPI +from homeassistant.components import media_source from homeassistant.components.media_player import MediaPlayerEntity +from homeassistant.components.media_player.browse_media import ( + async_process_play_media_url, +) from homeassistant.components.media_player.const import MEDIA_TYPE_MUSIC from homeassistant.const import ( CONF_HOST, @@ -631,6 +635,11 @@ async def _pause_and_wait_for_callback(self): async def async_play_media(self, media_type, media_id, **kwargs): """Play a URI.""" + 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_MUSIC: _LOGGER.debug("Play Media '%s'", media_id) await self._api.add_to_queue(uris=media_id, playback="start", clear=True) @@ -791,3 +800,11 @@ def _add_zones(self, outputs): self._api, outputs_to_add, ) + + 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/"), + ) From 5657a31aaca15f106762225ee8d61186ca847bbb Mon Sep 17 00:00:00 2001 From: davidlb Date: Fri, 8 Apr 2022 10:34:29 +0200 Subject: [PATCH 2/5] Restore previous config (as not available in 2022.04) --- custom_components/owntone_dleb/const.py | 53 +++++++++++++++---------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/custom_components/owntone_dleb/const.py b/custom_components/owntone_dleb/const.py index e01631c..995af76 100644 --- a/custom_components/owntone_dleb/const.py +++ b/custom_components/owntone_dleb/const.py @@ -1,5 +1,21 @@ """Const for forked-daapd.""" -from homeassistant.components.media_player import MediaPlayerEntityFeature +from homeassistant.components.media_player.const import ( + SUPPORT_BROWSE_MEDIA, + SUPPORT_CLEAR_PLAYLIST, + SUPPORT_NEXT_TRACK, + SUPPORT_PAUSE, + SUPPORT_PLAY, + SUPPORT_PLAY_MEDIA, + SUPPORT_PREVIOUS_TRACK, + SUPPORT_SEEK, + SUPPORT_SELECT_SOURCE, + SUPPORT_SHUFFLE_SET, + SUPPORT_STOP, + SUPPORT_TURN_OFF, + SUPPORT_TURN_ON, + SUPPORT_VOLUME_MUTE, + SUPPORT_VOLUME_SET, +) CALLBACK_TIMEOUT = 8 # max time between command and callback from forked-daapd server CONF_LIBRESPOT_JAVA_PORT = "librespot_java_port" @@ -45,25 +61,22 @@ "outputs": [], } SUPPORTED_FEATURES = ( - MediaPlayerEntityFeature.PLAY - | MediaPlayerEntityFeature.PAUSE - | MediaPlayerEntityFeature.STOP - | MediaPlayerEntityFeature.SEEK - | MediaPlayerEntityFeature.VOLUME_SET - | MediaPlayerEntityFeature.VOLUME_MUTE - | MediaPlayerEntityFeature.PREVIOUS_TRACK - | MediaPlayerEntityFeature.NEXT_TRACK - | MediaPlayerEntityFeature.CLEAR_PLAYLIST - | MediaPlayerEntityFeature.SELECT_SOURCE - | MediaPlayerEntityFeature.SHUFFLE_SET - | MediaPlayerEntityFeature.TURN_ON - | MediaPlayerEntityFeature.TURN_OFF - | MediaPlayerEntityFeature.PLAY_MEDIA - | MediaPlayerEntityFeature.BROWSE_MEDIA + SUPPORT_PLAY + | SUPPORT_PAUSE + | SUPPORT_STOP + | SUPPORT_SEEK + | SUPPORT_VOLUME_SET + | SUPPORT_VOLUME_MUTE + | SUPPORT_PREVIOUS_TRACK + | SUPPORT_NEXT_TRACK + | SUPPORT_CLEAR_PLAYLIST + | SUPPORT_SELECT_SOURCE + | SUPPORT_SHUFFLE_SET + | SUPPORT_TURN_ON + | SUPPORT_TURN_OFF + | SUPPORT_PLAY_MEDIA + | SUPPORT_BROWSE_MEDIA ) SUPPORTED_FEATURES_ZONE = ( - MediaPlayerEntityFeature.VOLUME_SET - | MediaPlayerEntityFeature.VOLUME_MUTE - | MediaPlayerEntityFeature.TURN_ON - | MediaPlayerEntityFeature.TURN_OFF + SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | SUPPORT_TURN_ON | SUPPORT_TURN_OFF ) From 9fb20d660a3fc679a734642c78f3cef656d4fdbe Mon Sep 17 00:00:00 2001 From: davidlb Date: Fri, 8 Apr 2022 16:51:25 +0200 Subject: [PATCH 3/5] Fix discovery warnings --- custom_components/owntone_dleb/config_flow.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/custom_components/owntone_dleb/config_flow.py b/custom_components/owntone_dleb/config_flow.py index af4ed9c..3fc3917 100644 --- a/custom_components/owntone_dleb/config_flow.py +++ b/custom_components/owntone_dleb/config_flow.py @@ -142,16 +142,16 @@ async def async_step_user(self, user_input=None): async def async_step_zeroconf(self, discovery_info): """Prepare configuration for a discovered forked-daapd device.""" version_num = 0 - if discovery_info.get("properties") and discovery_info["properties"].get( + if discovery_info.properties and discovery_info.properties.get( "Machine Name" ): with suppress(ValueError): version_num = int( - discovery_info["properties"].get("mtd-version", "0").split(".")[0] + discovery_info.properties.get("mtd-version", "0").split(".")[0] ) if version_num < 27: return self.async_abort(reason="not_forked_daapd") - await self.async_set_unique_id(discovery_info["properties"]["Machine Name"]) + await self.async_set_unique_id(discovery_info.properties["Machine Name"]) self._abort_if_unique_id_configured() # Update title and abort if we already have an entry for this host From 103f5496d5012a2a3ce6f6d568ad74281c56557b Mon Sep 17 00:00:00 2001 From: davidlb Date: Fri, 8 Apr 2022 17:05:06 +0200 Subject: [PATCH 4/5] Fix error --- custom_components/owntone_dleb/media_player.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_components/owntone_dleb/media_player.py b/custom_components/owntone_dleb/media_player.py index 94d4704..d7131ee 100644 --- a/custom_components/owntone_dleb/media_player.py +++ b/custom_components/owntone_dleb/media_player.py @@ -642,7 +642,8 @@ async def async_play_media(self, media_type, media_id, **kwargs): if media_type == MEDIA_TYPE_MUSIC: _LOGGER.debug("Play Media '%s'", media_id) - await self._api.add_to_queue(uris=media_id, playback="start", clear=True) + """ media_id = async_process_play_media_url(self.hass, media_id) """ + await self._api.add_to_queue(uris=media_id, playback="start", clear=True) else: _LOGGER.debug("Media type '%s' not supported", media_type) From 8ebeadb46a9e9146e3afe8f3f866ba7a97483a29 Mon Sep 17 00:00:00 2001 From: davidlb Date: Fri, 8 Apr 2022 17:14:17 +0200 Subject: [PATCH 5/5] Fix indent --- custom_components/owntone_dleb/media_player.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/owntone_dleb/media_player.py b/custom_components/owntone_dleb/media_player.py index d7131ee..b4fac7c 100644 --- a/custom_components/owntone_dleb/media_player.py +++ b/custom_components/owntone_dleb/media_player.py @@ -642,8 +642,8 @@ async def async_play_media(self, media_type, media_id, **kwargs): if media_type == MEDIA_TYPE_MUSIC: _LOGGER.debug("Play Media '%s'", media_id) - """ media_id = async_process_play_media_url(self.hass, media_id) """ - await self._api.add_to_queue(uris=media_id, playback="start", clear=True) + """ media_id = async_process_play_media_url(self.hass, media_id) """ + await self._api.add_to_queue(uris=media_id, playback="start", clear=True) else: _LOGGER.debug("Media type '%s' not supported", media_type)