Trying to play Spotify album on Sonos

I am trying to run a spotify album on an Sonos music_player. But it fails with an error message.

Action:

entity_id: media_player.kueche
media_content_id: spotify:album:6wiUBliPe76YAVpNEdidpY
media_content_type: playlist

The result is:

Logger: homeassistant.components.sonos.media_player
Source: components/sonos/media_player.py:1035 
Integration: Sonos (documentation, issues) 
First occurred: 22:18:17 (2 occurrences) 
Last logged: 22:19:55

Could not find a Sonos playlist named "spotify:album:6wiUBliPe76YAVpNEdidpY"

Any other idea to get this running. According to https://github.com/home-assistant/core/pull/9221 it should work. Any ideas?

It looks like the pull request is just related on Sonos-Album but not especially related on spotify on sonos.

1 Like

Hi Patrick,

Did you manage to play a Spotify album through Sonos?
Iā€™m also interested.

No. I am still looking for a good solution. I hoped that media browser could be a way to go. But could not get it to run.

Have you tried adding Spotify to My Sonos? It should then become available through the ā€œsourcesā€ list on a HA media card. Iā€™m not a Spotify user, but I have access to several radio stations this way.

I had a lot of trouble with the media browser too. Finally got it to work by deleting all references in configuration.yaml and doing a new UI integration. Quite impressive, but still a bit cranky - thereā€™s one speaker it will not recognise. The only difference from the others that I can see is that power is switched off overnight.

Havenā€™t tried controlling Sonos from scripts etc. yet.

It just offers me my Sonos local library. It does not even look on the Sonos favorites.

I thought this integration was an option https://www.home-assistant.io/integrations/spotify/ ?
But Spotify acts as a media player device, not as a source.
I have used https://github.com/jishi/node-sonos-http-api for magic cards (runs as a docker container)
Thinking about turning the nodes sonos http api into a HA addon.

1 Like

If you add the Spotify album to My Sonos it should appear as a source in Home Assistant.

Iā€™ve done that before, add to My Sonos so that HA recognizes the playlist (which can be an album). But I donā€™t know if I want 50 albums in My Sonos just for the HA integration. A great advantage is that it can be anything Sonos supports.
Not having to add to My Sonos would be one less step. Although youā€™ll need to get the album ID from Spotify doing it the other way. So thatā€™s also cumbersome.

Even in My Sonos there is a limit - I wanted to use this method to add some albums and it seems there is a limit around 60-70 favorites in My Sonos.
The only solution I see is to integrate the node sonos http api project into Home Assistant as it allows to chose spotify albums for Sonos via the spotify URI. If you find the time to do this @DavidDeSloovere it would really be awesome!

In the meanwhile I found a workaround involving a spotify connect device other than the Sonos speaker (e.g. echo dot).
See my post on this thread: Playing Spotify playlist on Sonos triggered by automation

@DavidDeSloovere @rcma @patrickse

I just added the ability to enqueue Spotify albums to the Sonos integration. You can try it out as a custom integration with HACS, see https://github.com/amelchio/sonos

- service: media_player.play_media
  data:
    entity_id: media_player.office
    media_content_id: https://open.spotify.com/album/1bt6q2SruMsBtcerNVtpZB
    media_content_type: music

- service: media_player.play_media
  data:
    entity_id: media_player.office
    media_content_id: spotify:track:7rPzEczIS574IgPaiPieS3
    media_content_type: music
    enqueue: true

Note, so far it only adds to the queue; you will have to make another service call to make it play.

4 Likes

That really sounds great. But why is the media_entity_id not in the form of
spotify:album:1bt6q2SruMsBtcerNVtpZB. Anyway, sounds awesome and I will test it tomorrow.

I am building an automation where my kids can start a random paw patrol album on Spotify playing on their Sonos speakers by pressing a push button. :love_you_gesture:

That form works as well, I listed two examples :slight_smile:

I am working on making it play immediately when dropping enqueue: true but I will be busy with other things for a few days.

Started wondering whether it should be media_content_type: music or media_content_type: playlist like in the OP. I am not quite sure what each is supposed to mean.

Anyway. Great work. :+1::+1: thanks a lot. I will try to embed it into my automation this weekend.

@amelchio

Got my scripts working. ItĀ“s a mixture of the Tags example with a random selection of an album from artist. My kids are happy, they can now switch between playing Paw Patrol and Super Wings and everytime a card is scanned it will play a random storyā€¦

ThereĀ“s only another thing I am still missing. Maybe you can give me a hint how I can achieve this. My wife always activates the ā€œrepeat modeā€ on the sonos speaker. I havenĀ“t found a way to disable repeat mode by using scripts or automationā€¦

Do you have an idea where to start?

@amelchio donā€˜t want to stress you. But I forked your repo and implemented the feature for my installation. Iā€˜ve implemented in a Sonos service like this

sonos.set_play_mode

With the data field like this

data:
  entity_id:
  play_mode: NORMAL

I guess that this is not the best way to do it. If you point me in a direction how the service should be called and if youā€˜re open for contributions, I could finalize this feature on my own.

What do you think?

Great that you got it working! Is enqueueing okay for you or are you missing a way to play immediately?

There is already a shuffle mode so I think you should implement repeat mode orthogonal to that. There is a repeat property in pysonos that you can use: https://github.com/amelchio/pysonos/blob/a63c4edbce8867f747fd3f6fd2682b8cfb9577d0/pysonos/core.py#L439-L453

However, I think repeat is fairly common so it should probably be implemented at the media_player level which is a bit more work.

Enqueueing is ok for me. But maybe play immediately should be implemented too. To make it a bit more polished.

I will have a look at the shuffle example, but I guess I will no be able to contribute to the media_player that easily.

I updated the custom integration, now enqueue: true is no longer required.

Let me have a go at adding media_player support for repeat mode.