Spotcast - custom component to start playback on an idle chromecast device

Thank you Jason.
Yes, the spotcast.start service exists. I do not see any entities named sensor.chromecast_devices, sensor.playlists_sensor or anything else similar. I do have a media player entity for my chromecast device though (perhaps that’s what you meant?), called ‘media_player.study_display’.

I tried using spotcast.start from devtools, but that produced an error. My script is exactly the same is the one I previously used on my old HA install which worked flawlessly.

I’m wondering if there is a problem with me using the existing Spotify ‘App’ on the Spotify Developers page that I used for my old HA installation? Should I create a new one?

Your study media player was set up by the cast integration and is independent of spotify or spotcast.

If the device and playlist sensor do not exist, I believe you spotcast integration is not set up correctly.

First, are you sure the Spotify integration itself is working properly? You need both the HA Spotify integration and Spotcast for Spotcast to work. If yes (ie - you have a working spotify media player in your config) , then I am going to guess that there is something wrong with your spotcast config - most likely the sp_key and sp_dc cookie values have changed.

I would go through the spotcast configuration again carefully, recopying the cookie values into your config. If you’ve done all that and it’s still not working post your script here and we’ll have a look to see if the problem is with that.

Are you specifying a device when you call spotcast?

image

When you click a media player card you’ve already selected which devices to play on. When you call spotcast, you need to tell it what device to play on.

I think what you’re seeing with the media browser is the built in Spotify integration media player “loses” the available source devices after a while (this is the reason spotcast is a thing). While the spotify media player still has source device in “memory” you can start spotcast and it will default to the sources still in memory. After that you need to specify the device again when you call spotcast.

Thanks, i am really learning a lot of new stuff reading your posts…

I am specifying a device, though I notice I am using the spotify_device_id (which I thought was the preferred way to do it as that was in the first code example on github) and not the device_name you use in your screenshot. Let me test that to see if it makes a difference!

Let me know how you make out. I’m curious if were you trying to use the Home Assistant device ID or the Spotify Device ID? Personally I’ve never bothered with the spotify device id, using the device name and/or entity id work fine so I never saw the point.

Good news! Using the device name in the service call fixed it. I’m not sure why this works differently than the device ID, especially because I assumed from the way the docs talk about it that the device ID is a more direct link to the device, but super happy that this easily solves it! @DIY-techie using device_name instead of spotify_device_id fixed it for me.

Just for added info, I was using the correct Spotify Device ID. The service call also works with this ID later, as long as the cast device has been activated via another route. But it won’t pull it out of idle.

1 Like

Unfortunately not. A friend of mine copied my code into his HA and just changed the entities and it works fine. So I know at least it isn’t the code but something with the spotcast install. Problem is I have re-installed spotcast and it’s still not working. I don’t want to give up on it as I really need this functionality.

How did you verify you credentials are correct?
Have you tried calling spotcast.start right from Developer Tools → Services?

service: spotcast.start
data:
  device_name: Dining Room Hub
  uri: spotify:playlist:37i9dQZF1DWSK8os4XIQBk

What happens when you do this?

If it didn’t work, set the spotcast level to debug and try again.

service: logger.set_level
data:
  custom_components.spotcast: debug

What do the logs say?

I configured a Mini Media Player with a Spotcast shortcut to start a playlist on one of my children’s Echo dots using the device ID specific to one of the accounts under my Spotify Family plan and the device. Initially, it works as expected, but within 1-2 days it will randomly stop working giving a generic device not found error when I tap the button:

Failed to call service spotcast/start. http status: 404, code:-1 - https://api.spotify.com/v1/me/player/play?device_id=<THEDEVICEID>: Device not found, reason: None

When I go to verify the Device ID using the same steps as before, I find (1) I have to say “Spotify Connect” and tap the Echo device in my Spotify app to get it to re-establish the connection and make it available for retrieval and (2) the Device ID has changed.

I think the problem is that this Echo device (and most others throughout my house) are logged into my primary Amazon Household account which is tied into the Spotify skill using a different Spotify account under the same Family Plan.

Has anyone else had a similar issue? Any ideas?

I have 3 spotify accounts. One is the “house” or HA account which is generally used if spotify is started by HA. This was done so HA starting spotify doesn’t interrupt our personal accounts if we are using them somewhere else (ie our phones)

The others are my wifes and my own personal accounts. If either of us start spotify via voice with the google assistants or from our phones Spotify is played using our respective accounts. (We can also use our own accounts from the UI if desired, for example if someone else is using the house account somewhere else or we want to play a personal playlist that isn’t on the house spotify account).

I have to account for all of these in my automations and scripts because one or more accounts may be in use at a time.

As for your error… try changing you service call from spotify_device_id to device_name. That seems to be much more reliable. See this recent post and the reply.

- service: spotcast.start
  data:
    device_name: Dining Room Hub
    uri: spotify:playlist:37i9dQZF1DXbYM3nMM0oPk

