openHAB integration to Spotify Web Connect API (player)

Could you please share a screeshot of your widget?
@pmpkk s widget looks a little scattered across the screen…

Here is the screenshot of my main page. It’s essentially pmpkk’s framework but with some heavy modding. Also be aware that my widgets might not be compatible with the proper pmpkk’s api.

Everything you need should be on my github. https://github.com/unparagoned/openhab_spotify-webconnect-api
The version of the spotify api on there should work, and is less likely to total your machine than my previous builds. The widget has it’s original name Ground Floor Widget.

If I had done this properly I would have pushed the improvements onto pmpkk’s github, but I hadn’t written anything in python before and still getting a handle on github.

Thanks, that really looks great, but I am doing something wrong.

I am able to control spotify with the controls on the right:

Your widget is just looking good with the matrix theme. But with the regular css (no matrix) all the items are scattered all over the place (in the screenshot you cannot even see the headline “Music”).

Is anyone out there who managed to get a proper look of the spotify widget without the matrix theme?

After upgrading to OH 2.2, build #1103, I get this in my openhab.log:

2017-12-01 22:37:34.856 [WARN ] [rest.core.internal.item.ItemResource] - Received HTTP PUT request at 'items/spotify_lastConnectionDateTime/state' with an invalid status value '2017-12-01T21:37:34+0000'.
2017-12-01 22:38:04.796 [WARN ] [rest.core.internal.item.ItemResource] - Received HTTP PUT request at 'items/spotify_lastConnectionDateTime/state' with an invalid status value '2017-12-01T21:38:04+0000'.
2017-12-01 22:38:34.503 [WARN ] [rest.core.internal.item.ItemResource] - Received HTTP PUT request at 'items/spotify_lastConnectionDateTime/state' with an invalid status value '2017-12-01T21:38:34+0000'.
2017-12-01 22:39:04.576 [WARN ] [rest.core.internal.item.ItemResource] - Received HTTP PUT request at 'items/spotify_lastConnectionDateTime/state' with an invalid status value '2017-12-01T21:39:04+0000'.
2017-12-01 22:40:34.887 [WARN ] [rest.core.internal.item.ItemResource] - Received HTTP PUT request at 'items/spotify_lastConnectionDateTime/state' with an invalid status value '2017-12-01T21:40:34+0000'.

And this continues every 30 seconds. Any idea what this could be?

I followed the guide, buy I always get an “ValueError”. I checked also the devices, but it is empty. Does anybody know how I can fix this?

[19:13:04] openhabian@openHABianPi:~$ /usr/bin/python /etc/openhab2/scripts/spotify.py
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 = 2017-12-03T18:13:10+0000
Done in 0.469512939453 seconds

I also encountered this issue. It seems that some behaviour changed. You can notice that the milliseconds are gone in the timestamp, which create the error.

You need to modify the following line (in my script line 478) in the Spotify script:

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

notice the .000 after %S.

Is anyone able to get either of the code sets to fire playlist off in shuffle mode?
I eventually get tired of the playlist always starting the same way.

Thanks

Is it true that I need to re-auth openhab after every restart?
Even with persistence storing correctly the auth code I need to run the spotify-auth.html again.

No. If persistance works correctly then that’s not necessary!

alright - thanks for the quick response. I will test it on the next restart and will be more patient :wink:

Please allow one more question - I struggle with setting the device.

I try to trigger a switch for my Amazon Alexa:

rule "Spotify change to Alexa"
when
	Item spotify_device_Alexa received command ON
then
	spotify_current_device_id.postUpdate("90e528c7a39axxxxxxx18f859fd96be8dd807")
	val resp = executeCommandLine("/usr/bin/python /etc/openhab2/scripts/spotify.py transfer_playback", 5000)
	logInfo("spotify.rules", "spotify_current_device set to Alexa")
	logInfo("spotify.rules", resp)
end

So my understanding is, that I need to set the new device_id and send the command transfer_playback.

But it doesn’t seem to work.

