SpotifyPlus Custom Integration

And why not use music assistant?

FYI - just released v1.0.20 of the SpotifyPlus integration

[ 1.0.20 ] - 2024/06/06

  • Changed all media_player.schedule_update_ha_state(force_refresh=True) calls to schedule_update_ha_state(force_refresh=False) to improve performance. Suggested by @bdraco, along with an explanation of why. Thanks @bdraco!
  • Changed logic to call session.hass.config_entries.async_update_entry via a hass.add_job call instead of calling directly. This fixes the issue of Detected that custom integration 'spotifyplus' calls hass.config_entries.async_update_entry from a thread other than the event loop, which may cause Home Assistant to crash or data to corrupt that was introduced with HA 2024.6 release.
  • Changed logic to access local file system files via a hass.async_add_executor_job call. This fixes the issue of Detected blocking call to open inside the event loop by custom integration 'X' ... that was introduced with HA 2024.6 release.
3 Likes

FYI - just released a new version of the SpotifyPlus integration

Apologies for the back-to-back releases, but the Spotify DJ exception bug was a pretty big deal as the DJ is a popular Spotify function.

[ 1.0.21 ] - 2024/06/07

  • Fixed a bug that caused player state to return an error every 30 seconds when playing the Spotify DJ playlist. As the Spotify Web API does not support retrieving the DJ playlist (spotify:playlist:37i9dQZF1EYkqdzj48dyYq), it simply returns a manually built representation of the Spotify DJ playlist with limited properties populated (uri, id, name, description, etc). Note that Spotify DJ support is not supported (as of 2024/06/07) by the Spotify Web API (this includes starting play, retrieving playlist details, retrieving playlist items, etc).
  • Added service zeroconf_discover_devices to discover Spotify Connect devices on the local network via the ZeroConf (aka MDNS) service, and return details about each device.
  • Added service zeroconf_device_getinfo to retrieve Spotify Connect device information from the Spotify Zeroconf API getInfo endpoint.
  • Added service zeroconf_device_resetusers to reset users for a Spotify Connect device by calling the Spotify Zeroconf API resetUsers endpoint.
  • Updated underlying spotifywebapiPython package requirement to version 1.0.44.
3 Likes

FYI - just released a new version of the SpotifyPlus integration

Apologies for the back-to-back releases, but each new release of HA lately seems to break things. Not complaining, as it’s just the nature of software updates sometimes.

[ 1.0.25 ] - 2024/06/08

  • Fixed a bug that was causing ValueError: list.remove(x): x not in list exceptions to be raised whenever the user changed configuration options for a service. This started appearing with the HA 2024.6.1 release.
2 Likes

FYI - just released a new version of the SpotifyPlus integration

[ 1.0.26 ] - 2024/06/10

  • Added service zeroconf_device_connect that calls the addUser Spotify Zeroconf API endpoint to issue a call to SpConnectionLoginBlob. If successful, the associated device id is added to the Spotify Connect active device list for the specified user account.
  • Added service zeroconf_device_disconnect that calls the resetUsers Spotify Zeroconf API endpoint to issue a call to SpConnectionLogout. The currently logged in user (if any) will be logged out of Spotify Connect, and the device id removed from the active Spotify Connect device list.
  • Removed service zeroconf_device_resetusers and replaced it with zeroconf_device_disconnect.
  • Updated underlying spotifywebapiPython package requirement to version 1.0.48.
2 Likes

@Fanful, @staceydodds @stennajay
I believe I have the “playback when another Spotify account is controlling the device” issue resolved. I added a new service to the 1.0.26 version of the SpotifyPlus integration that allows you to disconnect the current user on a device, then reconnect a different user to the same device.

Check out the wiki docs for the new Zeroconf Device Connect service. It will disconnect the current user from the device, and add a different user to the device; you can then issue the Play Transfer Playback service call to start play on the device.

Note that the username MUST match the account name (or one of them) that was used to configure Spotify Connect on the manufacturer device; you can’t just randomly select a Spotify account that is not known to the manufacturer device.

Use the ZeroConf Discover Devices service to retrieve the various parameters for the Connect service.

Example yaml:

service: spotifyplus.zeroconf_device_connect
data:
  entity_id: media_player.spotifyplus_john_s
  host_ipv4_address: 192.168.1.81
  host_ip_port: 8200
  cpath: /zc
  version: 1
  use_ssl: false
  username: [email protected]
  password: yourpassword
  pre_disconnect: true
  verify_device_list_entry: true

Let me know if it works for you or not. It should work, as it uses the Spotify Zeroconf API, which should be what your device uses to interface with Spotify Connect.

2 Likes

Hello @thlucas . I’m trying to follow your advice here.

