openHAB integration to Spotify Web Connect API (player)

@unparagoned

I think the first rule is working fine, as everything updates with current playing status when the cron runs.

Can you see anything wrong with the second rule which might be stopping me from sending commands back to spotify?

    rule "Spotify run script"
when
                Time cron "0/60 * * * * ? *" or
		Item spotify_forceupadte received update
	then		
			val resp =  executeCommandLine("C:/Python27/python.exe C:/openhab2/conf/scripts/spotify.py", 5000)
			logInfo("Spotify", resp)
end
rule "Spotify Action"
when
		Item spotify_action received update
	then		
			val resp =  executeCommandLine("C:/Python27/python.exe C:/openhab2/conf/scripts/spotify.py" + spotify_action.state.toString, 5000)
			logInfo("Spotify", resp)
end

If the first rule works I would have expected the second to work as well. There are a few posts on this board somewhere about issues around executing commands on windows, there were issues with spaces, etc. they might shed some light.

I use receivedCommand rather than the item stat since sometimes the item take some time to update, but that doesn’t sound like the issue here.

val resp =  executeCommandLine("/usr/bin/python /etc/openhab2/scripts/spotify.py " + receivedCommand.toString, 200000)

Try setting the rule manually to play. And try running the command in the quotes on cmd to see if it works there.

val resp =  executeCommandLine("C:/Python27/python.exe C:/openhab2/conf/scripts/spotify.py play", 5000)

Otherwise buy a raspberry pi, they are dead cheap and probably a much better setup than running it on a windows box. If it’s too much then pm me your address and I’ll buy you one.

@Dominic_Bonneau
Try openhabianpi.local:7070/static/spotify-auth.html or whatever you computer is called on your network. I don’t think using your public ip would work here, maybe your local ip would but I’m not sure.

Edit: @Dominic_Bonneau I managed to get the same error as you. In myopenhab.py the port is defined in init, change that to 127.0.0.1:7070 then it should work,

@unparagoned

Thanks I’ll try some things re spaces.

i do have a couple of RPI here, but decided to use the windows server for the extra power and the fact it’s already on. But it has been a bit of headache.

@unparagoned

Running from cmd with quotes

C:\Users\Administrator>"C:/Python27/python.exe C:/openhab2/conf/scripts/spotify.
py"
The filename, directory name, or volume label syntax is incorrect.

and without quotes

