This binding implements a bridge to the Spotify Player 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 Spotify Web API at [https://developer.spotify.com](https://developer.spotify.com) - this will get you a set of Client ID and Client Secret parameters to be used by your binding configuration.
### Create Spotify Application
Follow the instructions in the tutorial at [https://developer.spotify.com/web-api/tutorial/](https://developer.spotify.com/web-api/tutorial/).
Follow instructions under:
1. Setting Up Your Account
1. 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 openHAB Spotify Bridge you have to specify the allowed "Redirect URIs" aka white-listed addresses.
Here you have to specify the URL to the Bridge Authorization Servlet on your server.
For example if you run your openHAB server on `http://openhab:8080` you should add [http://openhab:8080/connectspotify](http://openhab:8080/connectspotify) as the redirect URIs.
This is important since the authorize process with Spotify takes place using your client web browser and Spotify will have to know the right URLs to your openHAB server for the authorization to be completed.
When you have authorized 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`.
1. Add a new **"Spotify Player Bridge"** thing. Choose new Id for the player, unless you like the generated one, put in the _Client ID_ and _Client Secret_ from the Spotify Application registration in their respective fields of the bridge configuration. You can leave the _refreshPeriod_ as is. Save the bridge.
1. The bridge thing will stay in state _INITIALIZING_ and eventually go _OFFLINE_ - this is fine. You have to authorize this bridge with Spotify.
1. Go to the authorization page of your server. `http://<your openHAB address>:8080/connectspotify`. Your newly added bridge should be listed there.
1. Press the _"Authorize Player"_ button. This will take you either to the login page of Spotify or directly to the authorization screen. Login and/or authorize the application. If the Redirect URIs are correct you will be returned and the entry should show you are authorized with you Spotify user name/id. If not, go back to your Spotify Application and ensure you have the right Redirect URIs.
1. The binding will be updated with a refresh token and go _ONLINE_. The refresh token is used to re-authorize the bridge with Spotify Connect Web API whenever required.
If no devices show up you can start Spotify App on your PC/Mac/iOS/Android and start playing on your devices as 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 bridge will only find active devices known by the Spotify Web API at the time the discovery is triggered.
Should the bridge configuration be broken for any reason, the authorization procedure can be reinitiated from step 6 whenever required.
You can force reinitialization by authorizing on the connect Spotify page, even if the page shows it as authorized. This will reset the refresh token.
The following configuration options are available on the Spotify Bridge player:
| clientId | This is the Client ID provided by Spotify when you add a new Application for openHAB to your Spotify Account. Go to <https://developer.spotify.com/> (Required) |
| clientSecret | This is the Client Secret provided by Spotify when you add a new Application for openHAB to your Spotify Account. (Required) |
| refreshPeriod | This is the frequency of the polling requests to the Spotify Connect Web API in seconds. |
| deviceName | This 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 Spotify Player app on your PC/Mac/iPhone/Android/xxx you should be able to add it as a thing.
Some devices can be restricted and not available for playing. The bridge will make these available in the discovery of devices, 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 (i.e. Chrome casts) when they are not active (they go into a 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/bridges they should show up whenever you initiate discovery of things.
If no devices are showing up, try to connect to the device(s) from your smartphone or computer to make sure the device(s) are in use by your user account.
The discovery of devices in the Spotify Web API is based on what is known by Spotify.
There is difference between e.g. smartphones and computers which can discover devices on the local network and the Web API which is not able to do so.
It only knows about a device if your account is currently associated with the device.
## Channels
### Bridge / Player
The channels on the bridge are the ones used to both control the active device and get details of currently playing music on the Spotify Account associated with the bridge.
In this example there is a bridge configured with Thing ID **user1** and illustrating that the bridge is authorized to play in the context of the Spotify user account **user1**.
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 - one time 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 does 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 they are available.
As soon as you press play, next, prev 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 playing of a Spotify Connect device.
This is different from what you see in smartphone app or the computer application.
There you are able to actively take over playing of a device even if someone else is playing on it.