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

Sorry, not at all… can you do it for me?

I can certainly give you step-by-step instructions, but the relevant file is found in the custom_components folder on your HA device, so you’d have to do the editing bit for yourself.

Do you already use a file editing Add-on, or otherwise have access e.g. via a Samba share to the files in the config directory on your HA machine? (In this last case you would be copying the file and editing it in a file editor e.g something like Notepad++).

Yes, if you give me step by step instructions i can edit the file… i use Visual Studio Code…

Ok then. By the way, this specific edit is UNTESTED - I have made other edits to this file successfully, in my case to add further playlist sensors for each of our household, and also add user album sensors too - so please make sure you have a back up to restore from should it all go wrong. Although actually the worst case scenario is really just that the 2 sensors for Spotcast won’t load.

Start by finding the folder for the Spotcast component: in my case that’s /config/custom_components/spotcast. The file you are looking for is called sensor.py.

Make a copy of this file to keep as a backup - you could call it sensor.py.old for example. Then if it goes wrong, you could delete the newer, edited version of the file, and restore the original by naming it back to sensor.py.

With the backup in place, you can start editing sensor.py. You are going to comment out several lines using the # character at the start of each line - this will just stop them from executing by effectively turning them into comments.

First comment out line 34:

    add_devices([ChromecastPlaylistSensor(hass, country)])

that is make it look like this:

#    add_devices([ChromecastPlaylistSensor(hass, country)])

Now do the same for lines 81 - 122.

Starting here:

 class ChromecastPlaylistSensor(SensorEntity):

and ending here (at the end of the file):

        self._state = STATE_OK

Don’t miss commenting out any of the lines in between! Once you’re happy with your edits, you will need to restart HA. Once HA has started, check the logs for any errors which reference the Spotcast component, or specifically the creation of sensors for it. If there are no errors, you should be good to go. If there are errors, just revert your edits, and maybe try again.

The sensor.playlists_sensor may still be restored as an unknown, non-functional entity on the first restart, but it won’t be updating and shouldn’t cause any of the errors you have been seeing, and should in any case just disappear after a few restarts. (You won’t be able to remove it via the UI as it has no unique id).

Finally, bear in mind that should the Spotcast component be updated in future, the update will overwrite any edits you have made, so you would have to go back and do them again. Of course, a future update could change the contents of the sensor.py file, so you would need to be sure that you are only commenting out the bits that create the playlist sensor.

Hope this is all clear - apologies if I’ve made the instructions too detailed and basic, but I thought it better to be sure.

1 Like

Thanks a lot!

The integration stopped working for me (at least chromecast casting )

It’s stopped working for me as well 2 days ago.
i havn’t touched it or changed anything and it simply stopped working.
i’m getting this error:
KeyError: ‘deviceID’

[BedRoom display(192.168.XX.XX):8009] Exception caught while sending message to controller SpotifyController: Message urn:x-cast:com.spotify.chromecast.secure.v1 from 459669df-58f1-4a3f-B8d7-da4907e463df to sender-0: {‘type’: ‘getInfoResponse’, ‘payload’: {‘version’: ‘2.9.0’, ‘publicKey’: ‘empty’, ‘remoteName’: ‘Spotify on Cast’, ‘deviceType’: ‘cast_video’, ‘brandDisplayName’: ‘google’, ‘modelDisplayName’: ‘Chromecast_Assistant_Touch’, ‘libraryVersion’: ‘5.30.3’, ‘resolverVersion’: ‘1’, ‘groupStatus’: ‘NONE’, ‘deviceAPI_isGroup’: False, ‘tokenType’: ‘accesstoken’, ‘clientID’: ‘d7df0127fb71444ea994202cb481ee2’, ‘productID’: 0, ‘scope’: ‘streaming’, ‘availability’: ‘’, ‘spotifyError’: 0, ‘status’: 101, ‘statusString’: ‘OK’}}

This is a known issue due to a change in the Spotify API and while a solution is being worked on, it is not necessarily solvable.

You can follow progress here.

1 Like

The spotcast integration is working again. The most recent version of spotcast (3.7.1) resolved the missing deviceID error.

Sorry, I’m a newbie, I followed the entire guide for the dual account but at the end I received an error and nothing works. I therefore tried to simplify everything by using a single account, configured therefore:

in the end of Configuration.yaml I add:

  sp_dc: !secret sp_dc
  sp_key: !secret sp_key
  country: IT #optional, added in 3.6.24

I create a new file secrets.yaml (in the same position of configuration.yaml)

sp_dc: [AQD3NgcJrkpdUaH9zgRAtZMUa2dgvkgGpuzzvBAaFpYI0H8EVMGdj1L32QlJEEEBF_mRfxPncN5yzA17-aAOVKILBt_l_SBn5UBchL_xMpj0h00_q5ke0_bq9fGmH5emCCE14zej7tKQ5RM81Zet7]
sp_key: [a1d86hgkc2cf-4fa4-b878-97e0f9672726]