C:\Users\Administrator>C:/Python27/python.exe C:/openhab2/conf/scripts/spotify.p
y
Successfully got state from OpenHab: ←[0;37mspotify_client_id←[0m
Successfully got state from OpenHab: ←[0;37mspotify_client_secret←[0m
Successfully got state from OpenHab: ←[0;37mspotify_access_token←[0m
Successfully got state from OpenHab: ←[0;37mspotify_refresh_token←[0m
Successfully got state from OpenHab: ←[0;37mspotify_token_issued←[0m
Successfully got state from OpenHab: ←[0;37mspotify_token_expiry←[0m
-- Calling Service: Update
Successfully posted state to OpenHab: ←[0;37mspotify_current_track ←[0m= ←[0;36m
Keep That Ligh - 7 "←[0m
Successfully posted state to OpenHab: ←[0;37mspotify_current_artist ←[0m= ←[0;36
mThe Tamlins←[0m
Successfully posted state to OpenHab: ←[0;37mspotify_current_cover ←[0m= ←[0;36m
https://i.scdn.co/image/80be2195a004f1bc742dd02a680a5354e9b9d650←[0m
Successfully posted state to OpenHab: ←[0;37mspotify_current_duration ←[0m= ←[0;
36m226346←[0m
Successfully posted state to OpenHab: ←[0;37mspotify_current_progress ←[0m= ←[0;
36m218472←[0m
Successfully posted state to OpenHab: ←[0;37mspotify_current_playing ←[0m= ←[0;3
6mON←[0m
Successfully posted state to OpenHab: ←[0;37mspotify_current_device ←[0m= ←[0;36
mG-PC←[0m
Successfully posted state to OpenHab: ←[0;37mspotify_current_volume ←[0m= ←[0;36
m100←[0m
Successfully posted state to OpenHab: ←[0;37mspotify_current_context_uri ←[0m= ←
[0;36mspotify:album:36q2E7Z20V7mI07ZD9zPdg←[0m
Successfully posted state to OpenHab: ←[0;37mspotify_current_device_id ←[0m= ←[0
;36m5e2540bc0eb417d3a5a650b9fe8a3afec6bbe404←[0m
Successfully posted state to OpenHab: ←[0;37mspotify_current_progress_percent ←[
0m= ←[0;36m96.52←[0m
 -> Success
Successfully posted state to OpenHab: ←[0;37mspotify_lastConnectionDateTime ←[0m
= ←[0;36m2018-03-05T21:46:37←[0m
Done in 2.10400009155 seconds

You said update works fine, you want to test play.

``
C:/Python27/python.exe C:/openhab2/conf/scripts/spotify.py play

I just get the same results, it just updates and that’s what I see. I need to do some reading on running python on windows as whatever I add after the .py will give [Errno 2] No such file or directory.

So I don’t know if its spacing, or quotes, or what, it must be different to running it on linux.

@gman and @Dominic_Bonneau

I tried using the script from windows and I got it working fine. I just mounted the linux folder to Y and ran the windows version of python over the script. Note the slashes are the other way.

I had to make one change to myopenhab.py because I was running script on a different computer than openhab, but other than that I didn’t have any issues. I changed the ip in myopenhab from 127.0.0.1 to openhabianpi or whatever your computer is called.

    def __init__(self):

    	self.openhab_ip = "openhabianpi:8080"

First change your dir to the one the script is in. Then this should work. Note the backwards slashes.

# First change your dir to the one the script is in. Then run the following.
Y:\scripts>C:\Python27\python.exe spotify.py play

If you are still having problems try my forked version of the script, it has a bit that should sort out running the script from any dir.

def main():

    t1 = time.time()
    c = spotify()
    args = sys.argv
    
    if( __file__ != "spotify.py" ):
        os.chdir(__file__.replace("spotify.py",""))
...
1 Like

@unparagoned I’ve got it working now by adding Python27 to PATH in system variables.

Now I can finish my music dashboard, cheers.

Hey I saw 2 or 3 Spotify widget here that use Spotify api can someone share the code ? :smiley:

I finally got this thing working, though couple of issues still to work out. One, I don’t see any of the svg icons and don’t know how to set that up. I downloaded the cheaper version of the squidink icons (1,000 vs 2,000). There is no big one large svg file in that download pack so I’m trying to understand how svg works and I can get these icons in here. Any ideas of where I can look to learn that? All my svg icons are saved as separate files.

If you place your SVG files in (\conf\icons\classic) you can call them from there.

In mine I have spotify-on.svg and spotify-off.svg

Then you could add a switch, set icon to custom, and in the icon box just type spotify, no need to type on or off or even .svg

Perfect!

One other question hoping someone can help with. My playlists are empty when I click on them, and when I click on the devices drop-down I get my devices yet when I click on them the device it’s playing on doesn’t change.

I am an beginner and I have tried several times to have this up and running, but I fail all the time.
I get this error message:
Successfully got state from OpenHab: spotify_client_id
Successfully got state from OpenHab: spotify_client_secret
Successfully got state from OpenHab: spotify_access_token
Successfully got state from OpenHab: spotify_refresh_token
Successfully got state from OpenHab: spotify_token_issued
Successfully got state from OpenHab: spotify_token_expiry
– Calling Token Refresh Service
{‘Content-Length’: ‘69’, ‘Keep-Alive’: ‘timeout=600’, ‘Server’: ‘nginx’, ‘Connection’: ‘keep-alive’, ‘Date’: ‘Wed, 07 Mar 2018 08:33:02 GMT’, ‘Content-Type’: ‘application/json’}
{u’error_description’: u’Invalid refresh token’, u’error’: u’invalid_grant’}
Successfully got state from OpenHab: spotify_auth_code
– Calling Token Service for the first time
{‘X-Content-Type-Options’: ‘nosniff’, ‘Content-Security-Policy’: “default-src ‘self’; script-src ‘self’ https://d2d1dxiu3v1f2i.cloudfront.net https://www.google-analytics.com https://www.google.com https://www.gstatic.com; img-src ‘self’ https://d2d1dxiu3v1f2i.cloudfront.net https://i.imgur.com https://d2mv8tnci56s9d.cloudfront.net https://profile-images.scdn.co https://aci.scdn.co https://graph.facebook.com https://fbcdn-profile-a.akamaihd.net https://scontent.xx.fbcdn.net https://www.google-analytics.com https://stats.g.doubleclick.net data: d2d1dxiu3v1f2i.cloudfront.net; font-src ‘self’ https://d2d1dxiu3v1f2i.cloudfront.net https://sp-bootstrap.global.ssl.fastly.net https://fonts.gstatic.com; style-src ‘self’ ‘unsafe-inline’ https://d2d1dxiu3v1f2i.cloudfront.net; frame-src ‘self’ https://www.spotify.com https://www.google.com https://app.adjust.com https://itunes.apple.com itms-apps:;”, ‘Content-Encoding’: ‘gzip’, ‘Transfer-Encoding’: ‘chunked’, ‘Strict-Transport-Security’: ‘max-age=31536000’, ‘Vary’: ‘Accept-Encoding’, ‘Keep-Alive’: ‘timeout=600’, ‘Server’: ‘nginx’, ‘Connection’: ‘keep-alive’, ‘X-UA-Compatible’: ‘IE=edge’, ‘Pragma’: ‘no-cache’, ‘Cache-Control’: ‘no-cache, no-store, must-revalidate’, ‘Date’: ‘Wed, 07 Mar 2018 08:33:03 GMT’, ‘X-Frame-Options’: ‘deny’, ‘X-Content-Security-Policy’: “default-src ‘self’; script-src ‘self’ https://d2d1dxiu3v1f2i.cloudfront.net https://www.google-analytics.com https://www.google.com https://www.gstatic.com; img-src ‘self’ https://d2d1dxiu3v1f2i.cloudfront.net https://i.imgur.com https://d2mv8tnci56s9d.cloudfront.net https://profile-images.scdn.co https://aci.scdn.co https://graph.facebook.com https://fbcdn-profile-a.akamaihd.net https://scontent.xx.fbcdn.net https://www.google-analytics.com https://stats.g.doubleclick.net data: d2d1dxiu3v1f2i.cloudfront.net; font-src ‘self’ https://d2d1dxiu3v1f2i.cloudfront.net https://sp-bootstrap.global.ssl.fastly.net https://fonts.gstatic.com; style-src ‘self’ ‘unsafe-inline’ https://d2d1dxiu3v1f2i.cloudfront.net; frame-src ‘self’ https://www.spotify.com https://www.google.com https://app.adjust.com https://itunes.apple.com itms-apps:;”, ‘Content-Type’: ‘application/json’}
{u’access_token’: u’BQC-o7x_JDUoqOjKo9wY6jJc1ZEyHslr3clQ0q1Da6ZAU64KhV75sJfaG2Nf3fCFdYNmpe5vaQpu3L5jjpW1W4GOmPnEoU2i1mhLldAbs8DTgRrn_v6geKH7hf6T8KhRRbabxL4m_aztKxqgoiABMLJjxiwu’, u’token_type’: u’Bearer’, u’expires_in’: 3600, u’refresh_token’: u’AQD4EpkUZPvhHqB9N0TKR88OJN8RYNyJt8UAXKIXVQf7DuG5RLjyCcLSBEGUy0fn2xnp_ITT5OVpHI2_BCvLpEGnt5opwsEG6soY6bgyn_C2w7InKvTzQq2JBgewRAtIfd4’, u’scope’: u’user-modify-playback-state user-read-playback-state’}
Successfully posted state to OpenHab: spotify_access_token = BQC-o7x_JDUoqOjKo9wY6jJc1ZEyHslr3clQ0q1Da6ZAU64KhV75sJfaG2Nf3fCFdYNmpe5vaQpu3L5jjpW1W4GOmPnEoU2i1mhLldAbs8DTgRrn_v6geKH7hf6T8KhRRbabxL4m_aztKxqgoiABMLJjxiwu
Successfully posted state to OpenHab: spotify_refresh_token = AQD4EpkUZPvhHqB9N0TKR88OJN8RYNyJt8UAXKIXVQf7DuG5RLjyCcLSBEGUy0fn2xnp_ITT5OVpHI2_BCvLpEGnt5opwsEG6soY6bgyn_C2w7InKvTzQq2JBgewRAtIfd4
Successfully posted state to OpenHab: spotify_token_expiry = 1520415183.14
Successfully posted state to OpenHab: spotify_token_issued = 2018-03-07T09:33:03
– Calling Service: Update
-> Failure: <type ‘exceptions.ValueError’>
Successfully posted state to OpenHab: spotify_lastConnectionDateTime = 2018-03-07T08:33:03.000+0000
Done in 1.34987711906 seconds

if I do sudo apt-get install python-requests then I get this error
Successfully got state from OpenHab: spotify_client_id
Successfully got state from OpenHab: spotify_client_secret
Successfully got state from OpenHab: spotify_access_token
Successfully got state from OpenHab: spotify_refresh_token
Successfully got state from OpenHab: spotify_token_issued
Successfully got state from OpenHab: spotify_token_expiry
– Calling Service: Update
-> Failure: <type ‘exceptions.ValueError’>
Successfully posted state to OpenHab: spotify_lastConnectionDateTime = 2018-03-07T08:37:24.000+0000
Done in 0.539511919022 seconds

Can any one guide me to the root of the error?

Hi all, trying to set this up - new to rest and curl…

Trying to set the client ID and secret via curl and get this error:
curl: (6) Could not resolve host: text
curl: (6) Could not resolve host: application
curl: (1) Protocol ““http” not supported or disabled in libcurl

Anyone shed some light on this? :slight_smile:

Thanks!

James

Hi @oneofayykind ,
how you got success with the error ?

-> Failure: <type ‘exceptions.ValueError’>

Tanks for help !

Hi,

I just install the API and it’s work great with the python script but how I’m suppose to use it in openhab and in my rules ?

should I map item to a exec ?

very strange my -> Failure: <type ‘exceptions.ValueError’> just dissapeared. I did not change any thing. The only differance was that I was now playing music on Spotify when I run the script

1 Like

I
I used the code below, usually it works great but some times not - I do not understand why. I do not know if the sleeps are required

rule "Spotify Test"
when
Item SpotifyTest received command OFF
then
        { 
	spotify_current_device_id.sendCommand("2546d8ce80580c473293d30e2c3477afaf985613") //Shield
	Thread::sleep(50)
	spotify_action.sendCommand("transfer_playback")
	Thread::sleep(50)
	spotify_action.sendCommand("play spotify:user:spotify:playlist:37i9dQZF1DWTMYgB8TqtmR")	//Hits Idag
	Thread::sleep(50)
	spotify_action.sendCommand("play")

        }	
end

:smiley: world can be easy. That worked in my case as well.

Thanks Patrick!

I just started using this integration yesterday and it was really easy to set up and works great so thanks to everyone who’s put work into it!

Does anybody else have issues with certain chromecast devices not ‘showing’ themselves to Spotify?

I have a Google Home as well as a standard Chromecast, plugged into my TV. When I use ‘transfer_playback’, it only works if I used Spotify on that chromecast device recently. I checked the spotify_device_list and the chromecasts disappear from the list after a while (when I use ‘get_devices’ to update)

I guess the problem is on Spotify’s end rather than anything to do with the openhab integration as a lot of the time I can’t see my chromcast devices in the Spotify app either.