A couple notes about the process, in case this might be useful for others.

  1. I think the help text on that field in the service visual editor is wrong (it’s a copy of the previous one):
    obraz
    The field doesn’t accept numbers, it’s a boolean. I haven’t found any mention of SSL in the discover device service results, so I just set this field as false.

  2. I didn’t have a Spotify password, because I used “Continue with Google” to sign up/login. However, it turns out Spotify lets you create a “device password” in the account settings on the website (inside the privacy and security section). So I created one and used it for this service.

And as for the result, I had to fiddle quite a bit, but I think I finally managed it. I will need to test this some more (and with it test the patience of my wife :smiley:), but it worked when I separately run the disconnect service, then the connect service without the pre-disconnect. In the end I managed to stop my wife’s session on the device and start playback from my account, all without touching the Spotify app.

1 Like

@Fanful
Glad to hear it is working for you.

Thanks for the notes on the SSL description; I will get that corrected with the next release today. Here is what it should have said:
Use SSL? True if the host device utilizes HTTPS Secure Sockets Layer (SSL) support; otherwise, False to utilize HTTP. Default is False (HTTP).

I also updated the ZeroConf Device Connect service wiki documentation with your “device password” notes. Thanks for that, as I don’t utilize the non-password login methods.

Can I ask what device hardware are you controlling? e.g. Sonos, Google Cast, Onkyo, etc. As well as what the CPath and Port numbers you are using to access your device(s)? I would like to add them to my ZeroConf Device Info by Manufacturer wiki documentation if you can supply me with the details.

FYI - just released a new version of the SpotifyPlus integration

[ 1.0.27 ] - 2024/06/12

  • Added extra state attribute media_playlist_content_id that contains the Content ID of current playing playlist context if one is active; otherwise, None.
  • Added property media_player.media_playlist_content_id that contains the Content ID of current playing playlist context if one is active; otherwise, None.
  • Added property media_player.media_playlist_content_type that contains the Content Type of current playing playlist if one is playing; otherwise, None.
  • Added property media_player.media_playlist_description that contains the Description of current playing playlist if one is playing; otherwise, None.
  • Added property media_player.media_playlist_image_url that contains the Image URL of current playing playlist if one is playing; otherwise, None.
  • Updated use_ssl description on all of the Zeroconf Device services.

Yamaha R-N402D, it’s a receiver with MusicCast.

Port: 80
CPath: /goform/spotifyConfig

1 Like

FYI - I put together a document on how to reawaken Spotify Connect device id’s that disappear after a certain time of inactivity. It utilizes various services from the SpotifyPlus integration to gather information about the device(s) and re-establish the connection to the Spotify Connect device list. Spotcast is not required by this process.

It contains an explanation of the problem, as well as a solution on how to re-awaken the device. The same procedure can also be used to switch Spotify accounts on a device, IF the device supports it (some devices only support one account, like my Bose SoundTouch unfortunately).

I have had good luck with Onkyo, Bose, PureSound, and Yamaha users that have been able to re-awaken devices with it. I would think it will work with Sonos and Google Cast devices too, or any device that natively supports the Spotify Connect Zeroconf API for that matter.

1 Like

@Fanful @staceydodds @stennajay @mhoogenbosch
FYI - just released a new version of the SpotifyPlus integration

[ 1.0.28 ] - 2024/06/19

  • Added service get_spotify_connect_devices that gets information about all available Spotify Connect player devices.
  • Added service get_player_now_playing that gets object properties currently being played on the user’s Spotify account.
  • Added service player_activate_devices that activates all static Spotify Connect player devices, and (optionally) switches the active user context to the current user context.
  • Added service player_resolve_device_id that resolves a Spotify Connect device identifier from a specified device id, name, alias id, or alias name. This will ensure that the device id can be found on the network, as well as connect to the device if necessary with the current user context.
  • Added service get_player_playback_state that gets information about the user’s current playback state, including track or episode, progress, and active device.
  • Added extra state attribute media_context_content_id that contains the Context Content ID of current playing context if one is active; otherwise, None.
  • Updated underlying spotifywebapiPython package requirement to version 1.0.59.

If you’re having issues with Spotify Connect “device not found” errors, then this is the release for you.

This release allows you to specify a Spotify Connect username and password in the configuration options. These credentials are then used by the various services that utilize a device_id parameter. It will verify that the device exists and is available, and will re-activate it automatically if need be. It will also take control of the device if another user is controlling it. You can also specify either a device ID value or the device name value, as well as any group alias id / name values (for multi-room use if configured).

Make sure you set the Spotify Connect username and password configuration options prior to using any of the following.
Figure 1 - New Spotify Connect Options

The Player Transfer Playback service will now transfer playback to either active or inactive devices, as well as switch the user context if need be.
Example:

service: spotifyplus.player_transfer_playback
data:
  entity_id: media_player.spotifyplus_john_s
  device_id: "Bose-ST300"
  play: true