(in this post in the string code, I change same letter for my privact, but I write the code for to be sure, is correct my file sorry)

I receive this error:
Invalid config for [spotcast]: value should be a string for dictionary value @ data[‘spotcast’][‘sp_dc’]. Got [‘AQD3NgcJrkpdUaH9zgRAtZMUa2dgvkgGpuzzvBAaFpYI0H8EVMGdj1L32QlJEEEBF_mRfxPncN5yzA17-aAOVKILBt_l_SBn5UBchL_xMpj0h00_q5ke0_bq9fGmH5emCCE14zej7tKQ5RM81Zet7’]
value should be a string for dictionary value @ data[‘spotcast’][‘sp_key’]. Got [‘a1d86hgkc2cf-4fa4-b878-97e0f9672726’]. (See /config/configuration.yaml, line 20).

Line 20 is the line where I start: spotcast:

if I write the codes directly in the configuration file it doesn’t give errors, so I think I did something wrong. could anyone help me? really hard at the beginning

Try removing the square brackets from around the sp_dc and sp_key values in your secrets.yaml file.

Open and close square brackets, ‘[’ and ‘]’, are generally used in yaml to define a list, so I think that’s what is causing the error - Spotcast needs a string not a list for these values.

1 Like

Thank you very much, it was just a problem with the square brackets! Now there are no problems restarting, I just have to figure out what I’ll be able to do hahaha

Frustratingly, I am seeing this error over and over again. The funny thing is - playing starts just completely fine for about 1 sec, and then it stops (probably due to this check/error):

