Skip to content

Spotify Binding ​

This binding implements a bridge to the Spotify Web API and makes it possible to discover Spotify Connect devices available on your Spotify Premium account.

Configuring the binding ​

The binding requires you to register an application with the Spotify Web API at https://developer.spotify.com. This will provide a Client ID and Client Secret to use in your binding configuration.

Create a Spotify application ​

Follow the instructions in the tutorial at https://developer.spotify.com/web-api/tutorial/. Follow the instructions under:

  1. Setting Up Your Account
  2. Registering Your Application
  • Step 6: entering website information can be skipped.
  • Step 7: setting Redirect URIs is very important

When registering your new Spotify application for the openHAB Spotify bridge, you must specify the allowed "Redirect URIs" (allowlist). Here you have to specify the URL to the bridge authorization servlet on your server.

Spotify only accepts http:// redirect URIs when the host is a loopback IP literal (127.0.0.1 or [::1]) — not the localhost hostname. For every other host, including your public domain or LAN hostname, the redirect URI must use https://.

If openHAB itself does not terminate https (e.g. it sits behind a reverse proxy that does), see Binding Configuration below for the forceHttps setting, which may be required for the binding to generate a matching https:// redirect URI.

This is important since the authorization process with Spotify uses your web browser, and Spotify must know the correct URL to your openHAB server for authorization to complete. After authorizing with Spotify, this redirect URI is where authorization tokens for your openHAB Spotify bridge will be sent, and they have to be received by the servlet on /connectspotify. The /connectspotify page itself always shows you the exact redirect URI the binding will send to Spotify — make sure it matches what you registered, including the scheme.

Binding Configuration ​

The binding has the following configuration options, available under the Spotify Binding add-on settings:

ParameterTypeDescription
forceHttpsBooleanAlways use https for the Spotify redirect URI, regardless of the scheme openHAB detects for the incoming request. Enable this when openHAB is reachable through a reverse proxy that terminates https but does not identify itself through a recognized forwarded-proto header (X-Forwarded-Proto, X-Forwarded-Ssl, Front-End-Https, or Forwarded), causing the redirect URI shown on /connectspotify to incorrectly use http. Defaults to false.

Configure the binding ​

  1. Install the binding and make sure the Spotify Binding is listed on your server.
  2. Complete the Spotify Application Registration if you have not already done so, see above.
  3. Make sure you have your Spotify application Client ID and Client Secret available.
  4. Add a new "Spotify Player Bridge" Thing. Choose a new ID for the player (unless you prefer the generated one), and enter the Client ID and Client Secret from the Spotify application registration in the corresponding fields of the bridge configuration. You can leave refreshPeriod as is. Save the bridge.
  5. The bridge Thing will stay in state INITIALIZING and eventually go OFFLINE — this is fine. You have to authorize this bridge with Spotify.
  6. Go to the authorization page of your server: https://<your openHAB address>/connectspotify (or http://127.0.0.1:8080/connectspotify for local/development access). Your newly added bridge should be listed there.
  7. Press the "Authorize Player" button. This will take you either to the Spotify login page or directly to the authorization screen. Log in and/or authorize the application. If the redirect URIs are correct, you will be returned and the entry should show you are authorized with your Spotify username/ID. If not, go back to your Spotify application and ensure you have the correct redirect URIs.
  8. The binding will be updated with a refresh token and go ONLINE. The refresh token is used to re-authorize the bridge with the Spotify Web API whenever required.

Now that your bridge is ONLINE, you can start a scan with the binding to auto-discover your devices.

If no devices show up, start the Spotify app on your PC/Mac/iOS/Android and start playing on your devices while you run discovery. This should make any Spotify Connect devices and Spotify apps discoverable. You may have to trigger the openHAB discovery several times, as the bridge will only find active devices known by the Spotify Web API at the time discovery is triggered.

If the bridge configuration breaks for any reason, the authorization procedure can be reinitiated from step 6 whenever required. You can force reinitialization by authorizing on the /connectspotify page, even if the page shows it as authorized. This will reset the refresh token.

