I was looking for a way to easily add the playback of Spotify into my automations. I struggled a little with media_player.play_media on various devices, it was just not very reliable for me. Fortunately, I stumbled upon an integration named SpotCast. SpotCast seems to have originally been intended for playback on ChromeCast devices but it seems to work quite fine for playback on any device I have that can link with Spotify.
This guide will show you how to enable and use SpotCast within your Home Assistant instance.
A couple of pre-requisites before we start:
- You have the Spotify integration already installed. See our guide.
- You have already enabled HACS. See our guide.
Now lets get going.
- Install the SpotCast Integration
Open the Add-ons menu item from HACS
Find the SpotCast Integration
Download the Integration
The SpotCast Integration is now installed, but that’s the easy bit. Next comes the configuration.
2. Get the sp_dc and sp_key details from Spotify.
Open a new incognito window in chrome to https://open.spotify.com/ – Log in with your Spotify account details.
Press F12 to open the developer tools option.
Find the application setting and under storage select cookies for the open.spotify.com URL. Take a note of the sp_key and sp_dc entries. Perhaps paste them into notepad.
3. Add the keys within file editor
Open File Editor within Home Assistant
Add the following entry to config and click save
spotcast: sp_dc: !secret sp_dc sp_key: !secret sp_key
Now Open secrets.yaml and add the below code – replacing the sp_key and sp_dc with those you took note of earlier.
Remember to click save once completed
sp_dc: my_long_sp_dc_is_pasted_instead_of_this sp_key: my_sp_key_is_pasted_instead _of_this
Restart Home Assistant to load the new yaml entries.
4. Add to an Automation
For this you will need to go back to https://open.spotify.com and open the playlist you would like to automate to start playing.
Click the 3 dots next to the playlist name and select share > copy link to playlist. paste the link into notepad so you don’t lose it.
Create your automation. Within the actions add ‘Call Service’ and switch to yaml. Add the below code
service: spotcast.start data: device_name: mydevicename uri: spotify:playlist:2dzijXtMQRjE8EWRG8QoXD?si=49791c0d59aa447e random_song: true shuffle: true limit: 20 force_playback: true repeat: "off" offset: 0 ignore_fully_played: false
device_name would be the name of your spotify device. I have found all chromecast and spotify enabled speakers or media devices work for this.
The uri is the playlist you copied the link to share. Notice that you only need the end section of the shared URL. in this example the shared URL copied was https://open.spotify.com/playlist/79mqkTUbukLwNOuRdTJiru?si=3b258737fe5d464c I have made bold the section you need.
I have set random_song to true. if you set false the playlist will play in order. I have also enabled shuffle, you may have other requirements.
Switch back from yaml to visual editor and you should see something similar to the below. You might find this view easier to continue to tweak settings.
That’s the end of the guide. Hopefully you find this useful.