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