The following configuration options are available on the Spotify player bridge:

ParameterDescription
clientIdThe Client ID provided by Spotify when you add a new application for openHAB to your Spotify account. Go to https://developer.spotify.com/ (required)
clientSecretThe Client Secret provided by Spotify when you add a new application for openHAB to your Spotify account. (required)
refreshPeriodThis is the frequency of the polling requests to the Spotify Connect Web API in seconds.

The following configuration option is available on the Spotify device:

ParameterDescription
deviceNameThis is the device name provided by Spotify (required).

Supported Things ​

All Spotify Connect–capable devices should be discoverable through this binding. If you can control them from the Spotify app on your PC/Mac/iPhone/Android/etc., you should be able to add it as a Thing. Some devices can be restricted and not available for playing. The bridge will list these in discovery, but they will never be ONLINE. A Spotify web player in a browser is only available as long as the page is open. It will get a unique id for that session. If you close the page it will be gone. Opening a new web player will result in a new id. Some devices will not be visible (e.g., Chromecast) when they are not active (they go into sleep mode and are not visible through the Spotify Web API). The binding will show them as GONE.

Discovery ​

As long as Spotify Connect devices are available in the context of the user account configured with the bridge(s), they should show up whenever you initiate discovery of things.

If no devices are showing up, try connecting to the device(s) from your smartphone or computer to make sure the device(s) are in use by your user account.

Discovery of devices in the Spotify Web API is based on what is known by Spotify. There is a difference between, e.g., smartphones and computers, which can discover devices on the local network, and the Web API, which cannot. It only knows about a device if your account is currently associated with it.

Channels ​

Bridge / Player ​

The channels on the bridge are used to both control the active device and get details of currently playing music on the Spotify account associated with the bridge.

Common Channels:

Channel Type IDItem TypeRead/WriteDescription
deviceNameStringRead-writeName of the currently active Connect device.
devicesSelectionRead-writeList of currently active Connect devices. Set the device ID to transfer playback to that device.
deviceVolumeDimmerRead-writeGet or set the active Connect Device volume.
deviceShuffleSwitchRead-writeTurn on/off shuffle play on the active device.
trackPlayStringRead-writeSet which music to play on the active device. This channel accepts Spotify URIs and URLs.
trackPlayerPlayerRead-writeThe Player Control of the active device. Accepts PLAY/PAUSE/NEXT/PREVIOUS commands.
trackRepeatStringRead-onlytrack repeats the current track. context repeats the current context. off turns repeat off.
trackNameStringRead-onlyThe name of the currently playing track.
trackDurationStringRead-onlyThe duration (m:ss) of the currently playing track. This is updated every second.
trackDurationMsNumber:TimeRead-onlyThe duration of the currently playing track.
trackProgressStringRead-onlyThe progress (m:ss) of the currently playing track. This is updated every second.
trackProgressMsNumber:TimeRead-onlyThe progress of the currently playing track.
playlistsSelectionRead-writeThis channel will be populated with the user's playlists. Set the playlist ID to start.
playlistNameStringRead-writeThe currently playing playlist, or empty if no playlist is playing.
albumNameStringRead-onlyAlbum Name of the currently playing track.
albumImageRawTypeRead-onlyAlbum image of the currently playing track.
albumImageUrlStringRead-onlyURL to the album image of the currently playing track.
artistNameStringRead-onlyArtist name of the currently playing track.

The playlists channel has 2 parameters:

ParameterDescription
offsetThe index of the first playlist to return. Default 0, max 100000
limitThe maximum number of playlists to return. Default 20, min 1, max 50

The albumImage and albumImageUrl channels have 1 parameter:

ParameterDescription
imageIndexIndex in the list to select the image size to show. 0 = large (default), 1 = medium, 2 = small

Note: The devices and playlists channels are Selection channels. They are dynamically populated by the binding with user-specific devices and playlists.

Advanced Channels:

Channel Type IDItem TypeRead/WriteDescription
accessTokenStringRead-onlyThe current access token used in communication with the Web API.
deviceIdStringRead-writeThe Spotify Connect device ID.
trackIdStringRead-onlyTrack ID of the currently playing track.
trackHrefStringRead-onlyTrack URL of the currently playing track.
trackUriStringRead-onlyTrack URI of the currently playing track.
trackTypeStringRead-onlyType of the currently playing track.
trackNumberStringRead-onlyNumber of the track on the album/record.
trackDiscNumberStringRead-onlyDisc Number of the track on the album/record.
trackPopularityNumberRead-onlyCurrently playing track popularity.
trackExplicitSwitchRead-onlyWhether or not the track has explicit lyrics.
albumIdStringRead-onlyAlbum ID of the currently playing track.
albumUriStringRead-onlyAlbum URI of the currently playing track.
albumHrefStringRead-onlyAlbum URL of the currently playing track.
albumTypeStringRead-onlyAlbum Type of the currently playing track.
artistIdStringRead-onlyArtist Id of the currently playing track.
artistUriStringRead-onlyArtist URI of the currently playing track.
artistHrefStringRead-onlyArtist URL of the currently playing track.
artistTypeStringRead-onlyArtist Type of the currently playing track.

Devices ​

There are channels on the devices that seemingly overlap those of the bridge. The difference between these overlapping channels is that the device channels always act in the context of the particular device. For example, if you assign a playlist to the trackPlay channel of the device, playback of that playlist will be activated on that particular device. Assigning a playlist to the trackPlay channel of the bridge will start playing the list on whichever device is active.

Common Channels:

Channel Type IDItem TypeRead/WriteDescription
trackPlayStringWrite-onlyUpdate to play a track, playlist, artist. Activates the device.
deviceNameStringRead-onlyName of the device.
deviceVolumeDimmerRead-writeVolume setting for the device.
devicePlayerPlayerRead-writePlayer control of the device.
deviceShuffleSwitchRead-writeTurn on/off shuffle play.

Advanced Channels:

Channel Type IDItem TypeRead/WriteDescription
deviceIdStringRead-writeThe Spotify Connect device ID.
deviceTypeStringRead-onlyThe type of device e.g. Speaker, Smartphone.
deviceActiveSwitchRead-onlyIndicates if the device is active or not. Should be the same as Thing status ONLINE/OFFLINE.
deviceRestrictedSwitchRead-onlyIndicates if this device allows to be controlled by the API or not. If restricted it cannot be controlled.

Actions ​

The bridge supports an action to play a track or other context URI. The following actions are supported:

java
play(String context_uri)
play(String context_uri, int offset, int position_ms)
play(String context_uri, String device_id)
play(String context_uri, String device_id, int offset, int position_ms)

Full Example ​

In this example there is a bridge configured with Thing ID user1, illustrating that the bridge is authorized to play in the context of the Spotify user account user1.

spotify.things:

dsl
Bridge spotify:player:user1 "Me" [clientId="<your client id>", clientSecret="<your client secret>"] {
  Things:
    device device1 "Device 1" [deviceName="<spotify device name>"]
    device device2 "Device 2" [deviceName="<spotify device name>"]
  Channels:
    String : playlists     [limit=50]
    String : albumImageUrl [imageIndex=1]
}

spotify.items:

