SpotifyPlus Custom Integration

I tried your card code, but really don’t know why, clicking over the card of spotifyplus i don’t get the library browser as you show in the screenshot. I get nothing…

@shpongledsummer
I believe the playlist history id you are referring to is called a snapshot id in the Spotify Web API world. There is no Spotify Web API endpoint to list playlist snapshot id history. The only way to track it would be if you manually saved the snapshot id value returned from the AddPlaylistItems, ClearPlaylistItems, ReorderPlaylistItems, and ReplacePlaylistItems calls.

Also, AFAIK I don’t think there are any forms in the the Spotify Web App to list this information either.

UPDATE: I just found the Recover Playlist page in the Spotify web interface. Per the description … If you deleted a playlist within the last 90 days, you can get it back. Find the playlist you want to recover below and click Restore.

I’m not sure if that is for deleted playlists only (seeme like it is based on wording), or if it can be used to restore a past snapshot.

@maurizio53
What does your Sonos Card dashboard look like? Can you reply with a screen capture of your dashboard that contains the Sonos Card and with the SpotifyPlus player selected.

Mine looks like this:

To display the media browser:

  • click to select your SpotifyPlus player from the left pane (SpotifyPlus Todd L in my example).
    Note that the SpotifyPlus integration has to be turned on. I don’t think there is a way to do this in the Sonos Card, though I am not for sure.
  • click the Browse Media link in the right pane.
    This displays the HA media browser for the SpotifyPlus media player. Mine looks like this:

This is what i see after creating the dashboard:

After that i click in the right panel and i get the media browser for SpotifyPlus, but then clicking over any of the item to try a play i get the same error i notified you two days ago about the media_player.play_media service.

@maurizio53
The problem is that the SpotifyPlus integration is not turned on.

Add the following to your dashboard with the Sonos Card. It simply adds 2 buttons to power On and Off the SpotifyPlus media player. I know there are probably better ways to do this, as well as look better, but this is for testing.

square: false
type: grid
title: SpotifyPlus Power Options
columns: 2
cards:
  - type: button
    entity: media_player.spotifyplus_mfabiani53
    name: 'On'
    icon: mdi:power
    icon_height: 32px
    show_name: true
    show_icon: true
    tap_action:
      action: call-service
      service: media_player.turn_on
      data:
        entity_id: media_player.spotifyplus_mfabiani53
  - type: button
    entity: media_player.spotifyplus_mfabiani53
    name: 'Off'
    icon: mdi:power
    icon_height: 32px
    show_name: true
    show_icon: true
    tap_action:
      action: call-service
      service: media_player.turn_off
      data:
        entity_id: media_player.spotifyplus_mfabiani53

What it looks like:
image

Save your dashboard changes, and click the On button. This will issue a turn_on service call to the SpotifyPlus integration to turn it on. Once that happens, you should see the Player section of the Sonos Card come to life.

@maurizio53
I just noticed this in your previous reply …

Are you trying to use the SpotifyPlus integration with a Spotify FREE membership account? Or do you have a Spotify PREMIUM membership account? The Spotify Web API only supports player functions for Spotify Premium membership accounts. If you don’t have a Spotify Premium membership account, then you won’t be able to control the Spotify player. Note that this applies to both Spotify and SpotifyPlus integrations.

Ah, ok… but why using the Spotify integration i can play spotify songs?

Thanks for the answer. I guess some of the services like the playlist analysis don’t work without putting this playlist history id in the configuration, right?

Just to be clear, I’m taking about this input field:

@maurizio53
Are you saying that you can control (e.g. play, pause, skip next, volume up / down, etc) the Home Assistant Spotify integration player using a Spotify FREE membership account? If so then I will have to research that, as the Spotify Web API states that those functions are reserved for premium account users. I also verified this in my testing - I could not control the player when logged on as my Spotify free account.

Note that you can listen for free using the Spotify Web player via their web app, which is different than the Home Assistant Spotify (or SpotifyPlus) integration.

@shpongledsummer
I’m not familiar with MusixMatch, assuming that is what the input fields are for in your screenshot image. For the entry that says “Spotify Playlist History ID (not URI)”, I would assume it’s asking for a playlist ID value (e.g. 6W6yL4bv7LrD8belwn5Fi4) instead of a URI value (e.g. spotify:playlist:6W6yL4bv7LrD8belwn5Fi4). Note that playlist snapshot ID’s are a different format (e.g. MTk3LGEzMjUwZGYwODljNmI5ZjAxZTRjZThiOGI4NzZhM2U5M2IxOWUyMDQ) than the ID and URI formats.

