Home

Awesome

Spotcast

hacs_badge spotcast Maintenance

Home Assistant custom component to start Spotify playback on an idle chromecast device or a Spotify Connect device (thanks to @kleinc80) which means that you can target your automation for chromecast as well as connect devices.

Spotcast implements a cast platform (requires Home Assistant Core 2022.2.0 or later), which enables Google Cast media player entities to play Spotify URI as well as to browse the Spotify library.

This component is not meant to be a full Spotify chromecast media_player but only serves to start the playback. Controlling the chromecast device and the Spotify playback after the initial start is done in their respective components. Because starting playback using the API requires more powerful token the username and password used for browser login is used.

Used by Spotify-Card.

Community post

Installation

HACS

This component is easiest installed using HACS.

Manual installation

Copy all files from custom_components/spotcast/ to custom_components/spotcast/ inside your config Home Assistant directory.

Configuration

Minimum Home Assistant version

Spotcast is compatible with any version since 2022.2.0.

[!WARNING] An iminent change to Home Assistant will require v3.9.2 when installing Home Assistant 2024.11 or newer. The current target version will be updated once 2024.11 become generally available.

Official Spotify Integration

Note that as of v3.5.2 you must also have the official Home Assistant Spotify Integration installed and configured for this custom component to work. This is because it provides the correct device list which has the correct scopes in its token.

Obtaining sp_dc and sp_key cookies

Spotcast uses two cookies to authenticate against Spotify in order to have access to the required services.

To obtain the cookies, these different methods can be used:

Chrome based browser

Chrome web console
  1. Open a new Incognito window at https://open.spotify.com and login to Spotify.
  2. Press Command+Option+I (Mac) or Control+Shift+I or F12. This should open the developer tools menu of your browser.
  3. Go into the application section.
  4. In the menu on the left go int Storage/Cookies/open.spotify.com.
  5. Find the sp_dc and sp_key and copy the values.
  6. Close the window without logging out (Otherwise the cookies are made invalid).

cookie in chrome developer tools

Firefox based browser

Firefox web console
  1. Open a new Incognito window at https://open.spotify.com and login to Spotify.
  2. Press Command+Option+I (Mac) or Control+Shift+I or F12. This should open the developer tools menu of your browser.
  3. Go into the Storage section. (You might have to click on the right arrows to reveal the section).
  4. Select the Cookies sub-menu and then https://open.spotify.com.
  5. Find the sp_dc and sp_key and copy the values.
  6. Close the window without logging out (Otherwise the cookies are made invalid).

Firefox developer tool

Single account

Add the following to your configuration.yaml:

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

Multiple accounts

Add accounts dict to the configuration and populate with a list of accounts to be able to initiate playback using diffferent accounts than the default.

If you are using v3.5.2 or greater and thus also have the core Spotify Integration installed, then the additional accounts will also need to be added there as well for multiple accounts to work.

spotcast:
  sp_dc: !secret primary_sp_dc
  sp_key: !secret primary_sp_key
  country: SE #optional, added in 3.6.24
  accounts:
    niklas:
      sp_dc: !secret niklas_sp_dc
      sp_key: !secret niklas_sp_key
    ming:
      sp_dc: !secret ming_sp_dc
      sp_key: !secret ming_sp_key

Edit secrets.yaml

Please note: configuration.yaml is a plain text file and it is not recommended to store your passwords in this file.

By using the !secret tag you are directing Home Assistant to look inside the secrets.yaml file for your key instead.

For a single account add the following to your secrets.yaml file:

sp_dc: [your sp_dc here]
sp_key: [your sp_key here]

For multiple accounts add the corresponding entries to your secrets.yaml file. For example, using the same accounts as above:

primary_sp_dc: [your sp_dc here]
primary_sp_key: [your sp_key here]
niklas_sp_dc: [niklas sp_dc here]
niklas_sp_key: [niklas sp_key here]
ming_sp_dc: [ming sp_dc here]
ming_sp_key: [ming sp_key here]

Call the service

The spotcast custom component creates a service called 'spotcast.start' in Home Assistant.

Start playback on Spotify connect device

- service: spotcast.start
  data:
    spotify_device_id: "ab123c5d7347324c2b1234567890f8d6dc40350"
    uri: "spotify:playlist:5xddIVAtLrZKtt4YGLM1SQ"
    random_song: true

Start playback on a device with default account

- service: spotcast.start
  data:
    device_name: "Kitchen"
    uri: "spotify:playlist:5xddIVAtLrZKtt4YGLM1SQ"
    random_song: true

where:

Optionally you can specify the entity_id of an existing Home Assistant chromecast media-player like:

- service: spotcast.start
  data:
    entity_id: "media_player.vardagsrum"
    uri: "spotify:playlist:5xddIVAtLrZKtt4YGLM1SQ"

Find Spotify Device ID

To use the Spotcast service with a Spotify Connect device, you need the spotify_device_id. To find the spotify_device_id, multiple option are available.

With Spotify developer portal

  1. Go to Spotify developer console
  2. Click TRY IT <br/>
  3. Extract the ID (there could be more than one if you have more than one speaker) from the 'RESPONSE SAMPLE' on the right

Through Spotcast log

  1. Enable the debug logs (instructions are in section Enabling Debug Log in this README)
  2. Reboot Home Assistant
  3. Go to Configuration >> Logs >> Load Full Home Assistant Log.
  4. For chromecast devices, make sure to play media on the device prior to checking the logs as they will not show unless active
  5. Find the log entry get_spotify_devices and look for the device ID.

Sonos Device ID

  1. Open the Spotify Web Player and sign in if needed
  2. Make sure you see your Sonos devices in the Connect popup
  3. Open your browser's DevTools (F12 for Chrome)
  4. Navigate to the Network tab
  5. Connect to the desired Sonos device in the Web Player
  6. Find the associated request in your DevTools
  7. The request URL looks something like this: https://gew1-spclient.spotify.com/connect-state/v1/connect/transfer/from/my_web_player_device_id/to/my_sonos_device_id
  8. The my_sonos_device_id is the spotify_device_id you are looking for.
Log example
2022-01-13 19:10:35 DEBUG (SyncWorker_0) [custom_components.spotcast.helpers] get_spotify_devices: media_player.spotify_felix: Spotify Félix Cusson: [{'id': '################################', 'is_active': True, 'is_private_session': False, 'is_restricted': False, 'name': 'Salon', 'type': 'CastAudio', 'volume_percent': 16}]
2022-01-13 19:10:35 DEBUG (SyncWorker_0) [custom_components.spotcast.helpers] get_spotify_devices: {'devices': [{'id': '###############################', 'is_active': False, 'is_private_session': False, 'is_restricted': False, 'name': 'Salon', 'type': 'CastAudio', 'volume_percent': 16}]}

# Look for
<...> Spotify Félix Cusson: [{'id': '################################', <- This is the device ID

Automation example

- id: 'christmas_play_christmas_music'
  alias: Christmas play Christmas music
  initial_state: 'on'
  trigger:
  - event_data:
      id: remote_windowlamps
      event: 5002
    platform: event
    event_type: deconz_event
  condition: []
  action:
  - service: spotcast.start
    data:
      uri: 'spotify:playlist:56Bor5fbMJlJV7oryb2p3k'
      random_song: true
      shuffle: true
      start_volume: 50
      entity_id: media_player.kitchen
- service: spotcast.start
  data:
    search: "Brown Bird" # resolves to spotify:artist:5zzbSFZMVpvxSlWAkqqtHP at the time of writing
    random_song: true
    shuffle: true
    start_volume: 50
    entity_id: media_player.kitchen

Transfer current playback for the account

Omitting uri will transfer the playback to the specified device.

- service: spotcast.start
  data:
    device_name: "Speaker kitchen"

Use the parameter force_playback to continue the user's playback even if nothing is currently playing.

- service: spotcast.start
  data:
    device_name: "Speaker kitchen"
    force_playback: true

where:

Start playback on a device with non default account

- service: spotcast.start
  data:
    account: "niklas"
    device_name: "Speaker kitchen"
    uri: "spotify:playlist:5xddIVAtLrZKtt4YGLM1SQ"

where:

Start podcast playback

Play the latest episode of a given podcast show.

- service: spotcast.start
  data:
    account: "niklas"
    device_name: "Speaker kitchen"
    uri: "spotify:show:6PeAI9SHRZhghU7NRPXvT3"
    ignore_fully_played: true

where

Use the sensor

The sensor has the discovered chromecasts as both json and an array of objects. Since v3.4.0 it does not do its own discovery but relies on data from core cast. Add the following to the sensor section of the configuration:

sensor:
  - platform: spotcast
    country: SE

The country tag was added in v3.6.24. This tag is optional. If ommited or if you haven't updated the configuration since the update, it will default to "SE" (which it always did before)

Sensor name:

sensor.chromecast_devices

Attributes

devices_json: [
  {
    "name": "Speaker kitchen",
    "cast_type": "audio",
    "model_name": "Google Home",
    "uuid": "xxxxx",
    "manufacturer": "Google Inc."
  },
  {
    "name": "Speakers upstairs",
    "cast_type": "group",
    "model_name": "Google Cast Group",
    "uuid": "xxxx",
    "manufacturer": "Google Inc."
  },
  {
    "name": "Living room",
    "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

Websocket API

The components websocket api.

Method: spotcast/playlist supporting different playlist_types.

Example usage:

// Retrieve playlists
const res = await this.props.hass.callWS({
  type: 'spotcast/playlists',
  playlist_type: 'featured', // 'user' for saved playlists, 'featured' for spotify featured, or personalized view id
  country_code: 'SV', // Optional country code used by featured playlists
  limit: 20, // Optional limit, default is 10
  account: 'ming' // optional account name
});

// Retrieve devices
const res = await this.props.hass.callWS({
  type: 'spotcast/devices',
  account: 'ming' // optional account name
});

// Retrieve player
const res = await this.props.hass.callWS({
  type: 'spotcast/player',
  account: 'ming' // optional account name
});

Enabling debug log

In configuration.yaml for you HA add and attach those the relevant logs. Be sure to disable it later as it is quite noisy.

logger:
  default: info
  logs:
    custom_components.spotcast: debug

Contribute

Please do! Open a Pull Request with your improvements.

This project was made possible by the original creator Niklas Fondberg. All your great work are greatly appreciated.

License

Apache 2.0