The log shows:

-- Calling Service: Transfer Playback
Successfully got state from OpenHab: ^[[0;37mspotify_current_device_id^[[0m
{"device_ids": ["90e528c7a39axxxxxxx18f859fd96be8dd807"]}
404

but right after that the device_id goes back to my iphone:

Successfully posted state to OpenHab: ^[[0;37mspotify_current_device_id ^[[0m= ^[[0;36m2fce3498847be4167b01xxxx7c57a89f1117d^[[0m

Is this a tming issue? Or is their an option so send
spotify.py transfer_playback device_id 90e528c7a39axxxxxxx18f859fd96be8dd807 ??
Sorry for this possibly stupid questions.

Hi im stuck here two.
really dont understand where to put the ID and the secret.
can you explain it in another way ?

thx

I think so I had the same problem. Its like spotifies api is out of sync so when you run update it gives you back old details. In my branch I commented out all updates and run them manually from the rule rather than as part of the python script.

@Rasmus_Johansen the gihub readme explains how to set the ID and secret, there are also details in this thread.

Edit:
Iinux the following commands should work fine from terminal. You probably can do the same from powershell in windows. Or get the postman plugin for chrome.

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

I made a panel to make it easier for me in the past. I’ve tried to clean it up and make it more general. So you just past your id in and secret, press the update buttons then click the link to the auth page and it should auth fine.

Hi @bennYx0x,

thanks for your suggestion (I have the same issue as @Dries).
However, after changing the line according to your suggestion, I get the following when calling spotify.py:

Traceback (most recent call last):
  File "/etc/openhab2/scripts/spotify.py", line 388, in <module>
    main()
  File "/etc/openhab2/scripts/spotify.py", line 382, in main
    c.updateConnectionDateTime()
AttributeError: 'spotify' object has no attribute 'updateConnectionDateTime' 

line 382:
c.updateConnectionDateTime()

line 388:
main()

any suggestion?

Hey NCO,

it looks like the method cant be find anymore, maybe you have a typo in your script after you adjusted your script to my changes?

The following line seems to be missing or malformed in your spotify script:

def updateConnectionDateTime(self):

Thanks for your quick response.
That was actually the problem.
I just copied and pasted your two lines mentioned somwhere above, but did mess it up (don’t know what or why).
This time I just added: %S.000 and that fixed it.
:slight_smile:

1 Like

Glad to hear! No problem :slight_smile:

Hi guys,

I’ve not had any responses.
Can a playlist be started with shuffle enabled?

Seems possible from the api

nice script!
but im having troubles getting it working.
it shows the current title, track artwork, but i cant get the commands like play/pause/volume working.
always getting 403.
any ideas?
do i need spotify premium for it?

2018-01-04 14:52:43.728 [ome.event.ItemCommandEvent] - Item 'spotify_action' received command pause

2018-01-04 14:52:43.742 [vent.ItemStateChangedEvent] - spotify_action changed from next to pause

2018-01-04 14:52:44.610 [vent.ItemStateChangedEvent] - spotify_current_playing changed from ON to OFF

==> /var/log/openhab2/openhab.log <==

2018-01-04 14:52:44.634 [WARN ] [rest.core.internal.item.ItemResource] - Received HTTP PUT request at 'items/spotify_lastConnectionDateTime/state' with an invalid status value '2018-01-04T13:52:44+0000'.

2018-01-04 14:52:44.720 [INFO ] [lipse.smarthome.model.script.Spotify] - 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: Pause

Successfully posted state to OpenHab: spotify_current_playing = OFF

403

Error posting state to OpenHab: spotify_lastConnectionDateTime (HTTP Response State could not be parsed: 2018-01-04T13:52:44+0000)

Done in 0.4285800457 seconds
1 Like

Hello Guy´s
i need help at this step. I am a newbie in openhab
How and where do i check that the spotify_auth_code is set in OpenHab
How and where do i Set the REDIRECT_URI in spotify.py to the right value
How do i use the spotify.py