FYI - just released v1.0.14 of the SpotifyPlus integration

  • Added service player_media_play_track_favorites to play all track favorites for the current user.
  • Increased all browse media limits from 50 items to 150 items.
  • Updated Media Browser logic to ignore Sonos-Card ‘favorites’ node requests, as there is no Spotify direct equivalent.
  • Updated underlying spotifywebapiPython package requirement to version 1.0.41.

FYI - just released v1.0.15 of the SpotifyPlus integration

  • Added MediaPlayerEntityFeature.VOLUME_MUTE support to handle volume mute requests.
  • Added MediaPlayerEntityFeature.VOLUME_STEP support to handle volume step requests.
  • Updated Media Browser logic to return an empty BrowseMedia object when ignoring Sonos-Card ‘favorites’ node requests, as a null object was causing numerous Browse Media should use new BrowseMedia class log warnings.
  • Updated underlying spotifywebapiPython package requirement to version 1.0.42.

I want to be able to use spotifyplus.player_transfer_playback but i do not think I fully understand how it works…

The standard scenario is that I start to play music from the spotify app on mobile and select my source there.

Trough spotcast i get media_player.spotify_marcus and use that to control spotify and the volumes of the speakers of the group that is playing. If I change song with that entity or in the app they both sync with the information. In other words, they control the same thing.

I want to add functionality in HA to chose between speakers/google home created speaker groups (a mix between cc audios and nest minis).

I thought that the Spotifyplus intregration would work the same way but media_player.spotifyplus_marcus remains “unactive” when i start from Spotify app on mobile.

Also when I start from media_player.spotifyplus_marcus nothing happens i my spotify app on mobile.

I can start and control music from media_player.spotifyplus_marcus so it shouldnt be a setup problem.

At the same time I have problems with speakers and chromecast audios going idle and not selectable from HA but thats another problem…

Some kind of workaround could be to get “playing” information (playlist, track etc…) from my spotcast media_player and somehow start that on my spotifyplus media_player but that dont realy sort my problem…

Help :slight_smile:

Hi there. I’m trying to display my Spotify playlists as images with the playlist sensor, but somehow I can’t get this sensor to retrieve my playlists. When I’m trying to run the ‘get playlists’ service an unknown error shows up.

This is the error from the logs:

Logger: homeassistant.components.websocket_api.http.connection
Quelle: custom_components/spotify_plus/playlists.py:159
Integration: Home Assistant WebSocket API (Dokumentation, Probleme)
Erstmals aufgetreten: 22:53:18 (1 Vorkommnisse)
Zuletzt protokolliert: 22:53:18

