Spotify: user Liked Songs collection playable (#160452)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Vasily G.
2026-01-09 17:48:39 +01:00
committed by GitHub
parent f79eef150e
commit 5f7dc49215
5 changed files with 36 additions and 10 deletions
@@ -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(
@@ -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,
]
@@ -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
@@ -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]
@@ -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(