openHAB integration to Spotify Web Connect API (player)

@unparagoned Thanks so much for the quick reply. I’m going to try MapDB right away.

I also don’t see the String spotify_auth “http://debra.local:8080/static/spotify-auth.html” in my spotify.items file. I’m adding it in now.

Finally got persistence to work. my.openHAB and MapDB didn’t work for me. I finally switched to mqtt and got it working.

ImportError: No module named myopenhab

That is the error I get when trying to run the spotify.py file in terminal after the install. I got the New Auth Code successful and saved to OpenHab after running the spotify_auth.hml file.

The habpanel shows up but none of my info from my Spotify account is populated and no buttons work.

I get this when i try to add the client ID and Client secret.

curl: (6) Could not resolve host: text
curl: (6) Could not resolve host: application
curl: (6) Could not resolve host: “openhabianpi.local

Help!?!

Try using IP address of your OH system instead of openhabianpi.local

Hi
have tried it.
same error …

pi@raspberrypi:~ $ curl -X PUT --header “Content-Type: text/plain” --header “Accept: application/json” -d “/1cacxxxxxxxxxxxxxxxxxxxxxxxxxxeaa4” “192.168.1.77:8080/rest/items/spotify_client_id/state”
curl: (6) Could not resolve host: text
curl: (6) Could not resolve host: application
curl: (6) Could not resolve host: “192.168.1.77

Remove state from the url, also I’m not sure why you have a forward slash to start your id. Just use the REST API page to set the value rather than struggling with curl. Or have a look above where I posted a habpanel widget to set the client id and pass. It might just be formatting but have a look at the type of quotes you are using, don’t past the quotes, type them in. " != “

curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "1cacxxxxxxxxxxxxxxxx" "http://openhabianpi:8080/rest/items/spotify_client_id"

This is great, but I just can’t seem to interact with spotify from the panel, I’ve tried sending next, play, pause to spotify_action, and nothing. The volume slider I created has no effect, it all seems read only. I’m on Windows, if that makes a difference.

Hi Guys, thanks for the script!

I have a noob quesion. I’ve done all the steps of the Tutorial but now i have no idea using this in the “Basic UI”. Is that possible to, or is the layout shown in the pic (frist post) only possible in “Hab Panel”

Thanks for your help!

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
-> Error refreshing token:{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
Successfully posted state to OpenHab: spotify_access_token = XXXXXXXXZ714aPay4q4iVj2FQqZvUcNA2aVwEq4PofvuKGbOrtAzKwEKyinYUn4eAkfrYuKiAFeiId3ac1ZL-ecRZIMHn1vgcpXleJdKSjWaQ
Successfully posted state to OpenHab: spotify_refresh_token = XXXXXXXXOQzoAMOVk4pRgVzUv8cO45POAlPoqQjXNq0P4dsdfPgD3sDsqwlhnPi_CSaXbCE35hgMPZYk
Successfully posted state to OpenHab: spotify_token_expiry = 1520217770.44
Successfully posted state to OpenHab: spotify_token_issued = 2018-03-04T20:42:50
– Calling Service: Update

https://api.spotify.com/v1/me/player/
-> Failure: <type ‘exceptions.ValueError’>
Successfully posted state to OpenHab: spotify_lastConnectionDateTime = 2018-03-05T01:42:50.000+0000
Done in 0.938762903214 seconds

Basic UI is pretty easy to setup. Here are some examples

Switch item=spotify_action mappings=["pause"="pause", "play"="play", "current"=current]
Text item=spotify_current_track label="Track [%s]"
Text item=spotify_current_artist label="[%s]"

@gman Check that the rule that sends the command is setup correctly, most people run it on linux so the path to the script might be different. Try to do it manually from cmd, then edit the rule that executes the command. Try searching the thread for more details.

python c://path/scripts/spotify.py play

@oneofayykind search the thread. There are a couple reasons that might be the cause. 1. You need to fix the time settings, a simple search of the thread will show you what to do. 2. You are playing offline music.

@unparagoned I checked the time settings and I believe they are right?

def updateConnectionDateTime(self):
self.oh.sendCommand(‘spotify_lastConnectionDateTime’,time.strftime("%Y-%m-%dT%H:%M:%S.000+0000",time.gmtime(time.time())))

The player doesn’t load so I’m not sure what I’m playing?

How did you fix this error @Jonas88

hi! I just got rid of the code sentences that handle the lastconnectiondatetime.
I could not get it working with it either.
I don’t need that information so i did not care.

@oneofayykind

    def updateConnectionDateTime(self):
        self.oh.sendCommand('spotify_lastConnectionDateTime',time.strftime("%Y-%m-%dT%H:%M:%S.000+0000",time.gmtime(time.time())))  

This works for me.

I removed the lastconnectiondatetime and I still get this error when running:

– Calling Service: Update

https://api.spotify.com/v1/me/player/
-> Failure: <type ‘exceptions.ValueError’>

I wonder if my persistence isn’t setup correctly? What else could be causing this?

One thing I notice is when I go to the http://localhost:8080/static/spotify-auth.html page and authenticate, it works so I know I have the right URI redirect link. I hit authenticate and it does. I then go back to that link and it asks to authenticate again. If I google that exception error Spotify says it’s a 401 error no token provided.

Have you got spotify running on your computer? Start it playing using the spotify software and see if update works.

If I play it on my computer that does work. The HABPanel updates even, though nothing in the panel works, but I’m getting closer. If I run the script in terminal again it updates the HABPanel.

Also now I’m getting a 401 error the access token is expired. I’m logged into my app and I refresh the auth code and still the same thing. It doesn’t do it if I create a new app.

Hey this look really nice … I try it on my server everything worked fine until I launched the .py script…

this is what I get :

Error getting state from OpenHab: spotify_client_id (HTTP Response 404)
Error getting state from OpenHab: spotify_client_secret (HTTP Response 404)
Error getting state from OpenHab: spotify_access_token (HTTP Response 404)
Error getting state from OpenHab: spotify_refresh_token (HTTP Response 404)
Error getting state from OpenHab: spotify_token_issued (HTTP Response 404)
Error getting state from OpenHab: spotify_token_expiry (HTTP Response 404)
Traceback (most recent call last):
  File "spotify.py", line 384, in <module>
    main()
  File "spotify.py", line 347, in main
    c = spotify()
  File "spotify.py", line 47, in __init__
    if (time.time() > float(self.token_expiry)):
ValueError: could not convert string to float: 

For info my openhab run on port 7070 but I put this into Spotify URI and in the py script : MYIP:7070/static/spotify-auth.html

I saw a myopenha.py script should I give access to myopenhab to the Spotify items ?