core: Spotify fails after linking
The problem
Spotify integration fails to reload the session after a few hours of setting it up (although it works perfectly before that). This also appears to repro on other accounts (check post). To fix it I have to remove the integration, install and link with my account again but the error shows up again after a few hours. Check additional info below on why I think this is happening.

What is version of Home Assistant Core has the issue?
core-2021.7.3
What was the last working version of Home Assistant Core?
No response
What type of installation are you running?
Home Assistant OS
Integration causing the issue
Spotify
Link to integration documentation on our website
https://www.home-assistant.io/integrations/spotify/
Example YAML snippet
I just added the integration from the UI and linked my account.
Anything in the logs that might be useful for us?
Update for media_player.jmyaccount fails
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 401, in async_update_ha_state
await self.async_device_update()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 609, in async_device_update
raise exc
File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/src/homeassistant/homeassistant/components/spotify/media_player.py", line 211, in wrapper
result = func(self, *args, **kwargs)
File "/usr/src/homeassistant/homeassistant/components/spotify/media_player.py", line 469, in update
run_coroutine_threadsafe(
File "/usr/local/lib/python3.9/concurrent/futures/_base.py", line 445, in result
return self.__get_result()
File "/usr/local/lib/python3.9/concurrent/futures/_base.py", line 390, in __get_result
raise self._exception
File "/usr/src/homeassistant/homeassistant/helpers/config_entry_oauth2_flow.py", line 464, in async_ensure_token_valid
new_token = await self.implementation.async_refresh_token(self.token)
File "/usr/src/homeassistant/homeassistant/helpers/config_entry_oauth2_flow.py", line 88, in async_refresh_token
new_token = await self._async_refresh_token(token)
File "/usr/src/homeassistant/homeassistant/components/cloud/account_link.py", line 147, in _async_refresh_token
self.hass.data[DOMAIN], self.service, token["refresh_token"]
KeyError: 'refresh_token'
Logger: homeassistant.helpers.entity
Source: components/cloud/account_link.py:147
First occurred: 22 July 2021, 22:21:30 (1247 occurrences)
Last logged: 10:43:24
Additional information
It appears that it losses the refresh token which is used to obtain a new access token when the latter expires (after a few hours). This is the documentation on authorisation from Spotify https://developer.spotify.com/documentation/general/guides/authorization-guide/.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 4
- Comments: 17 (2 by maintainers)
I’ve followed the same procedure as @DavidWicks and everything has been working for a day now. If this is the case then I definitely think the integration should request a client id & secret during installation to improve the user experience. Currently it is a mix of the new way of adding integrations and the old way of manually editing your
configuration.yamlfile. Without clear instructions stating that is the case. 😞This is unrelated, please use the community forum. Your error actually explains the problem. You need to activate the device physically. To play on inactive devices use spotcast. Also your URI is not in the correct format, look for examples in the wikis.
Mine seems to be resolved. My latest attempt is to setup the spotify application (https://www.home-assistant.io/integrations/spotify/#create-a-spotify-application) and adding my own account to the
user and accesstab of the developer dashboard (top right). Then when setting up in the web UI, I made sure that theclientIDand secrete in the URL matches the one in my developer account.This morning on see on my developer dashboard homeassistant has contacted the developer account and probably have successfully renewed the token.
I have the same issue. I added the Spotify integration via the UI and everything worked for about an hour or so then my logs start filling up with the
KeyErrormessage every 30 seconds. I later noticed the docs refer to setting up a developer account, but I’m not sure that is still required with the updated integration so I haven’t done it yet. I wanted to get some clarification first.