2023-10-04 10:58:29.236 ERROR (MainThread) [homeassistant.components.script.start_music_t] Slaaptijd: Error executing script. Unexpected error for call_service at pos 2: Timeout when waiting for status response from Spotify app
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 468, in _async_step
await getattr(self, handler)()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 704, in _async_call_service_step
response_data = await self._async_run_long_action(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 666, in _async_run_long_action
return long_task.result()
^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2012, in async_call
response_data = await coro
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2056, in _execute_service
return await self._hass.async_add_executor_job(target, service_call)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/spotcast/__init__.py", line 221, in start_casting
spotify_device_id = spotcast_controller.get_spotify_device_id(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/spotcast/spotcast_controller.py", line 301, in get_spotify_device_id
spotify_cast_device.start_spotify_controller(access_token, expires)
File "/config/custom_components/spotcast/spotcast_controller.py", line 104, in start_spotify_controller
sp.launch_app()
File "/config/custom_components/spotcast/spotify_controller.py", line 110, in launch_app
raise LaunchError(
pychromecast.error.LaunchError: Timeout when waiting for status response from Spotify app
2023-10-04 10:58:29.251 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140663668132032] Timeout when waiting for status response from Spotify app
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 226, in handle_call_service
await hass.services.async_call(
File "/usr/src/homeassistant/homeassistant/core.py", line 2012, in async_call
response_data = await coro
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2049, in _execute_service
return await target(service_call)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 605, in _service_handler
response = await self._async_start_run(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 563, in _async_start_run
script_result = await coro
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 594, in _async_run
return await self.script.async_run(script_vars, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1578, in async_run
return await asyncio.shield(run.async_run())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 420, in async_run
await self._async_step(log_exceptions=False)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 470, in _async_step
self._handle_exception(
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 493, in _handle_exception
raise exception
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 468, in _async_step
await getattr(self, handler)()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 704, in _async_call_service_step
response_data = await self._async_run_long_action(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 666, in _async_run_long_action
return long_task.result()
^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2012, in async_call
response_data = await coro
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2056, in _execute_service
return await self._hass.async_add_executor_job(target, service_call)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/spotcast/__init__.py", line 221, in start_casting
spotify_device_id = spotcast_controller.get_spotify_device_id(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/spotcast/spotcast_controller.py", line 301, in get_spotify_device_id
spotify_cast_device.start_spotify_controller(access_token, expires)
File "/config/custom_components/spotcast/spotcast_controller.py", line 104, in start_spotify_controller
sp.launch_app()
File "/config/custom_components/spotcast/spotify_controller.py", line 110, in launch_app
raise LaunchError(
pychromecast.error.LaunchError: Timeout when waiting for status response from Spotify app

edit: October 9th I’m not sure, but maybe the problem is not with spotcast, but simply with the Spotify integration on Home assistant

Here I am, I’m still the one who needs your help.
I wanted to use this integration mainly for just one reason: to be able to set in a single automation everything my house needs to do when I put my daughter to bed, including the lullaby playlist, without having to use Google Home routines.
I achieved my goal and I’m happy, but I have a big problem, and I don’t know what it depends on, I think it’s within this integration and the changes made to spotify developper…
When I’m at home and I ask google home “hey google play music” in my Italian language, but the meaning doesn’t change, every time the same songs start in order, which I’ve certainly already listened to, but they’re not in any playlist and I don’t know how to do otherwise.
Before, if I was out in the car or on foot for a run listening to Spotify and I got home, at the command “hey google play music” at home, the music would start again at the same point, but now it’s always that song that I’m starting to hate for this reason.
Do you know if this integration has anything to do with it and if it is possible to replace this with the continuation of the music?
Thank you

I’ve the same problem, the service media_player.select_source for spotify in combination with an echo (or any other device) doesn’t realy change the Source…

My code:
service: media_player.select_source
entity_id: media_player.spotify_daniel
data:
source: “Echo Dot”

could you tell me what ssh command you used to fix this?

Any news on this? I’m still struggling with this issue and haven’t found a solution yet.

same error but only since 2023.11.1

2023-11-04 18:23:17.161 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.playlists_sensor fails
Traceback (most recent call last):
File “/config/custom_components/spotcast/spotcast_controller.py”, line 177, in get_spotify_token
).result()
^^^^^^^^
File “/usr/local/lib/python3.11/concurrent/futures/_base.py”, line 456, in result
return self.__get_result()
^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/concurrent/futures/_base.py”, line 401, in __get_result
raise self._exception
File “/config/custom_components/spotcast/spotcast_controller.py”, line 216, in start_session
raise TokenError()
custom_components.spotcast.spotcast_controller.TokenError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 696, in async_update_ha_state
await self.async_device_update()
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 959, in async_device_update
await hass.async_add_executor_job(self.update)
File “/usr/local/lib/python3.11/concurrent/futures/thread.py”, line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/config/custom_components/spotcast/sensor.py”, line 116, in update
resp = self.hass.data[DOMAIN][“controller”].get_playlists(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/config/custom_components/spotcast/spotcast_controller.py”, line 394, in get_playlists
client = self.get_spotify_client(account)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/config/custom_components/spotcast/spotcast_controller.py”, line 262, in get_spotify_client
auth=self.get_token_instance(account).access_token
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/config/custom_components/spotcast/spotcast_controller.py”, line 169, in access_token
self.ensure_token_valid()
File “/config/custom_components/spotcast/spotcast_controller.py”, line 165, in ensure_token_valid
self.get_spotify_token()
File “/config/custom_components/spotcast/spotcast_controller.py”, line 187, in get_spotify_token
raise HomeAssistantError(“Could not get spotify token.”)
homeassistant.exceptions.HomeAssistantError: Could not get spotify token.
2023-11-04 18:24:17.167 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.playlists_sensor fails
Traceback (most recent call last):
File “/config/custom_components/spotcast/spotcast_controller.py”, line 177, in get_spotify_token
).result()
^^^^^^^^
File “/usr/local/lib/python3.11/concurrent/futures/_base.py”, line 456, in result
return self.__get_result()
^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/concurrent/futures/_base.py”, line 401, in __get_result
raise self._exception
File “/config/custom_components/spotcast/spotcast_controller.py”, line 216, in start_session
raise TokenError()
custom_components.spotcast.spotcast_controller.TokenError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 696, in async_update_ha_state
await self.async_device_update()
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 959, in async_device_update
await hass.async_add_executor_job(self.update)
File “/usr/local/lib/python3.11/concurrent/futures/thread.py”, line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/config/custom_components/spotcast/sensor.py”, line 116, in update
resp = self.hass.data[DOMAIN][“controller”].get_playlists(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/config/custom_components/spotcast/spotcast_controller.py”, line 394, in get_playlists
client = self.get_spotify_client(account)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/config/custom_components/spotcast/spotcast_controller.py”, line 262, in get_spotify_client
auth=self.get_token_instance(account).access_token
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/config/custom_components/spotcast/spotcast_controller.py”, line 169, in access_token
self.ensure_token_valid()
File “/config/custom_components/spotcast/spotcast_controller.py”, line 165, in ensure_token_valid
self.get_spotify_token()
File “/config/custom_components/spotcast/spotcast_controller.py”, line 187, in get_spotify_token
raise HomeAssistantError(“Could not get spotify token.”)
homeassistant.exceptions.HomeAssistantError: Could not get spotify token.

Need some help please. I have no issues running spotcast with the default account, but even after adding the second account, the secret keys to secrets.yaml, and the second account to config.yaml. I can not get it to trigger. Can anyone share their code for multiple accounts.

in config.yaml, secrets.yaml and account field of the card, should I be using the entity, the friendly name, or whatever I called it in the secrets/config.yaml.

Screenshot 2023-12-07 200855

Screenshot 2023-12-07 200211

Anyone manged to use SpotCast to a LMS device (Squeezebox) with Spotify Connect enabled using Spotty

Documentation says it should be able to cast to Spotify Connect device but it´s not working for me.

I got the same exact question

1 Like