Thank you. I had read those posts and gave device_name a try, but using that same syntax, I keep getting ‘Failed to call service spotcast.start. Unknown error’ in the Developer|Services Tools and ‘Failed to call service spotcast/start. ‘NoneType’ object is not subsriptable’ when actually trying it with a service call from Mini Media Player. The only seemingly relevant difference between our setups is Google vs Echo devices. Does Google allow for more than one Spotify account to be connected to a Google Assistant (Home?) device at a time?

EDIT: I have also tried the media_player entity_id. Same errors.

Yes, but I don’t think that should matter here?
Have you tried the spotcast call specifying the account?

- service: spotcast.start
  data:
    account: <your account name>
    device_name: Dining Room Hub
    uri: spotify:playlist:37i9dQZF1DXbYM3nMM0oPk

One of my children who is older has their own Spotify account (under our Family plan). I have configured spotcast with their sp_dc and sp_key and successfully used that account in their original preset buttons.

However, going by this, the main house account is configured without a name (a default of sorts).

I’ve tried using that default account (no account specified) and my child’s account. Same issues.

Here’s a debug log:
Logger: homeassistant.helpers.script.websocket_api_script
Source: custom_components/spotcast/spotcast_controller.py:187
Integration: Spotcast (documentation, issues)
First occurred: 3:34:02 PM (1 occurrences)
Last logged: 3:34:02 PM

websocket_api script: Error executing script. Unexpected error for call_service at pos 1: ‘NoneType’ object is not subscriptable

Traceback (most recent call last): File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 447, in _async_step await getattr(self, handler)() File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 680, in _async_call_service_step await service_task File “/usr/src/homeassistant/homeassistant/core.py”, line 1627, in async_call task.result() File “/usr/src/homeassistant/homeassistant/core.py”, line 1670, in _execute_service await self._hass.async_add_executor_job( File “/usr/local/lib/python3.9/concurrent/futures/thread.py”, line 58, in run result = self.fn(*self.args, **self.kwargs) File “/config/custom_components/spotcast/init.py”, line 190, in start_casting spotify_device_id = spotcast_controller.get_spotify_device_id( File “/config/custom_components/spotcast/spotcast_controller.py”, line 200, in get_spotify_device_id spotify_device_id = self._getSpotifyConnectDeviceId(client, device_name) File “/config/custom_components/spotcast/spotcast_controller.py”, line 187, in _getSpotifyConnectDeviceId for device in devices_available[“devices”]: TypeError: ‘NoneType’ object is not subscriptable

Hmm. So in my spotcast setup I redefine the default account to give it a name. I would try that and try calling it with that account.

Another question. Do you see the media player you are trying to target in sensor.chromecast_devices attributes? I’m wondering if your Echo is being picked up as a Spotify Connect device for spotcast.

Have you ever succesfully started Spotify with spotcast on that echo?

Edit. Nvm. I see above it was working.

spotcast: # https://github.com/fondberg/spotcast
  sp_dc: !secret HASSIO_SP_DC
  sp_key: !secret HASSIO_SP_KEY
  accounts:
    hassio:
      sp_dc: !secret HASSIO_SP_DC
      sp_key: !secret HASSIO_SP_KEY
    jazzyisj:
      sp_dc: !secret JAZZYISJ_SP_DC
      sp_key: !secret JAZZYISJ_SP_KEY
    sherigagnon:
      sp_dc: !secret SHERI_SP_DC
      sp_key: !secret SHERI_SP_KEY

Hmmm…I gave it a try. Here’s my updated !include’d yaml

sp_dc: !secret spotcast_family_sp_dc
sp_key: !secret spotcast_family_sp_key
country: US
accounts:
  family: #new line
    sp_dc: !secret spotcast_family_sp_dc #new line
    sp_key: !secret spotcast_family_sp_key #new line
  dad:
    sp_dc: !secret spotcast_dad_sp_dc
    sp_key: !secret spotcast_dad_sp_key
  child:
    sp_dc: !secret spotcast_child_sp_dc
    sp_key: !secret spotcast_child_sp_key

I restarted HA and tried the service call again. Same error.

Is the media player listed in sensor.chromecast device attributes?
Are you trying to start spotify directly from dev_tools to test it?
Can you post the service call yaml you are using?

No. These are Amazon Echo (Spotify Connect) devices presented as media_player entities in HA.

Here is the service call I’m using to test.

service: spotcast.start
data:
  uri: spotify:playlist:37i9dQZF1EIYtCYWOqKQq0
  force_playback: true
  account: family
  device_name: Echo Dot Living Room

Ah. I have all cast devices. I thought the chromecast_devices would have picked up the Spotify Connect devices also. And yeah your service call looks ok…

Do you have any other media players that spotcast does work on? Do you have any spotcast errors on startup? You’re sure the credentials haven’t expired?

You might have to open an issue on the github. I think I’m all out of ideas.

I’m fairly certain the sp_dc and sp_key are still good since the service call works with a renewed Spotify Device ID, but never the other two “device types.” But, I’m not certain the token isn’t expiring after 1-2 days. Wouldn’t an expired token only affect the use of a Spotify Device ID though?

I’ll head over to GitHub. Thanks for your help!