openHAB integration to Spotify Web Connect API (player)

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!

Those having issues on windows may want to look at how the path variable is set up for python. I assume it won’t be /usr/bin/python

Setted the path on windows variables and from win console i can use “python [scriptPath]” without problem, but still not working on openhab console, the problem is the missing of error message :confused:

Hi all,i have this message when test spotify.py,
Successfully got state from OpenHab: spotify_token_expiry
– Calling Service: Update
{
“timestamp” : 1502806217089,
“progress_ms” : 243053,
“is_playing” : true,
“item” : {
“album” : {
“album_type” : “compilation”,
“artists” : [ {
“external_urls” : {
“spotify” : “https://open.spotify.com/artist/711MCceyCBcFnzjGY4Q7Un
},
“href” : “https://api.spotify.com/v1/artists/711MCceyCBcFnzjGY4Q7Un”,
“id” : “711MCceyCBcFnzjGY4Q7Un”,
“name” : “AC/DC”,
“type” : “artist”,
“uri” : “spotify:artist:711MCceyCBcFnzjGY4Q7Un”
} ],
“available_markets” : [ “AD”, “AR”, “AT”, “AU”, “BE”, “BG”, “BO”, “BR”, “CA”, “CH”, “CL”, “CO”, “CR”, “CY”, “CZ”, “DE”, “DK”, “DO”, “EC”, “EE”, “ES”, “FI”, “FR”, “GB”, “GR”, “GT”, “HK”, “HN”, “HU”, “ID”, “IE”, “IS”, “IT”, “JP”, “LI”, “LT”, “LU”, “LV”, “MC”, “MT”, “MX”, “MY”, “NI”, “NL”, “NO”, “NZ”, “PA”, “PE”, “PH”, “PL”, “PT”, “PY”, “SE”, “SG”, “SK”, “SV”, “TR”, “TW”, “US”, “UY” ],
“external_urls” : {
“spotify” : “https://open.spotify.com/album/4ydl8Ci7OsndhI2ALnrpIv
},
“href” : “https://api.spotify.com/v1/albums/4ydl8Ci7OsndhI2ALnrpIv”,
“id” : “4ydl8Ci7OsndhI2ALnrpIv”,
“images” : [ {
“height” : 640,
“url” : “https://i.scdn.co/image/a6d307979309bfc1b8ba9ca64aeae4dc9f51f1ce”,
“width” : 640
}, {
“height” : 300,
“url” : “https://i.scdn.co/image/fed88d1739d842a4b706594ed2af44720634fdd3”,
“width” : 300
}, {
“height” : 64,
“url” : “https://i.scdn.co/image/9b3cc150a0fea3e7fdeb1c18253df9570046ca09”,
“width” : 64
} ],
“name” : “Iron Man 2”,
“type” : “album”,
“uri” : “spotify:album:4ydl8Ci7OsndhI2ALnrpIv”
},
“artists” : [ {
“external_urls” : {
“spotify” : “https://open.spotify.com/artist/711MCceyCBcFnzjGY4Q7Un
},
“href” : “https://api.spotify.com/v1/artists/711MCceyCBcFnzjGY4Q7Un”,
“id” : “711MCceyCBcFnzjGY4Q7Un”,
“name” : “AC/DC”,
“type” : “artist”,
“uri” : “spotify:artist:711MCceyCBcFnzjGY4Q7Un”
} ],
“available_markets” : [ “AD”, “AR”, “AT”, “AU”, “BE”, “BG”, “BO”, “BR”, “CA”, “CH”, “CL”, “CO”, “CR”, “CY”, “CZ”, “DE”, “DK”, “DO”, “EC”, “EE”, “ES”, “FI”, “FR”, “GB”, “GR”, “GT”, “HK”, “HN”, “HU”, “ID”, “IE”, “IS”, “IT”, “JP”, “LI”, “LT”, “LU”, “LV”, “MC”, “MT”, “MX”, “MY”, “NI”, “NL”, “NO”, “NZ”, “PA”, “PE”, “PH”, “PL”, “PT”, “PY”, “SE”, “SG”, “SK”, “SV”, “TR”, “TW”, “US”, “UY” ],
“disc_number” : 1,
“duration_ms” : 273840,
“explicit” : false,
“external_ids” : {
“isrc” : “AUAP07900029”
},
“external_urls” : {
“spotify” : “https://open.spotify.com/track/1V6kRlJnTC4W319N0BvJag
},
“href” : “https://api.spotify.com/v1/tracks/1V6kRlJnTC4W319N0BvJag”,
“id” : “1V6kRlJnTC4W319N0BvJag”,
“name” : “If You Want Blood (You’ve Got It)”,
“popularity” : 61,
“preview_url” : “https://p.scdn.co/mp3-preview/e964a5be5f2f6f13712e81ab02713df08d8421c2?cid=f1fbae74042d42c9816a88e6c04d4098”,
“track_number” : 7,
“type” : “track”,
“uri” : “spotify:track:1V6kRlJnTC4W319N0BvJag”
},
“context” : {
“external_urls” : {
“spotify” : “http://open.spotify.com/user/spotify/playlist/37i9dQZF1DXec50AjHrNTq
},
“href” : “https://api.spotify.com/v1/users/spotify/playlists/37i9dQZF1DXec50AjHrNTq”,
“type” : “playlist”,
“uri” : “spotify:user:spotify:playlist:37i9dQZF1DXec50AjHrNTq”
},
“device” : {
“id” : “13cfd1cda5002a9b87b3302827b77404ffa855bc”,
“is_active” : false,
“is_restricted” : false,
“name” : “Spotify Web Player”,
“type” : “Computer”,
“volume_percent” : 100
},
“repeat_state” : “off”,
“shuffle_state” : false
}
Successfully posted state to OpenHab: spotify_current_track = If You Want Blood (You’ve Got It)
Successfully posted state to OpenHab: spotify_current_artist = AC/DC
Successfully posted state to OpenHab: spotify_current_cover = https://i.scdn.co/image/fed88d1739d842a4b706594ed2af44720634fdd3
Successfully posted state to OpenHab: spotify_current_duration = 273840
Successfully posted state to OpenHab: spotify_current_progress = 243053
Successfully posted state to OpenHab: spotify_current_progress = 243053
Successfully posted state to OpenHab: spotify_current_playing = ON
Successfully posted state to OpenHab: spotify_current_device = Spotify Web Player
Successfully posted state to OpenHab: spotify_current_volume = 100
Successfully posted state to OpenHab: spotify_current_device_id = 13cfd1cda5002a9b87b3302827b77404ffa855bc
-> Success
Successfully posted state to OpenHab: spotify_lastConnectionDateTime = 2017-08-15T14:13:49+0000
Done in 1.20426917076 seconds
Why?
thanks

I think you have the Debug flag turned on. Otherwise it looks like it’s doing what it is supposed to.

The JSON in the log is the API response. When debug is off, it is not output.

Thanks

I have played around a bit with this now and I have an improvement suggestion.

How about adding some config variables at the very top of the py script?
(I am not so familiar with python but this should be possible i think?!)

In my case Volume up by 10% was a bit too much for the yamaha device i was running the playback,
so i changed the volume up and down buttons to 5% in the .py file.
Some config values for senseful thiings would be really nice.

What do you think about this?