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

spotcast - custom component to start playback on an idle chromecast device as well as adds a sensor to list the chromecast devices currently on the network, this exists kind of in the chromecast media player but it is hard to programmatically use that functionality.

This component is used by https://github.com/custom-cards/spotify-card, see Spotify Lovelace Card

34 Likes

Hi,
just to confirm, this help resolve the issue with the official component forgetting the devices?

Your component looks like the missing link for us to start streaming Spotify on a Chromecast device. Nice!

I’m getting these log listings:

Setup of platform spotcast is taking over 10 seconds.
ModuleNotFoundError: No module named ‘spotify_token’
Update of sensor.chromecast_devices is taking over 10 seconds

What I did:

  • Downloaded the files (as a ZIP from your Github) then copied them to /config/custom_components/spotcast/
  • Added my Spotify username and password to configuration.yaml
  • Restarted Home Assistant (is Hassio btw)

Can you help me on my way?

Edit: just restarted HA again and the ModuleNotFound error is gone. Now for the other warnings?

Seems your network is slow for some reason. The 10 sec warning can be ignored though

Which official component?
Plus this components main use case is to start spotify on a chromecast device…

You are right. Public Enemy is blasting from my Google Home Mini now :+1:

1 Like

I think @phairplay means that this script should work (but doesn’t when the Chromecast device is off):

play_song:
  alias: 'Play a Spotify track on Chromecast'
  sequence:
    - service: media_player.select_source
      data:
        entity_id: media_player.spotify
        source: 'Living Room'
    - service: media_player.play_media 
      data:
        entity_id: media_player.spotify
        media_content_type: music
        media_content_id: spotify:track:6xFXusME7FMMyMkPrIv2WJ

But with your component this does the trick even with devices that are ‘off’:

- service: spotcast.start
  data:
    device_name: 'Living Room'
    uri: 'spotify:track:6xFXusME7FMMyMkPrIv2WJ'

A question though: when playing a song using spotcast.start, I notice the entity ‘media_player.spotify’ changes state to ‘playing’, but the entity ‘media_player.chromecast_living_room’ remains ‘off’. Any idea why this happens? The automation to turn on my amp depends on that :slight_smile:

1 Like

Some people their spotfiy component retains the devices its can play from.i .e Google home or Google mines.

Yet mine like some others here it forgets them once the devices are off.

Before I moved home and on another wifi network I could have spotify play music directly

In short the answer is that the only way to start spotify on a chromecast device is through this component nowadays. I’m on my phone so I will not detail it but it has to do with the token for the spotify media component is not powerful enough to start the app on chromecast.

sorry to be dumb, but could you please share a working script example?

i have your component working and it sees all my cast devices

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

I tried this

play_song:
  alias: 'Play a Spotify track on Chromecast'
  sequence:
   - service: spotcast.start
     data:
     device_name: 'Master Bedroom Speaker'
     uri: 'spotify:track:6xFXusME7FMMyMkPrIv2WJ'

yet checking config kicks up an error

You need to check the log for what error it is. I suspect that it is one of the following:

  1. You have not installed spotcast correctly. Check in devtools-> dev-service if spotcast.start is listed as a service.
  2. ‘Master Bedroom Speaker’ is not an exact match for the chromecast device. Check the sensor.chromecast_devices for what devices are available
  3. Your spotify username and or password is incorrect. These should be the same as when you login on https://open.spotify.com
  4. You don’t have a premium subscription
1 Like

1, Yes spotcast.start does appear in the dev tool list
2, ‘Master Bedroom Speaker’ was lifted straight from the devices_json:
3, it is correct because the sensor it populated with the list of devices
4, yes I do have a premium account

Capture

using the above I posted as a script I get the following error

Invalid config for [script]: expected dict for dictionary value @ data['script']['play_song']['sequence'][0]['data']. Got None
extra keys not allowed @ data['script']['play_song']['sequence'][0]['device_name']. Got 'Master Bedroom Speaker'
extra keys not allowed @ data['script']['play_song']['sequence'][0]['uri']. Got 'spotify:track:6xFXusME7FMMyMkPrIv2WJ'. (See /config/configuration.yaml, line 176). Please check the docs at https://home-assistant.io/components/script/

You need two extra spaces in front of the last 2 lines of your script. It is an indentation problem.

1 Like

Amazing! Haven’t been able to try it yet but it seems my two days foray of setting up and MPD instance and Icecast server simply to start playlists in spotify (which I have used a total of 2 times) was completely in vain! :smiley:

Great! On a side note I am checking with core devs who owns media_player.py because I think these should merge. There is one problem in that the official spotify component in HA used normal clientId and client-secret from spotify dev site which is not powerful enough to initiate playback on chromecast devices (hence this small component).

If I am to take over maintenance of that I would be greatful if someone else would join me

3 Likes

you’re kidding me!
I fell foul of the blooming indentation ahhhh!
many thanks

@fondberg thank you for trying to help with my clear stupidity

it does work yet as already mentioned HA doesn’t detect that the google device is playing anything

I also got some, probably silly error:

2019-05-02 16:35:00 ERROR (MainThread) [homeassistant.core] Error executing service <ServiceCall script.spotify2 (c:c1e75e9bf17d45d398378972b6daa38f)>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1147, in _safe_execute
    await self._execute_service(handler, service_call)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1160, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/script/__init__.py", line 116, in service_handler
    context=service.context)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/script/__init__.py", line 174, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/local/lib/python3.7/site-packages/homeassistant/helpers/service.py", line 88, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1138, in async_call
    self._execute_service(handler, service_call))
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1162, in _execute_service
    await self._hass.async_add_executor_job(handler.func, service_call)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/spotcast/__init__.py", line 73, in start_casting
    access_token, expires = get_spotify_token(username=username, password=password)
  File "/config/custom_components/spotcast/__init__.py", line 43, in get_spotify_token
    import spotify_token as st
ModuleNotFoundError: No module named 'spotify_token'

1, spotcast.start does appear in the dev tool list
2, ‘Wohnzimmer’ is a name used in chromecast.sensor:
3, the sensor it populated with the list of devices (does this mean my spotify credentials are correct?
4, yes I do have a premium account

My configuration.yaml:

spotcast:
  username: [email protected]
  password: hidden

My script:
image
(Please don’t ask me, why sometimes I cannot post here with proper intendation, hence the screenshot)

Sorry for these beginner questions! Thank you for providing this great component!

you need one more indent from - service down

spotify:
  alias: 'Spotify'
  sequence:
   - service: spotcast.start
     data:
       device_name: 'Wohnzimmer'
       uri: 'spotify:playlist:37i9dQZF1DX3yvAYDslnv8'