Minor improvements to the Spotify Integration documentation

The Spotify integration was my first use of a media player on Home Assistant and I was thinking my fresh eyes could contribute some minor improvements to the documentation. Before I open a pull request …

The Media section in the UI is the easiest place to try it out. Foolishly I didn’t notice the media browser. Is there documentation we can link to?

I could only find these release notes 0.115: B-Day release! Media browser, tags, automations & WTH - Home Assistant

Source selection is important to understand (i.e. if none is selected nothing will play) and these are the names defined by the Spotify API so unlikely to perfectly match the entity names in Home Assistant.

I believe no sources show until you use the Spotify App (phone or website) to play on a device. It may help to understand that if the device isn’t Spotify Connect (i.e. Google Cast) the source won’t persist, so this integration cannot initiate playback on a Chromecast. If anyone can confirm this we could try to explain it.

A third point, the media_content_id must have the correct corresponding media_content_type. It can be a URL or the Spotify URI string

https://open.spotify.com/playlist/37i9dQZF1DWVFeEut75IAL and spotify:playlist:37i9dQZF1DWVFeEut75IAL both work.

Most experienced users will already know this. Let me know if you can confirm any of the three points above and if you think these be welcome additions to the documentation

You might want to wait until after 2022.3. Check out the beta release note for more info.

Google was no help but from https://github.com/home-assistant/core/releases I found the link to 2022.3: Beta release notes - Home Assistant

The UI ability to select media to play sounds interesting. I am not sure how it will impact what I am talking about but I will keen an eye out. Thanks

I can confirm the requirement to select the source. I have to tell Alexa once in a while to „connect with Spotify“ and select my echo dot in the Spotify app. Once finished, Spotify plays nicely in HA. I haven‘t found out when to connect though. Sometimes it persists for days, sometimes not.

So a hint in the docs may help. Anyone with an idea to connect from within HA?

Interesting @swa72, my experience with Google Home is different so it is curious how Alexa Echos behave.

I would really welcome input to explain the limitations as clearly (and accurately) as possible. Workarounds welcome. See the end of this draft I just wrote:

Selecting Spotify output device

To play media Spotify needs a device selected known as the source. Currently, this integration cannot initiate playback to a device not already available from the Spotify API. You may need to first use Spotify on your smart speaker or phone to add a device.

# Example code to select an AV receiver as the output device
service: media_player.select_source
entity_id: media_player.spotify_myaccount
data:
  source: "Denon AVR-X2000"

The source list of available devices can be found in the Details section of the Spotify Media Player Control and the source_list attribute in the Developer tools States or templates using

{{ states.media_player.spotify_myaccount.attributes.source_list }}

The device name must be known to Spotify and therefore is often different from names given to entities in Home Assistant.

A known limitation is that although Spotify Connect devices tend to persist as available devices, other devices such as Amazon Alexa speakers may only be remembered for a while and phones and Google Cast devices disappear when playback stops.

{{ "Denon AVR-X2000" in states.media_player.spotify_kcd83.attributes.source_list }}

Will return false if Spotify forgets it… which might help

I was trying to think of the best example of Spotify and came up with the following. I think it would best sit under the media_player documentation.

Not sure if anyone is reading… but all input is welcome.

This example combines a number of media player commands to set the output speaker for Spotify to 30% volume and play a randomly shuffled track from the playlist Calming Classical.

This demonstrates how the multiple steps a user would go through can be combined into one script that can then be called by a single button press or automation.

# Script demonstrating media player functionality
# To use this script you must update each media_player entity id to match your Spotify integration and smart speaker device
alias: Demo Spotify
icon: mdi:music-box
mode: single
sequence:
# In this demo we first prepare a receiver that supports Spotify
  - service: scene.apply
    data:
      entities:
        media_player.denon_avr_x2000:
          state: 'on'
          sound_mode: STEREO # sound mode is only supported by select devices
          volume_level: 0.3
# Set the output. See the Spotify integration documentation about how the concept of source differs from other media players
  - service: media_player.select_source
    entity_id: media_player.spotify_kcd83
    data:
      source: Denon AVR-X2000

# If the script is too fast we may get a "No active playback device" error
  - alias: Allow time for service to wake from idle
    wait_template: '{{ not is_state(''media_player.spotify_kcd83'', ''idle'') }}'
    timeout: '00:00:30'

# Start a playlist then set it to repeat, shuffle and skip to a random track
  - service: media_player.play_media
    data:
      entity_id: media_player.spotify_kcd83
      media_content_id: 'spotify:playlist:37i9dQZF1DWVFeEut75IAL'
      media_content_type: playlist

  - service: media_player.repeat_set
    data:
      entity_id: media_player.spotify_kcd83
      repeat: all
  - service: media_player.shuffle_set
    data:
      entity_id: media_player.spotify_kcd83
      shuffle: true
  - service: media_player.media_next_track
    data:
      entity_id: media_player.spotify_kcd83

Pushing changes here:

sorry to interrupt i’m still struggling with connecting spotift to HA, Keep getting “INVALID_CLIENT: Failed to get client”
any idea?