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

Maybe I should write a script… if chromecast stop turn it on… I must think of a condition though…

I was always wondering if one might be able to create an on the fly scene, stop Spotify, then activate the scene again. Perhaps you can find out? :wink:

I tried to make a scene or a start the service cast but it is not working. What is needed is too choose the media player from the lovelace card but I don’t know if this is possible or how to do it.
At least in my case when the spotcast stops all I have to do is to choose the chromecast player from the picture below.

103930

This used to work for me, but now I’m getting this error when calling the service:

‘ChromecastInfo’ object has no attribute ‘services’

Any idea what might be causing this? Since it wasn’t working I updated the cookies in my secrets.yaml to be current, but still received the same error.

Maybe post your code?

I think Spotify and Google Cast are having some issues btw. Having issues streaming since yesterday. Also Google Home to Spotify directly had issues, so can’t be spotcast/HA itself.

Replaced tokens twice (log mentioned expired tokens first). After a while suddenly worked (but sometimes hiccups when skipping songs). Now again issues, also from Google Home > Spotify directly.


Logger: homeassistant.core
Source: custom_components/spotcast/__init__.py:452 
First occurred: 11:01:13 (2 occurrences) 
Last logged: 11:03:05

Error executing service: <ServiceCall spotcast.start (c:48e40fd4105711eba8212755870ff66b): entity_id=media_player.multispeakers_cast, uri=spotify:playlist:37i9dQZEVXcQorVlmdOh35, random_song=True, shuffle=True, force_playback=False, repeat=true, offset=0>
Error executing service: <ServiceCall spotcast.start (c:8b36271a105711eba646ef90cbb687b4): entity_id=media_player.multispeakers_cast, random_song=True, repeat=playlist, force_playback=True, offset=0, shuffle=False>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 1351, in catch_exceptions
    await coro_or_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1374, in _execute_service
    await self._hass.async_add_executor_job(handler.func, service_call)
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/spotcast/__init__.py", line 302, in start_casting
    spotify_cast_device.startSpotifyController(access_token, expires)
  File "/config/custom_components/spotcast/__init__.py", line 452, in startSpotifyController
    sp.launch_app()
  File "/usr/local/lib/python3.8/site-packages/pychromecast/controllers/spotify.py", line 88, in launch_app
    raise LaunchError(
pychromecast.error.LaunchError: Timeout when waiting for status response from Spotify app

@fondberg do you perhaps have the same experience?

Edit: also trying to cast from Spotify app to Chromecast speakers (thirdy party and Google mini’s) have issues. Either my network is messed up, or Spotify > Google Cast is having issues. Spotify Connect works fine.

Edit 2: casting other sources like online radio streams to Google Cast speakers works fine.

Edit 3: seems a widespread issue: https://community.spotify.com/t5/Android/Spotify-and-Nest-Mini-can-t-cast-or-play-on-device/td-p/5054694

I figured I’d share all this info and findings so others know Google Cast is having issues, not spotcast.

1 Like

I just wanted to say thanks for this great component! I’ve been using Spotcast in combination with room presence detection to have my Spotify music follow me around my home. I’ve written a guide on how to get it set up if anyone else is interested.

1 Like

I love the ability to transfer playback! Am I correct in assuming that while this component supports spotify connect devices, it does not support sonos speakers as spotify connect devices?

(I’d like to be able to transfer playback from my phone to my sonos when arriving home.)

I believe spotcast uses the officiel Spotify integration to play on Spotify Connect speakers. The official integration indeed does not support Sonos speakers.

Perhaps spotcast uses it’s own method to cast to Spotify Connect, but that’s unlikely. So if you have tested this and it does not work, this is the reason.

1 Like

If I have understood correctly, spotcast does not use the official Spotify integration to start playback, but rather uses it’s own method with tokens/cookies from the browser login. That is why it’s able to start playback on devices you initially don’t see in the official Spotify integration. So in theory you can start playback with spotcast without involving the official Spotify integration at all. But you will still need it for controlling the playback afterwards.

Unfortunately you are correct, even with this method it does not support Sonos speakers. It was mentioned in the documentation on GitHub when Spotify Connect support was introduced to spotcast.

I had 2 Sonos speakers for testing but I was not able to automate them as advanced as I wanted with multiple Spotify account simultaneously. I also had an older Spotify Connect speaker which I didn’t have much luck with, even when using spotcast.

So to make it easy for myself I dumped those and now have 14 Cast devices instead (12x JBL Link Music, 1x Google Nest Hub, 1x Google Nest Mini), and now I can do everything I want with the help of this awesome component. :slight_smile:

1 Like

I meant Spotcast uses probably uses the Spotify integration for only the Spotify Connect casting. To cast to Chromecast it uses its own implementation (indeed of scraping the tokens through web player). Perhaps it uses that same implementation to cast to Spotify Connect speakers, but if that was the case you would think you could also cast to Sonos (if their also listed through web).

I think if you want automate music when using Sonos, you should see what the Sonos component has to offer with playback options. It is possible sinds HA 117 to play Spotify URI’s directly on Sonos (kinda like how Spotcast works):

  • @amelchio also added support for playing Spotify URIs with the Sonos integration.
1 Like

Yeah, you can definitely do some advanced stuff with the Sonos component, and the ability to send URIs is a great improvement. However I suspect it is still not possible to do some of the things I wanted, which includes multiple Spotify accounts. (i.e. start playback from Spotify account A to Sonos speaker 1, and then start playback from Spotify account B to Sonos speaker 2).

1 Like

Cool, thanks!

hi, i have a problem and i don’t know if it’s spotcast or spotify’s fault.
My problem is that after one hour the stream is interrupted (play song on chrmecast that are plugged into tv, all go well until, after about 1h, chromecast switched from playing to idle mode).
I did not find any error in the logs.
has anyone had the same problem as me? how can i solve?
thanks for all

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

And my quote of fondberg there as well. This isn’t anyone’s ‘fault’ per se. It’s just how Spotify works and how spotcast is written in order to use it. Short answer: there is nothing you can do about this and hopefully Spotify someday releases a decent API other people can use.

Yeah, this is a known limitation because of how this component currently works.

However I think it should be possible to work around this if you only use spotcast to “wake up” the device you want to play to, and then use the normal “media_player.play_media” service to play media to the device which should be available as a source after it’s woken up by spotcast.

Have anyone tested this? If not I will report back when I have gotten the time to test it.

Won’t work. When you started a stream with spotcast, it will interrupt after 60 minutes of playback, regardless of stopping and starting again through spotcast or a different device. The token will expire after 60 minutes, even if you play it on a different device and it will stop anyway. That is what I experienced at least always.

I haven’t used spotcast for the last month now, I stopped with Chromecast alltogether so I don’t know if stuff has changed on Spotify’s end last month.

I create an automation that after 50 Min restart spotcast (before I check if Chromecast is on on Spotify).
It’s workaround but for now it’s ok
UPDATE
if i put 50 min timer it doesent work (i think that when i call for the second time spotcast it see that there are a valid token and it reuse that)
i think it should work if the timer is set to exactly 60 minutes with the eventual cut of the song you are listening at the 60th minute

Hi, can you provide the source for that automation please?

Where do i place that? in the configuration.yaml or sensor.yaml

and where do i have to place this?

devices_json: [{"name": "Kök", "cast_type": "audio", "model_name": "Google Home", "uuid": "xxxxx", "manufacturer": "Google Inc."}, {"name": "Högtalare uppe", "cast_type": "group", "model_name": "Google Cast Group", "uuid": "xxxx", "manufacturer": "Google Inc."}, {"name": "Vardagsrum", "cast_type": "cast", "model_name": "HK Citation 300", "uuid": "xxxx", "manufacturer": "Harman Kardon"}]

last_update: 2019-05-01T15:27:49.828553+02:00

friendly_name: Chromecast Devices

The sensors.yaml is a file included by your configuration through the line

sensor: !include sensors.yaml

That means all your sensors go there without the need of the “sensor:” part.
So in your sensors.yaml simply write

- platform: spotcast
devices_json: [{"name": "Kök", "cast_type": "audio", "model_name": "Google Home", "uuid": "xxxxx", "manufacturer": "Google Inc."}, {"name": "Högtalare uppe", "cast_type": "group", "model_name": "Google Cast Group", "uuid": "xxxx", "manufacturer": "Google Inc."}, {"name": "Vardagsrum", "cast_type": "cast", "model_name": "HK Citation 300", "uuid": "xxxx", "manufacturer": "Harman Kardon"}]

last_update: 2019-05-01T15:27:49.828553+02:00

friendly_name: Chromecast Devices

These are just the attributes of the entity sensor.chromecast_devices which gets created through the above sensor definition.

EDIT: I don’t write in forums that often, can someone telle me why the code appears red?