Skip to content

Commit

Permalink
Add support for the Spotify DJ (#107268)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Co-authored-by: Joost Lekkerkerker <[email protected]>
  • Loading branch information
BTMorton and joostlek authored Jan 6, 2024
1 parent 4af47a4 commit 427f7a7
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion homeassistant/components/spotify/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
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,
Expand Down Expand Up @@ -423,7 +427,19 @@ def update(self) -> None:
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:
Expand Down

0 comments on commit 427f7a7

Please sign in to comment.