diff --git a/homeassistant/components/spotify/browse_media.py b/homeassistant/components/spotify/browse_media.py index 686431da249d..07e8cb77d179 100644 --- a/homeassistant/components/spotify/browse_media.py +++ b/homeassistant/components/spotify/browse_media.py @@ -26,7 +26,13 @@ from homeassistant.components.media_player import ( from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant -from .const import DOMAIN, MEDIA_PLAYER_PREFIX, MEDIA_TYPE_SHOW, PLAYABLE_MEDIA_TYPES +from .const import ( + DOMAIN, + MEDIA_PLAYER_PREFIX, + MEDIA_TYPE_SHOW, + MEDIA_TYPE_USER_SAVED_TRACKS, + PLAYABLE_MEDIA_TYPES, +) from .util import fetch_image_url BROWSE_LIMIT = 48 @@ -107,7 +113,7 @@ class BrowsableMedia(StrEnum): CURRENT_USER_PLAYLISTS = "current_user_playlists" CURRENT_USER_FOLLOWED_ARTISTS = "current_user_followed_artists" CURRENT_USER_SAVED_ALBUMS = "current_user_saved_albums" - CURRENT_USER_SAVED_TRACKS = "current_user_saved_tracks" + CURRENT_USER_SAVED_TRACKS = MEDIA_TYPE_USER_SAVED_TRACKS CURRENT_USER_SAVED_SHOWS = "current_user_saved_shows" CURRENT_USER_RECENTLY_PLAYED = "current_user_recently_played" CURRENT_USER_TOP_ARTISTS = "current_user_top_artists" @@ -119,7 +125,7 @@ LIBRARY_MAP = { BrowsableMedia.CURRENT_USER_PLAYLISTS.value: "Playlists", BrowsableMedia.CURRENT_USER_FOLLOWED_ARTISTS.value: "Artists", BrowsableMedia.CURRENT_USER_SAVED_ALBUMS.value: "Albums", - BrowsableMedia.CURRENT_USER_SAVED_TRACKS.value: "Tracks", + BrowsableMedia.CURRENT_USER_SAVED_TRACKS.value: "Liked songs", BrowsableMedia.CURRENT_USER_SAVED_SHOWS.value: "Podcasts", BrowsableMedia.CURRENT_USER_RECENTLY_PLAYED.value: "Recently played", BrowsableMedia.CURRENT_USER_TOP_ARTISTS.value: "Top Artists", @@ -321,6 +327,7 @@ async def build_item_response( # noqa: C901 for saved_album in saved_albums ] elif media_content_type == BrowsableMedia.CURRENT_USER_SAVED_TRACKS: + title = LIBRARY_MAP.get(media_content_type) if saved_tracks := await spotify.get_saved_tracks(): items = [ _get_track_item_payload(saved_track.track) @@ -445,8 +452,10 @@ def item_payload(item: ItemPayload, *, can_play_artist: bool) -> BrowseMedia: MediaType.EPISODE, ] - can_play = media_type in PLAYABLE_MEDIA_TYPES and ( - media_type != MediaType.ARTIST or can_play_artist + can_play = ( + media_type in PLAYABLE_MEDIA_TYPES + and (media_type != MediaType.ARTIST or can_play_artist) + and media_type != BrowsableMedia.CURRENT_USER_SAVED_TRACKS ) return BrowseMedia( diff --git a/homeassistant/components/spotify/const.py b/homeassistant/components/spotify/const.py index a89d35b7edfe..de537a566fde 100644 --- a/homeassistant/components/spotify/const.py +++ b/homeassistant/components/spotify/const.py @@ -27,6 +27,7 @@ SPOTIFY_SCOPES = [ MEDIA_PLAYER_PREFIX = "spotify://" MEDIA_TYPE_SHOW = "show" +MEDIA_TYPE_USER_SAVED_TRACKS = "current_user_saved_tracks" PLAYABLE_MEDIA_TYPES = [ MediaType.PLAYLIST, @@ -35,4 +36,5 @@ PLAYABLE_MEDIA_TYPES = [ MediaType.EPISODE, MEDIA_TYPE_SHOW, MediaType.TRACK, + MEDIA_TYPE_USER_SAVED_TRACKS, ] diff --git a/homeassistant/components/spotify/media_player.py b/homeassistant/components/spotify/media_player.py index d6265cbc39dc..a833edadaa3a 100644 --- a/homeassistant/components/spotify/media_player.py +++ b/homeassistant/components/spotify/media_player.py @@ -35,7 +35,11 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from .browse_media import async_browse_media_internal -from .const import MEDIA_PLAYER_PREFIX, PLAYABLE_MEDIA_TYPES +from .const import ( + MEDIA_PLAYER_PREFIX, + MEDIA_TYPE_USER_SAVED_TRACKS, + PLAYABLE_MEDIA_TYPES, +) from .coordinator import SpotifyConfigEntry, SpotifyCoordinator from .entity import SpotifyEntity @@ -332,7 +336,13 @@ class SpotifyMediaPlayer(SpotifyEntity, MediaPlayerEntity): if media_type in {MediaType.TRACK, MediaType.EPISODE, MediaType.MUSIC}: kwargs["uris"] = [media_id] elif media_type in PLAYABLE_MEDIA_TYPES: - kwargs["context_uri"] = media_id + context_uri = media_id + + if media_type == MEDIA_TYPE_USER_SAVED_TRACKS: + user_data = await self.coordinator.client.get_current_user() + context_uri = f"spotify:user:{user_data.user_id}:collection" + + kwargs["context_uri"] = context_uri else: _LOGGER.error("Media type %s is not supported", media_type) return diff --git a/tests/components/spotify/snapshots/test_media_browser.ambr b/tests/components/spotify/snapshots/test_media_browser.ambr index e241893df3b0..6ebbd869f00f 100644 --- a/tests/components/spotify/snapshots/test_media_browser.ambr +++ b/tests/components/spotify/snapshots/test_media_browser.ambr @@ -47,7 +47,7 @@ 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_saved_tracks', 'media_content_type': 'spotify://current_user_saved_tracks', 'thumbnail': None, - 'title': 'Tracks', + 'title': 'Liked songs', }), dict({ 'can_expand': True, @@ -497,7 +497,7 @@ # name: test_browsing[current_user_saved_tracks-current_user_saved_tracks] dict({ 'can_expand': True, - 'can_play': False, + 'can_play': True, 'can_search': False, 'children': list([ dict({ @@ -529,7 +529,7 @@ 'media_content_type': 'spotify://current_user_saved_tracks', 'not_shown': 0, 'thumbnail': None, - 'title': 'Tracks', + 'title': 'Liked songs', }) # --- # name: test_browsing[current_user_top_artists-current_user_top_artists] diff --git a/tests/components/spotify/test_media_player.py b/tests/components/spotify/test_media_player.py index 664418cc3776..a7f9f00a4197 100644 --- a/tests/components/spotify/test_media_player.py +++ b/tests/components/spotify/test_media_player.py @@ -461,6 +461,11 @@ async def test_play_media_in_queue( "spotify:episode:3oRoMXsP2NRzm51lldj1RO", {"uris": ["spotify:episode:3oRoMXsP2NRzm51lldj1RO"]}, ), + ( + "spotify://current_user_saved_tracks", + "spotify:user:1112264111:collection", + {"context_uri": "spotify:user:1112264111:collection"}, + ), ], ) async def test_play_media(