I also added a new Player Activate Devices service that allows you to re-activate ALL of your Spotify Connect devices on the network with one service call.

Example:
Activate all Spotify Connect player devices on the local network and switch the device user context to our user context. This will disconnect other users from all spotify connect player devices defined to the local network.

service: spotifyplus.player_activate_devices
data:
  entity_id: media_player.spotifyplus_john_s
  verify_user_context: true
  delay: 0.5

Give it a whirl, and let me know if you have issues.

Thanks, and make it a great day!

3 Likes

I just installed the update. Something’s wrong with the config options, the 2 options, for the username and password, have no name/description:

I’m on Firefox.

I can’t get past this window:

There is a device playing spotify, but it doesn’t discover.

I’m in a bit of a loop. I can’t get info, because the service isn’t there. I can’t add a default playing device to start the integration.

@Fanful - That appears to be a browser caching issue.
I would reboot HA, then clear the cache, then go into the configuration options again.

1 Like

@mhoogenbosch
The zeroconf_device_getinfo service is still in place - can you call it to retrieve device details? Example:

service: spotifyplus.zeroconf_device_getinfo
data:
  entity_id: media_player.spotifyplus_todd_l
  host_ipv4_address: 192.168.1.81
  host_ip_port: 8200
  cpath: /zc
  version: 1
  use_ssl: false

You might also try calling the new Get Spotify Connect Services service to retrieve the device list. This is what the configuration options UI is calling to retrieve the device list.

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

If you cannot execute any SpotifyPlus service, then you might try the getInfo call from a desktop browser. Example:
http://192.168.1.82:8200/zc?action=getInfo&version=1.0

Also, are there any related messages showing up in the System Log?

UPDATE - is the Spotify Connect device you are not seeing using IPV4 to connect? or IPV6? I have not tried the device discover process with IPV6 protocol, as all of my gear uses IPV4.

You’re right, it was cache. I’m embarrassed I haven’t thought about that :smiley: .

1 Like

I’m the same guy from: Device not available after device standby / shutdown - [SOLVED] · thlucas1/homeassistantcomponent_spotifyplus · Discussion #11 (github.com). Got a bit of background story ;-).

But the integration doesn’t start at all. When I go back to .27 the device list resolves a device. but with .28 it doesn’t resolve any devices at all.

As for the service, I cannot run it, it results in an error:

Deze fout is ontstaan door een aangepaste integratie.

Logger: custom_components.spotifyplus
Bron: custom_components/spotifyplus/__init__.py:1991
integratie: SpotifyPlus (documentatie, problemen)
Eerst voorgekomen: 08:42:49 (1 gebeurtenissen)
Laatst gelogd: 08:42:49

SpotifyApiError: SAM0001E - An unhandled exception occured while processing method "GetInformation". 'str' object has no attribute 'get'
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/spotifywebapipython/zeroconfapi/zeroconfconnect.py", line 573, in GetInformation
    result = ZeroconfGetInfo(root=responseData)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/spotifywebapipython/zeroconfapi/zeroconfgetinfo.py", line 25, in __init__
    super().__init__(root=root)
  File "/usr/local/lib/python3.12/site-packages/spotifywebapipython/zeroconfapi/zeroconfresponse.py", line 32, in __init__
    self._ResponseSource = root.get('responseSource', None)
                           ^^^^^^^^
AttributeError: 'str' object has no attribute 'get'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/config/custom_components/spotifyplus/__init__.py", line 1991, in _update_devices
    scDevices = await hass.async_add_executor_job(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/spotifywebapipython/spotifyclient.py", line 7960, in GetSpotifyConnectDevices
    info:ZeroconfGetInfo = zconn.GetInformation()
                           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/spotifywebapipython/zeroconfapi/zeroconfconnect.py", line 584, in GetInformation
    raise SpotifyApiError(SAAppMessages.UNHANDLED_EXCEPTION.format(apiMethodName, str(ex)), ex, logsi=_logsi)
spotifywebapipython.spotifyapierror.SpotifyApiError: SpotifyApiError: SAM0001E - An unhandled exception occured while processing method "GetInformation".
'str' object has no attribute 'get'

@mhoogenbosch - Not a problem; you are one of the more experienced guys I have worked with in the past, and diagnosing your previous issue was pretty easy. Anyone that can run a WireShark trace is a stand-up guy my book. :smiley:

With that said, it might be a little bit involved tracking this issue down as you have quite a few (9 or more?) Spotify Connect devices on your network. I created a new GitHub Issue on the SpotifyPlus integration repository to track this.

Please respond via the issue when you get a chance.

Thanks!

1 Like

I’m going to look at the case tomorrow! This evening we need to support the national team :ok_hand: