Spotify Media Player delay

@ShadowFist @Osher @Jerry_D @leonkunst88 @BYOB @aquarist684

I have developed a new SpotifyPlus Home Assistant custom component integration that expands the built-in Spotify capabilities. It’s a simple install to the “custom_components” directory via a HACS UI custom repository installer.

I also developed the underlying SpotifyWebApiPython library that interfaces with the Spotify Web API.

With the release of v1.0.11, player status changes are detected in near real-time IF changes are made via Home Assistant (e.g. UI, service call, automation, etc).

This was accomplished by changing the integration SCAN_INTERVAL from 30 seconds to 1 second, but also adding logic to the update method to only call the underlying Spotify Web API at selected intervals:

  • call every 30 seconds if no user or track change activity.
  • call if a player command is issued: set_volume_level, media_play, media_pause, media_previous_track, media_next_track, media_seek, play_media, select_source, set_shuffle, and set_repeat.
  • call every second for 5 seconds after a player command is issued. This allows for extra time it may take for the Spotify Connect Player to update it’s status after the command is processed.
  • call if the nowplaying track position has less than 5 seconds remaining. This is accomplished by subtracting the nowplaying DurationMS value (e.g. length of the track) from the nowPlaying track ProgressMS (e.g. current position of the track) value. This logic adds a few more calls to the Spotify Web API, but not many. It’s not perfect, but the acceptable trade-off is near real-time updates of Spotify Connect Player status.

Note that Spotify Connect Player changes are NOT detected in near real-time if controlling the Spotify Connect Player from another non-HA service (e.g. Spotify Web App, Spotify Mobile App, etc). In this case, changes will be detected within 30 seconds by the HA state machine. For example, if you advance to the next track from the Spotify Web player, it will not show the change in HA for up to 30 seconds. This is a limitation of the Spotify Web API, in that Spotify does not offer a websockets API that informs clients of Spotify Connect Player status and relies on client polling.

Thought you would like to know, and hope it helps!

More Information Links

1 Like

Hi @ShadowFist

I get an error with your code for some reason it does not work.
Care to asssit?

Thank you

From the log:
Logger: custom_components.pyscript.file.spot2
Source: custom_components/pyscript/
integration: Pyscript Python scripting (documentation, issues)
First occurred: 11:55:35 (1 occurrences)
Last logged: 11:55:35

Exception in </config/pyscript/> line 3: trigger: ^ SyntaxError: invalid syntax (, line 3)

From the error, it looks like you’re using the code as a python script instead of a regular automation.

If that’s the case, then my automation won’t work because scripts don’t have triggers.

1 Like

I have an issue when trying to modify, pause or stop music whilst spotify is casting on my Sonos speaker

Failed to call service media_player/media_pause. Restricted device

If playing on my computer speaker all works fine.

Please advise?