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.

0c5edaad2f78d7cd085fc806799e4a645712deed

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)

Most upvoted comments

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.yaml file. Without clear instructions stating that is the case. 😞

I think that the issue happens when you link the account before adding your developer credentials to the config. The reason I did this is because in the UI it never asked me to and I haven’t read the wiki about it. I added my credentials (client id and client secret), reinstalled the app and relinked my account, and so far it’s been working. Let me know if this works. IMO, there should be a check for the credentials before attempting to link the account. What do you think @frenck ?

With this, the GUI Integration Alert disappears, but I get this error:

Logger: spotipy.client
Source: /usr/local/lib/python3.9/site-packages/spotipy/client.py:258 
First occurred: 13:35:59 (7 occurrences) 
Last logged: 13:49:41

HTTP Error for PUT to https://api.spotify.com/v1/me/player/play returned 404 due to Player command failed: No active device found
HTTP Error for PUT to https://api.spotify.com/v1/me/player/play?device_id=02cd60d1aaa94e77c8b115b183e9a7f8fd280e33 returned 400 due to Invalid context uri
HTTP Error for PUT to https://api.spotify.com/v1/me/player/volume?volume_percent=62 returned 404 due to Player command failed: No active device found

After calling the service:

service: media_player.play_media
target:
  entity_id: media_player.spotify_user_account
data:
  media_content_type: playlist
  media_content_id: >-
    "https://open.spotify.com/playlist/37i9dQZF1DX3ExziScPqAH?si=0a07255f848246f2"

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 access tab of the developer dashboard (top right). Then when setting up in the web UI, I made sure that the clientID and 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 KeyError message 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.