Extract Spotify URI from media browser - pipe to HA/Sonos

Since Spotify can’t use Sonos as a media player for its content (see e.g. this forum post: Media browser, Spotify, select output device?), the awesome Spotify media browser has limited value to Sonos owners. The same probably applies to other media players as well.

My question is, can we use the Spotify media browser, hijack the event when content is selected (what is currently set to “play”), grab the Spotify URI from that content and let Home Assistant call a play_media service with the extracted URI? That way, it doesn’t matter that Spotify doesn’t support the player natively. I think it would provide a lot of value for many users.

I don’t speak the front end language natively, but my studies into the front end repo has led me to hope that one can hijack the on media-picked-event (e.g. through the action param here: https://github.com/home-assistant/frontend/blob/0ec58007c960e19f0c8fc46a0f1efd1d714e9f5e/src/components/media-player/dialog-media-player-browse.ts#L41). But I don’t know if the Spotify URI is actually there for hass to grab when browsing the media.

Does anyone think this is doable? Does anyone see a way to achieve it?

Did you ever found a solution to this?

No, I’m sorry, I didn’t. But it would still be awesome if we could make it work.

@kristomi
I have made a custom component from the Spotify integration My-Hassio-config/config/custom_components/spotify at master · gieljnssns/My-Hassio-config · GitHub with a new attribute last_selected

When you click in the Spotify media browser this new attribute becomes the Spotify URI (some delay)

With this automation you can start playback on a Sonos player

alias: test
description: ''
trigger:
  - platform: state
    entity_id: media_player.spotify
    attribute: last_selected
condition: []
action:
  - service: media_player.play_media
    target:
      entity_id: media_player.sonos
    data:
      media_content_type: music
      media_content_id: |
        {{ state_attr("media_player.spotify", "last_selected") }}
mode: single

There is still the error No active playback device found

And you have to make sure the Sonos player you are trying to start is the coordinator and not the slave of any group.