dsl
Player spotifyTrackPlayer    "Player"               {channel="spotify:player:user1:trackPlayer"}
String spotifyDevices        "Active device [%s]"   {channel="spotify:player:user1:devices"}
Switch spotifyDeviceShuffle  "Shuffle mode"         {channel="spotify:player:user1:deviceShuffle"}
String spotifyTrackRepeat    "Repeat mode: [%s]"    {channel="spotify:player:user1:trackRepeat"}
String spotifyTrackProgress  "Track progress: [%s]" {channel="spotify:player:user1:trackProgress"}
String spotifyTrackDuration  "Track duration: [%s]" {channel="spotify:player:user1:trackDuration"}
String spotifyTrackName      "Track Name: [%s]"     {channel="spotify:player:user1:trackName"}
String spotifyAlbumName      "Album Name: [%s]"     {channel="spotify:player:user1:albumName"}
String spotifyArtistName     "Artist Name: [%s]"    {channel="spotify:player:user1:artistName"}
String  spotifyAlbumImageUrl "Album Art"            {channel="spotify:player:user1:albumImageUrl"}
String spotifyPlaylists      "Playlists [%s]"       {channel="spotify:player:user1:playlists"}
String spotifyPlayName       "Playlist [%s]"        {channel="spotify:player:user1:playlistName"}

String device1DeviceName    {channel="spotify:device:user1:device1:deviceName"}
Player device1Player        {channel="spotify:device:user1:device1:devicePlayer"}
Dimmer device1DeviceVolume  {channel="spotify:device:user1:device1:deviceVolume"}
Switch device1DeviceShuffle {channel="spotify:device:user1:device1:deviceShuffle"}

String device2DeviceName    {channel="spotify:device:user1:device2:deviceName"}
Player device2Player        {channel="spotify:device:user1:device2:devicePlayer"}
Dimmer device2DeviceVolume  {channel="spotify:device:user1:device2:deviceVolume"}
Switch device2DeviceShuffle {channel="spotify:device:user1:device2:deviceShuffle"}

spotify.sitemap:

dsl
sitemap spotify label="Spotify Sitemap" {

  Frame label="Spotify Player Info" {
    Selection item=spotifyDevices       label="Active device [%s]"
    Default   item=spotifyTrackPlayer   label="Player"
    Switch    item=spotifyDeviceShuffle label="Shuffle mode:"
    Text      item=spotifyTrackRepeat   label="Repeat mode: [%s]"
    Text      item=spotifyTrackProgress label="Track progress: [%s]"
    Text      item=spotifyTrackDuration label="Track duration: [%s]"
    Text      item=spotifyTrackName     label="Track Name: [%s]"
    Image     item=spotifyAlbumImageUrl label="Album Art"
    Text      item=spotifyAlbumName     label="Currently Played Album Name: [%s]"
    Text      item=spotifyArtistName    label="Currently Played Artist Name: [%s]"
    Selection item=spotifyPlaylists     label="Playlist" icon="music"
  }

  Frame label="My Spotify Device 1" {
    Text    item=device1DeviceName label="Device Name [%s]"
    Default item=device1Player
    Slider  item=device1DeviceVolume
    Switch  item=device1DeviceShuffle
  }

   Frame label="My Spotify Device 2" {
    Text    item=device2DeviceName label="Device Name [%s]"
    Default item=device2Player
    Slider  item=device2DeviceVolume
    Switch  item=device2DeviceShuffle
  }
}

spotify.rules

rules
val spotifyActions = getActions("spotify", "spotify:player:user1")
// play the song
spotifyActions.play("spotify:track:4cOdK2wGLETKBW3PvgPWqT")

Binding model and Spotify Web API ​

The model of the binding is such that the bridge acts as a player in the context of a specific user. All devices currently associated with the user account are available to control.

You can add multiple bridges to allow playing in the context of multiple Spotify user accounts. Therefore a device can exist multiple times — once for every bridge configured. This is seen in the Thing ID which includes the name of the bridge it is bound to.

The Web API and its documentation do not imply a certain model, and it can be argued whether the model chosen matches it or not. The current model is different in the sense that a Spotify application only controls the active device, and you then transfer playback to another available device. In this binding, the model allows you to control any device discovered at any time, if it is available. As soon as you press play, next, previous, or assign a playlist to the device, it will be activated.

At the time of writing, the Spotify Web API does not allow you to take over playback of a Spotify Connect device. This is different from what you see in the smartphone app or the desktop application. There, you are able to actively take over playback of a device even if someone else is playing on it.