From 427f7a786681903e921cd862c237f50443649559 Mon Sep 17 00:00:00 2001 From: Ben Morton Date: Sat, 6 Jan 2024 16:22:46 +0000 Subject: [PATCH] Add support for the Spotify DJ (#107268) * Add support for the Spotify DJ playlist by mocking the playlist response Add error handling for playlist lookup to ensure it doesn't break current playback state loading * Run linters Add exception type to playlist lookup error handling * Fix typo in comment Co-authored-by: Joost Lekkerkerker --------- Co-authored-by: Joost Lekkerkerker --- .../components/spotify/media_player.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/spotify/media_player.py b/homeassistant/components/spotify/media_player.py index 6ef2697ba774..0204cc30fbb0 100644 --- a/homeassistant/components/spotify/media_player.py +++ b/homeassistant/components/spotify/media_player.py @@ -61,6 +61,10 @@ REPEAT_MODE_MAPPING_TO_SPOTIFY = { value: key for key, value in REPEAT_MODE_MAPPING_TO_HA.items() } +# This is a minimal representation of the DJ playlist that Spotify now offers +# The DJ is not fully integrated with the playlist API, so needs to have the playlist response mocked in order to maintain functionality +SPOTIFY_DJ_PLAYLIST = {"uri": "spotify:playlist:37i9dQZF1EYkqdzj48dyYq", "name": "DJ"} + async def async_setup_entry( hass: HomeAssistant, @@ -423,7 +427,19 @@ class SpotifyMediaPlayer(MediaPlayerEntity): if context and (self._playlist is None or self._playlist["uri"] != uri): self._playlist = None if context["type"] == MediaType.PLAYLIST: - self._playlist = self.data.client.playlist(uri) + # The Spotify API does not currently support doing a lookup for the DJ playlist, so just use the minimal mock playlist object + if uri == SPOTIFY_DJ_PLAYLIST["uri"]: + self._playlist = SPOTIFY_DJ_PLAYLIST + else: + # Make sure any playlist lookups don't break the current playback state update + try: + self._playlist = self.data.client.playlist(uri) + except SpotifyException: + _LOGGER.debug( + "Unable to load spotify playlist '%s'. Continuing without playlist data", + uri, + ) + self._playlist = None device = self._currently_playing.get("device") if device is not None: