SpotifyPlus Custom Integration

@Inaxsesable
I will look into this. I probably broke something while trying to get the Sonos support working. Will keep you posted.

FYI - just released a new version of the SpotifyPlus integration .

[ 1.0.45 ] - 2024/07/30

  • Updated service player_transfer_playback to support Sonos devices with some limitations; see the Sonos Limitations wiki documentation for further details.
  • Updated service player_media_play_context and player_media_play_tracks to support Sonos devices with some limitations; see the Sonos Limitations wiki documentation for further details.
  • Fixed a bug in the player_transfer_playback service that was not transferring control to offline Spotify Connect devices.

@Inaxsesable @joolz1
This should fix the transfer playback issues. Please see the Sonos Limitations wiki documentation for detailed information on Sonos device limitations. You may still have to switch the music source via the Spotify Desktop / Mobile app if you use multiple music sources on your Sonos device(s).

1 Like

My nice script for getting my spotify music from my phone to the Soundtouch doesn’t work anymore :frowning:

service: spotifyplus.player_transfer_playback
data:
  entity_id: media_player.spotifyplus_diegocampy
  play: true
  device_id: Bose

I tried and deleted the string of numbers and letters as device id, saving the name as Bose (its name in home assistant) but nothing works. Maybe I should find the device id again? I don’t remember how to do it. Currently with Bose device id or without specifying device id, the script response is always:
Error executing action
Device not found

I’m sure you’ve already explained to me how to recover the ID, but I can’t, I don’t remember how to do it

@Diegocampy
Just make sure you have updated to SpotifyPlus v1.0.45, as the v1.0.40-44 had some issues with transfer of playback due to Sonos support additions.

You can use the Spotify Connect name for the device_id argument of the spotifyplus.player_transfer_playback service call.

The Spotify Connect name can be found in the Spotify Mobile / Desktop App “Connect to a device” menu:

You can also find the names and id’s via the spotifyplus.get_spotify_connect_devices service output. Use the HA Developer Tools \ Services to call the service and display the results. Just specify your SpotifyPlus entity_id (mine is spotifyplus_todd_l in this example):

service: spotifyplus.get_spotify_connect_devices
data:
  entity_id: media_player.spotifyplus_todd_l

Sample Output - the result id value is the device id, and name is the device name.

user_profile:
  display_name: Todd L
  ...
result:
  - id: 30fbc80e35598f3c242f2120413c943dfd9715fe
    is_active: false
    is_private_session: false
    is_restricted: false
    name: Bose-ST10-1
    supports_volume: false
    type: SPEAKER
    volume_percent: 0
  - id: 5d4931f9d0684b625d702eaa24137b2c1d99539c
    is_active: false
    is_private_session: false
    is_restricted: false
    name: Bose-ST10-2
    supports_volume: false
    type: SPEAKER
    volume_percent: 0
  - id: 3756903295ba7b47f8c36fc9a4ff7d833431a667
    is_active: false
    is_private_session: false
    is_restricted: false
    name: Bose-ST300
    supports_volume: false
    type: SPEAKER
    volume_percent: 0
  - id: 10e64fcf9695bd2796ce62b84d688558e39cd311
    is_active: false
    is_private_session: false
    is_restricted: false
    name: HAVM-SpotifyConnect
    supports_volume: false
    type: Speaker
    volume_percent: 0
  - id: f87342b3ad455375317d5af8aabde64a28bd49d0
    is_active: false
    is_private_session: false
    is_restricted: false
    name: Office
    supports_volume: false
    type: SPEAKER
    volume_percent: 0
  - id: 80da4987232671a83397682373f7ce21ea92f2e4
    is_active: false
    is_private_session: false
    is_restricted: false
    name: THLUCASI9
    supports_volume: false
    type: Computer
    volume_percent: 0
1 Like

I apologize, Also this time the problem was not due to Home Assistant, but my Bose who occasionally have a problem with connection (problems with his app too and I have to reconfigure it).Sorry and thanks, now it works with both ID code and simple Bose name . Thanks

1 Like

Thank you so much for this integration! I always wanted to be able to transfer my music from the phone to my Sonos speakers (2x One SL) using Home Assistant and now I finally can!

The only issue I had (so far) is when transferring the playback it will not pass the rest of the queue but only the current playing song. To workaround this, I’ve created a script which I’m gonna share in case someone else is interested.

The script basically gets the current queue, extracts the track IDs and adds them to the Sonos queue for each track found, while transferring the current song to be played immediately instead.

If there is a better solution or I’ve just been dumb/blind, I’m all ears!

alias: "Music: Transfer Spotify playback"
sequence:
  - service: spotifyplus.get_player_queue_info
    metadata: {}
    data:
      entity_id: media_player.spotifyplus_bexem
    response_variable: result
  - variables:
      queue_length: "{{ result.result.queue | length }}"
      first_uri: >-
        {{ result.result.queue[0].uri if result.result.queue[0].uri is defined
        else 'No URI found' }}
      all_uris_except_first: "{{ result.result.queue[1:] | map(attribute='uri') | list | to_json }}"
  - service: spotifyplus.player_transfer_playback
    data:
      entity_id: media_player.spotifyplus_bexem
      device_id: NAMEORIDOFYOURSONOSSPEAKER
      play: true
      delay: 1
    enabled: true
  - repeat:
      for_each: "{{ all_uris_except_first }}"
      sequence:
        - service: media_player.play_media
          target:
            entity_id: media_player.YOURSONOSSPEAKER
          data:
            enqueue: add
            media_content_type: music
            media_content_id: "{{ repeat.item }}"
    enabled: true
description: ""
icon: mdi:transfer
2 Likes

@bexem

Sonos devices do not support the Spotify Web API Transfer Playback endpoint from commercial streaming applications. A 403 Forbidden, Restricted device error is generated by the Spotify Web API if Transfer Playback is issued to a Sonos device. Note that Spotify applications do not have this limitation (e.g. Desktop / Mobile applications) and can transfer playback at will, as they are not considered “commercial” applications.

The SpotifyPlus integration player_transfer_playback custom service gets around this limitation by issuing a SoCo Play command to the Sonos device, which instructs the device to resume play at the last position in the SPOTIFY_CONNECT music source. This works great when the queue was started by the Spotify Desktop or Mobile Player applications.

Note that things can get weird if the Sonos device music source is not set to SPOTIFY_CONNECT! If the target Sonos device music source is NOT set to SPOTIFY_CONNECT, then a local queue is built and the currently playing track from the source device is added to the queue and played via the SoCo API. A Soco seek command will then be executed to resume play at the specified point where play was transferred. Unfortunately, play will stop after the track ends as it is the only item in the queue. The HA UI for the media player will also report the source incorrectly, as the Spotify Web API will contain the source that IT thinks is selected; this source will be different than the Sonos device playing it’s local queue, as the device music source is not SPOTIFY_CONNECT!

I like your idea of calling the get_player_queue_info service to transfer the Spotify Connect queue to the target device. I will get this implemented in the next release. Note that the media_player.play_media queueing will not be used, as I will use the SoCo add_share_link_to_queue for each items returned from the get_player_queue_info results. It’s doing the same thing, but removes the requirement for the Sonos integration (e.g. media_player.YOURSONOSSPEAKER).

The problem still remains though, in that the entries transferred are being added to a local Sonos queue; play will stop after the last track ends, and the source will not report correctly to the HA UI as the Sonos device music source is not SPOTIFY_CONNECT.

UPDATE - a better way to explain it …
The HA media player UI for the SpotifyPlus integration reports the current status of the users Spotify player via the Spotify Web API Get Playback State endpoint. The Sonos device’s music source must be set to Spotify in order to reflect the correct status of the Sonos device in the HA media player UI for the SpotifyPlus integration. If the Sonos device is playing a non-Spotify music source (e.g. local queue items, QPlay, etc), then the status of the Sonos device will not be reflected correctly in the HA media player UI for the SpotifyPlus integration. This is because the Sonos device is no longer providing updates to the Spotify Web API, as the Sonos device music source is not set to Spotify. This can be confusing, especially if a Sonos local queue is built from a Spotify queue or playlist; the Sonos device appears to be playing Spotify content, but it’s really playing items from a local queue (that happen to be copied from a Spotify queue or playlist).

1 Like

FYI - just released a new version of the SpotifyPlus integration

[ 1.0.46 ] - 2024/08/04

  • Updated service player_transfer_playback to transfer the Spotify users queue (20 items max) to the Sonos device (instead of just the currently playing track). See the Sonos Limitations wiki documentation for further details about Sonos-related issues.

@bexem
Give the new release a try and let me know how it works for you.
Thanks.

Just tested and it works flawlessly so far! That simplifies the whole script by a lot.
I will let you know if I get any error, but hopefully not.

Also, thank you for the explanation earlier, I can see the effort you’ve put in your work, kudos and thank you for it. :slightly_smiling_face:

1 Like

Hello,

First thanks a lot for this integration, I was finally able to control Spotify easily on SONOS and Amazon Echo devices. And even with no latency where my other problem was the huge latency on the cards.

I have a question because I wasn’t able to find the information.
Is it possible to hide some devices in the drop down menu ?

For exemple currently I have :

  • Speaker 1
  • Speaker 2
  • Speaker 3
  • My personal iPhone

I want to remove my iPhone from this list because I don’t need it and don’t want the music to come through it by accident.

If it’s not possible yet, do you think you can add this feature please ?

Thanks

1 Like

That would be a good feature to have. I should be able to get that added in the next release. Will keep you posted.

FYI - just released a new version of the SpotifyPlus integration

[ 1.0.47 ] - 2024/08/05

  • Added configuration option to hide specified device names from the HA media player UI source list.

Note that you will need to clear your browser cache after installing the update, as option descriptions were added to string resources.

@Elrau
Give the new release a try and let me know how it works for you.
Thanks.

per the updated wiki documentation …

Hide Source List Devices

This option allows you to specify a list of device names (semi-colon delimited) that will be hidden from the HA media player UI source list. The device names listed here will not be displayed in the HA media player UI source_list attribute of device names. Note that hidden device names are still accessible via the various integration custom service calls though.

The device names can be specified case-insensitive (e.g. “bose-st10-1” and “Bose-ST10-1” specify the same device), and have leading / trailing spaces; all comparisons are done with lower-case, and leading / trailing spaces removed.

This option was introduced with integration v1.0.47.

Hi,

So fast …
Tested on my side and works perfectly as expected.

Thanks a lot for the feature !
I’ll let you know if I have some ideas.

Have a nice day

1 Like

Hi @thlucas

Thanks for putting this together and sharing with everyone.
This integration is a nice addition to the Core Spotify Integration.

I just successfully create the integration for my 3 spotify accounts (all part of my family plan).
Just so you know, I did not have to create an app per user.
In my main account, I created an app for SpotifyPlus, and there I added 2 others users to grant them access.
This process is the same as with the Spotify integration and is easier to deal with than having to create an app per user.

Side note; thanks for explaining the issue with Sonos; it helped to understand the issue I was facing with Spocast.

Finally, as I am here, it seems to me that your integration could nicely replace the core integration. Have you thought about it?

@NicoLeOca
Thank you for the kind words.

Regarding the single app ID reference, that will work fine if the added users do not want to access their own user content (e.g. private playlists, favorite artists / albums/ tracks, etc). You only need to create separate app id’s if they want to access their own favorites via HA.

Also note that the core Spotify integration is not required, and can be disabled if you like (or leave it enabled if you have script references).

I have thought about submitting the integration to core, but it seems there are quite a few hoops to jump thru with that process. I like the flexibility of the HACS install process, and it’s very easy for users to upgrade.

@thlucas
Thanks for your reply

indeed, being core has consequences.

I just checked. The other users can access their own content in SpotifyPlus, using the same app as mine.

@NicoLeOca
Regarding The other users can access their own content in SpotifyPlus, using the same app as mine, are you sure about that?

I am pretty sure that you will only see the main users’ favorites when you issue service calls to retrieve favorites for the user. For example, the following service call will retrieve the track favorites of the main user associated with the client id (there is no argument for a shared user-id):

action: spotifyplus.get_track_favorites
data:
  entity_id: media_player.YOURSPOTIFYPLUSENTITY
  limit_total: 100

Note that this is different than PLAYING a track / album / artist, as most of those are public entities (with country / market restrictions), and can be played by any user that can access the parent country / market.

Another way to test it is to use the HA Media Browser, and select the Playlists, Artists, Albums, Tracks categories. You will only see favorite content for the main id.

Not trying to dissuade you from using shared credentials, just want to make sure you understand how it works.

I do confirm that I have access to each user’s favorites (using their spotifyplus entity)


My oldest son just got his spotify account and only has one favorite

With your integration, registring shared users behaves similarly to what happens with the core Spotify integration.

1 Like

@NicoLeOca
Ahhhh, I see. I did not realize you were defining 3 separate services (1 per user) and sharing the same Spotify application definition. That makes sense, as you login separately with each entity when you add the service.

Awesome!

Hi,
I have an issue calling for this service
service: spotifyplus.player_media_play_track_favorites
I get an error “Bad format for parameter state”
Can you help me to idendify the issue please?