[140068242020160] Error handling message: Unknown error (unknown_error) Ben from 192.168.178.35 (Mozilla/5.0 (Linux; Android 13; 2112123AG Build/TKQ1.220829.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/123.0.6312.120 Mobile Safari/537.36 Home Assistant/2024.4.1-12576 (Android 13; 2112123AG))
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 27, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 794, in handle_execute_script
    script_result = await script_obj.async_run(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1713, in async_run
    return await asyncio.shield(create_eager_task(run.async_run()))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 457, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 509, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 539, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 507, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 736, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 699, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2543, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2580, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/spotify_plus/playlists.py", line 204, in spotify_playlists
    results = await asyncio.gather(*coroutines)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/spotify_plus/playlists.py", line 175, in process_playlist_async
    analysis = await analyze_playlist_async(playlist_id)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/spotify_plus/playlists.py", line 159, in analyze_playlist_async
    analysis_totals[attr] += track[attr]
                             ~~~~~^^^^^^
TypeError: 'NoneType' object is not subscriptable

@shpongledsummer
I believe you want to direct your question to the spotify_plus custom integration developer, as this thread is for the spotifyplus (no underscore) custom integration. They are 2 separate integrations.

Thanks for pointing this out, I wasn’t aware there are 2 different Spotify Plus integrations. Well time to try out your integration then :wink:

1 Like

@stennajay

I don’t think this is possible via the SpotifyPlus media player, as it does not support grouping.

Is the media_player.spotifyplus_marcus player turned on in HA? It will need to be turned on in order to receive updates from the HA state engine. It can also take anywhere from 1 - 30 seconds for the player to recognize status changes IF not controlling the player via SpotifyPlus (e.g. your mobile device, stock Spotify integration, SpotCast media player, etc).

The Spotify Connect Player is controlled by a device id value (not a name), no matter what you are using to control it (e.g. SpotCast, Spotify, SpotifyPlus, etc); I am not totally familiar with SpotCast, but I believe it somehow converts a name to a device id, which it then uses to interface with the Spotify Connect Web services to control the Spotify Connect Player.

Not sure about that one. I know when I start play via SpotifyPlus, it almost immediately tracks in the browser (Chrome) on spotify.com. The Mobile Spotify player may be different, not sure.

Ya, the idle (or lost device) problem is an issue with chromecast devices. What I wonder is if the device id changes between when a chromecast device is playing, stops and goes idle after 10 mins (or so), and then starts playing again. If the device id stays the same, then it should not be a problem. I know when I run that scenario with my Bose SoundTouch devices, the device id remains the same; the device will re-awaken and start playing. I am not for sure if that is a Bose thing that re-activates it, or not though. It could differ by manufacturer.

If the device id DOES remain the same, then it should be a simple call to the spotifyplus.player_transfer_playback to transfer playback to the specified device id with the play attribute set to true to start playing after the active player is transferred. Something like this:

service: spotifyplus.player_transfer_playback
data:
  entity_id: media_player.spotifyplus_marcus
  device_id: 6f66ef4fbf687bbba393d8c38599aad484b30901  # your chromecast device id
  play: true

I can use the above to transfer playback to any known device id (HA Bose SoundTouch, Web player on spotify.com, mobile device). The issue though is that the web and mobile player device id’s change every time you open a new web page or start the app. If your Chromecasts change the device id every time, then you will need to use SpotCast to switch, as SpotifyPlus is not smart enough to transfer playback to a device NAME (on a device ID).

Hope it helps!

FYI - just released v1.0.16 of the SpotifyPlus integration

  • Added extra state attribute spotifyplus_device_id that lists the Spotify Connect Player device id that is in use.
  • Added extra state attribute spotifyplus_device_name that lists the Spotify Connect Player device name that is in use.
  • Refer to the wiki documentation page for more details about custom state variables.

This update will allow you to display the device id that is currently active in the SpotifyPlus media player; use the developer tools \ state display to view the new state attributes. It should look something like this:

The Spotify Connect Player device id assigned to my Bose-ST10-2 player is 5d4931f9d0684b625d702eaa24137b2c1d99539c.

FYI - just released v1.0.17 of the SpotifyPlus integration

  • Added device name support to the following custom services that take a Spotify Connect Player deviceId argument for player functions. You can now specify either a device id or device name in the deviceId argument to target a specific Spotify Connect Player device. Services updated were: player_media_play_context, player_media_play_track_favorites, player_media_play_tracks, player_transfer_playback.
  • Updated underlying spotifywebapiPython package requirement to version 1.0.43.

Apologies for the back to back releases, but the Spotify Connect Player device name support is a big improvement over having to specify device id’s.

SpotifyPlus is now smart enough to transfer playback to a Spotify Connect Player device NAME (or a device ID if you still prefer it); like so:

service: spotifyplus.player_transfer_playback
data:
  entity_id: media_player.spotifyplus_johnsmith
  device_id: "Web Player (Chrome)"
  play: true

This is a big deal for web and mobile player users, as the Spotify Connect Player device id’s for those devices change every time you open the app or a new browser tab / window.

Thank you so much for the fast respons and for the updates you did on the integration!

I have created the speaker groups in google home and they are selecteble from Spotify app on mobile. I also get the group in the source list at the state display for spotifyplus list when selecting them from Spotify app. I´m 98% sure that they was selecteble in the spotify integration a year (or more) ago so something has changed.

After your messege i tried again with the “media_player.spotifyplus_marcus” and got the same result as the “usual” media_player.spotify_marcus. So that problem sorted it self out when i was sleeping…

Regarding chromecasts/nest home they get the state “off” after a while when not playing. When that state changes to off they disappear. Thanks to your update I got the device_id and device_name. I tried the transfer_playback with both but get “error running action - Device not found”

This looks like a chromecast problem and i have no other sources to test this with.

The strange thing is that i have a automation for my doorbell that uses “Media player: Play media”. That automation plays a localy saved doorbell sound on all selected chomecast even if their previous state was “off”. In other words its possible to send media to a cc that is off…

I also tried to send my doorbell command to wake my cc speakers and directly after use the spotifyplus.player_transfer_playback but without luck…

After around 5 hours googleing and trying in HA I deleted the Spotify integration in HA and installed it again and I finaly got a list of all my speakers/speaker groups in that card. Still no luck in SpotifyPlus integration though.