Spotify media player authorization problem

Thanks, I’m not but I’ll give it a go and attempt to change that.

The change of the polling interval decreased my daily API requests from ~16k to ~6k. Still a lot, but still a lot less than it was before.

Perhaps it can be decreased some more by applying adaptive polling times. For instance, if Spotify hasn’t been playing for 1 hour it can go back to updating every 2 minutes. After 3 hours it can go down to 5 minutes. Perhaps even lower if it’s 6 or 12 hours of being idle. If either Spotify starts playing by external controls (e.g. Spotify app) or if the media_player component is being used (e.g. pressing “Play”) from HA it could reset the timer back to 30s.

This would be more conservative towards the Spotify API, however if they don’t have a problem with ~6k requests per day it could also simply stay that way.

That looks like an awesome component. How can you use components that are still in development/not merged yet?
Is there a simple way to add these components to an existing HA installation?

@syphernl The problem right now is that it takes 2 requests for every update(). One to get the current playback state, and one to get the currently available devices, so that doubles the number of requests right off the bat. My concern with adaptive polling is the cold start problem. If I press “play” in the Spotify app in the morning after a night of being idle, HASS won’t update in the worst case until whatever the max adaptive interval is. 5+ minutes is a long time to wait, especially if you have automations based on the play state.

Spotify does have a websocket API which would solve all these problems, but it isn’t “public”, so I’m not comfortable basing a component on it.

@HansWurst90 It varies from component to component, but for this one, if you view the Pull Request, you can copy the “spotify.py” file into the same directory location in your local Home Assistant install, and it’ll just work. You’ll also have to do pip3 install https://github.com/happyleavesaoc/spotipy/archive/544614f4b1d508201d363e84e871f86c90aa26b2.zip#spotipy==2.4.4 with your Home Assistant virtual environment activated.

1 Like

@happyleaves Sweet! I will try it out when I’m home tonight

@happyleaves sorry to bother you again. But where is my “local Home Assistant install”?
What’s the path? You don’t mean the folder where the configuration.yaml is, do you?

Depends entirely on how you installed Home Assistant. It’d be the directory with all the code in it.

I used the hassbian diskimage for a clean reinstall and I can’t find info on what the path would be :smiley:

@happyleaves I managed to get it to work!

I am running the latest version of Home Assistant, being 0.43.2, and the following error is being added to my log every 62 seconds:

17-04-28 23:19:40 ERROR (MainThread) [homeassistant.helpers.entity] Update for media_player.spotify fails
Traceback (most recent call last):
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/helpers/entity.py", line 225, in async_update_ha_state
    None, self.update)
  File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
    value = future.result()
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/components/media_player/spotify.py", line 158, in update
    devices = self._player.devices().get('devices')
AttributeError: 'NoneType' object has no attribute 'get'

I see @syphernl had the same (or similar) error some time ago. Am I doing something wrong? Is this a bug?

@fanaticDavid

I am having the same issue when i attempt to cast a playlist to chromecast. Did you manage to solve this one yet or does anyone else know a solution?

I don’t even have to do anything with the Spotify component to get those errors. I’ve stopped using it for the time being.

Hi,
I am not sure why you wanted to get the player to idle state, what is the purpose?
I noticed that my spotify player can enter idle after a longer inactivity, and when it does enter idle it seems is does not work to get it into playing mode again? Neither through automation nor the HA spotify player? The only way I have found to wake it from idle is to start playing from e.g. a windows or android spotify client!? How can I wake it up from idle using HA automation? Automations that normally work does not work when the player is idle.

I am getting the following error and no spotify panel (I received the initial authorization panel) - any suggestions?

Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/homeassistant/helpers/entity_component.py”, line 397, in _async_add_entity
yield from entity.async_device_update(warning=False)
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/homeassistant/helpers/entity.py”, line 308, in async_device_update
yield from self.hass.async_add_job(self.update)
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py”, line 358, in iter
yield self # This tells Task to wait for completion.
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/tasks.py”, line 290, in _wakeup
future.result()
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py”, line 274, in result
raise self._exception
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/concurrent/futures/thread.py”, line 55, in run
result = self.fn(*self.args, **self.kwargs)
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/homeassistant/components/media_player/spotify.py”, line 164, in update
self.refresh_spotify_instance()
File “/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/homeassistant/components/media_player/spotify.py”, line 146, in refresh_spotify_instance
self._oauth.is_token_expired(self._token_info))
AttributeError: ‘SpotifyOAuth’ object has no attribute ‘is_token_expired’

Hey,
im getting errors when i try and deploy this.
when i hit the “authorize” button, the page gives me a timeout: xx.duckdns.org took too long to respond.

Config.
media_player:
#https://www.home-assistant.io/components/spotify/

  • platform: spotify
    client_id: !secret SpotifyClientID
    client_secret: !secret SpotifySecret

BaseURL = https://xxx.duckdns.org:8123

Spotify Redirct URL = https://xxx.duckdns.org:8123/api/spotify

URL its getting the errors on:
https://xxxxxx.duckdns.org:8123/api/spotify?code=xxxxxxxxxxxxxxxxxxx

ive double checked both my ID and Secret are correct

Note: i dont see a .spotify-token-cache file?
if i need to specify a path, how and where do i do this, to give the right access sorry?

Did you ever solve this?