openHAB integration to Spotify Web Connect API (player)

Here’s a python script that integrates the Spotify player API (work in progress):

Thanks to this script/rules, I can set up a HabPanel like the screenshot above.

For more on setting up HabPanel with this theme, check out this thread:

https://community.openhab.org/t/custom-theme-in-habpanel-2-1-example/

20 Likes

Sorry, there was a bug in the refresh method. I just fixed. Please get the latest version of spotify.py :slight_smile:

Thanks, its works great now :slight_smile:
Is there a way to trigger playing a specific song or playlist to a spotify connect device?

That’s next on my list! :slight_smile:

I was looking through your instructions on your git and i’m pretty new to openhab, could you explain how i’d do this,
5. Give the client id and secret to OpenHab
Save the Client ID to spotify__client__id in OpenHab
Copy the Client Secret to spotify__client__secret in OpenHab

I used the rest api interface. There are the commands anyway

curl -X PUT --header “Content-Type: text/plain” --header “Accept: application/json” -d “/[Your spotify_client_secret]” “http://openhabianpi.local:8080/rest/items/spotify_client_secret/state

curl -X PUT --header “Content-Type: text/plain” --header “Accept: application/json” -d “/[Your spotify_client_id]” “http://openhabianpi.local:8080/rest/items/spotify_client_id/state

Where [Your spotify_client_secret] is you client secret, etc.

Done!

You can now add a context_uri to the play command, e.g.

spotify.py play spotify:user:spotify:playlist:37i9dQZF1DX5OepaGriAIm

Also, to make this easier from with HabPanel, I created a switch “spotify_action”, which is a string with the command,

e.g. set the state to “play spotify:user:spotify:playlist:37i9dQZF1DX5OepaGriAIm” and it will run the play command for that URI.

Updated files here: https://github.com/pmpkk/openhab_spotify-webconnect-api

Great!
That works also :slight_smile: Now for one final request if I may… Start playing to a specific device like this api:
https://api.spotify.com/v1/me/player

This is how I modified spotify.py to play to a device

`
def play_device(self,new_device):
""“
Resume player device
”""
print "-- Calling Service: Play Device"
try:
device_payload=json.dumps({“device_ids”: [new_device]})
resp = self.call( “” ,“PUT”,device_payload)
self.update()
if (self.debug): print resp
except:
print " -> Play Device Failure: ", sys.exc_info()[0]
resp = “”

        return resp

`

Nice work!! Thank you so much! :slight_smile:

Thank you, Spotify integration is great. I did all the procedures to allow spotify.py to function.

I am struggling to find how to get this into my Habpanel (I am not yet on your great theme). Do I add each item on a widget (if so which type)? Or do i import widgets somewhere?

Thank you.

You might want to have a look at the HabPanel thread which has a really cool interface.

I plan on moving over when I get a chance.

Yes, thanks. I have tried the .css stylesheet, but have a long ways to go on my panel. Just want to see if it works in the default panel as well. i suppose not to get the interactive functions?

I just tried habpanel for the first time and it seems to work if you follow the instructions here https://github.com/pmpkk/openhab-habpanel-theme-matrix.

The spotify interface is on the “ground floor widget” which is “interactive”.

Before that I had made my own makeshift controls and interface in a sitemap accessed through the basic ui, which was basically just adding the spotify items. (The script is a bit nicer now so you may need to add a rule and make some virtual items to trigger them to play/pause, etc.)

Thank you. When I get some time I will look/learn. Glad it is possible.

Hey pmpkk. I forked your thing to play around with. The code is a bit messy but I’ve implemented playing to devices in a quite user friendly way. You can add the device at any point after play, using device id, index, or name (partial match as well)
spotify.py play 1 spotify:album:1Mrv9cxsetXUfwNtQk0Dht spotify.py play 0d1841b0976bae2a3a310dd74c0f3df354899bc8 spotify:album:1Mrv9cxsetXUfwNtQk0Dht spotify.py play "John's Echo" spotify.py play "echo" spotify.py devices /*lists all devices and saves them to spotify_devices */ spotify.py devices home /* These select devices that match either name, id, or the device index spotify,py devices 3
There are also a billion ways to get the decide ids, etc. So it should be simple to implement it in any interface.

I’m not that hot on git-hub etiquette. But if my code isn’t that terrible feel free to move it back to the main branch.

I have that error when testing spotify.py :
/etc/openhab2/scripts $ /usr/bin/python /etc/openhab2/scripts/spotify.py
Traceback (most recent call last):
File “/etc/openhab2/scripts/spotify.py”, line 6, in
import requests
ImportError: No module named requests

any idea ?

1 Like

Which version of python are you using and on which platform?

1 Like

Hello everyone…
at point 7., when i have to test spotify.ph (/usr/bin/python /etc/openhab2/scripts/spotify.py), i have the error on openhab console "Command not found: /usr/bin/python"
I don’t know how to check python position
Running openhab2.1 on windows 10 on “c:\openHab2” and installed Python 3.6.1.
Exec binding installed

Thanks! That’s very cool! I’ll try to merge your changes into my latest version when I get a chance!