From 10c7725a901bc4328979e8aa0454e4bfa5832ce4 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 12 Feb 2022 22:53:15 +0100 Subject: [PATCH] Fix Spotify session token refresh (#66390) --- homeassistant/components/spotify/__init__.py | 6 ++++++ homeassistant/components/spotify/media_player.py | 10 ++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/spotify/__init__.py b/homeassistant/components/spotify/__init__.py index c3e9504c05c..446c2ace82c 100644 --- a/homeassistant/components/spotify/__init__.py +++ b/homeassistant/components/spotify/__init__.py @@ -121,6 +121,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: raise ConfigEntryNotReady from err async def _update_devices() -> list[dict[str, Any]]: + if not session.valid_token: + await session.async_ensure_token_valid() + await hass.async_add_executor_job( + spotify.set_auth, session.token["access_token"] + ) + try: devices: dict[str, Any] | None = await hass.async_add_executor_job( spotify.devices diff --git a/homeassistant/components/spotify/media_player.py b/homeassistant/components/spotify/media_player.py index b3bb2efd1c0..06efb5558d1 100644 --- a/homeassistant/components/spotify/media_player.py +++ b/homeassistant/components/spotify/media_player.py @@ -515,9 +515,7 @@ class SpotifyMediaPlayer(MediaPlayerEntity): run_coroutine_threadsafe( self._session.async_ensure_token_valid(), self.hass.loop ).result() - self._spotify_data[DATA_SPOTIFY_CLIENT] = Spotify( - auth=self._session.token["access_token"] - ) + self._spotify.set_auth(auth=self._session.token["access_token"]) current = self._spotify.current_playback() self._currently_playing = current or {} @@ -581,7 +579,11 @@ async def async_browse_media_internal( partial(library_payload, can_play_artist=can_play_artist) ) - await session.async_ensure_token_valid() + if not session.valid_token: + await session.async_ensure_token_valid() + await hass.async_add_executor_job( + spotify.set_auth, session.token["access_token"] + ) # Strip prefix media_content_type = media_content_type[len(MEDIA_PLAYER_PREFIX) :]