1
mirror of https://github.com/home-assistant/core synced 2024-07-15 09:42:11 +02:00

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 <joostlek@outlook.com>

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Ben Morton 2024-01-06 16:22:46 +00:00 committed by GitHub
parent 4af47a4815
commit 427f7a7866
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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: