openHAB integration to Spotify Web Connect API (player)

There is nothing wrong with the widget, as mentioned this was working fine before.

I use the widget to enter the client id and secret, it authenticates correctly. But when I go back into the Open Auth link, it seems its not authenticated.

I also cannot see the token and so on being stored in Influx DB

Its not saving the token it seems.

This was working perfectly before.

The static/spotify-auth.html page will not be clear that it has been authorised. But once you done it once you should never need to do it again. Here mine is fully authorised and work and this is what the auth link looks like.

image

If it is a persistence issue then try this setup.

Mapdb for persistence

mapdb.persist in the persistence folder

Strategies {
		everyHour : "0 0 * * * ?"
    	everyDay  : "0 0 0 * * ?"
		default = everyUpdate
}

Items {
		* : strategy = everyChange, everyDay, restoreOnStartup
}

I reinstalled influx db persistance from Paperui.

I used the Widget

11:53:38.399 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘spotify_client_id’ received command 5c80fa9aebb74b41a6eefe36631de518
11:53:38.415 [INFO ] [smarthome.event.ItemStateChangedEvent] - spotify_client_id changed from NULL to 5c80fa9aebb74b41a6eefe36631de518
11:53:47.371 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘spotify_client_secret’ received command 3dd797838ff540a5b0319a7dc6a5e71c
11:53:47.380 [INFO ] [smarthome.event.ItemStateChangedEvent] - spotify_client_secret changed from NULL to 3dd797838ff540a5b0319a7dc6a5e71c
11:53:53.592 [INFO ] [smarthome.event.ItemStateChangedEvent] - spotify_auth_code changed from NULL to AQAkpr66g43p1f9hY0jBpl7sh8Xm1_p-t7l1MmKO_qTN0DOjjrne6LdsNM2HO_6rBhsi9aHkj0k6UuAADRjTL3wRzKveTfIKTYoaz9FD36l5XMLUtJPPlHaQlvMf7wvqGchUCVv7L4a8VsqzBKivtl4hTQfeppEk1PgBQ3KH8yHpRtE1xOZlViMYDPeMwtmNZIFB8D1YKnVXibUIMeUhNPcsBxhRZq8Lk3etiUK96bEhg9lyGy3ajXasTHbIt6QeHo-XfBmw_ECncgkEGYlIx2jd4Zxi4dIbjdPd1R7pyw-oHWk

Problem remains:

Successfully got state from OpenHab: spotify_token_expiry
-- Calling Token Refresh Service
{'client_secret': 'XXX', 'redirect_uri': 'http://192.168.0.3:8080/static/spotify-auth.html', 'client_id': 'XX', 'refresh_token': 'NULL', 'grant_type': 'refresh_token'}
{'Content-Length': '69', 'Keep-Alive': 'timeout=600', 'Server': 'nginx', 'Connection': 'keep-alive', 'Date': 'Thu, 05 Jul 2018 01:57:25 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
{'Content-Length': '68', 'Keep-Alive': 'timeout=600', 'Server': 'nginx', 'Connection': 'keep-alive', 'Date': 'Thu, 05 Jul 2018 01:57:26 GMT', 'Content-Type': 'application/json'}
{u'error_description': u'Invalid redirect URI', u'error': u'invalid_grant'}
-- Calling Service: Play
Successfully got state from OpenHab: spotify_current_device_id
401
-- Calling Service: Update
{u'error': {u'status': 401, u'message': u'Invalid access token'}}
 -> Item node missing from response :(
-- Calling Service: Get Devices
 -> Device List Failure:  <type 'exceptions.KeyError'>
-- Calling Service: Get Playlists
{u'error': {u'status': 401, u'message': u'Invalid access token'}}
 -> Playlist List Failure:  <type 'exceptions.KeyError'>
Successfully posted state to OpenHab: spotify_lastConnectionDateTime = 2018-07-05T01:57:28.000+0000
Done in 2.9642021656 seconds

I fixed the issue, found an article on Stackoverflow.

Your URI must match in the spotify script and the Web Dev console. I had my private, internal IP address in the script but my external dns name in the Web dev.

Both variants was tried. Internal IP was the first option. Or you ended put it in the Web Dev console at the end?

I did web dev before also. I also emptied the states

Does anyone know how to stop the huge amount of information in the Log file re: Spotify Updates?

There are logInfo(“Spotify”, resp) statements in the spotify.rule file that I’m sure I can remove but then I may miss information.

One rule in in particular generates a whole page off stuff!

Are the large logs an issue?
Why not comment out the log info statements like you suggested?

You don’t need any of the logs unless something is wrong. So you could define DEBUG at the top of the rules and put IF statements around the logInfo statements. So if you have a problem you can then enable later on.

If you want more control you can edit spotify,py and myopenhab.py and comment out print statements to achieve what you want.

1 Like

Thank you. Ill try your suggestions. The playlist rule generates a load of stuff that files the screen. No real issue just see it as pointless info

i followed this tutorial step by step but i do not know where i can do in point 5:

" Give the client id and secret to OpenHab

Save the Client ID to spotify_client_id in OpenHab (e.g. through openhab CLI smarthome:update spotify_client_id {your_id})
Copy the Client Secret to spotify_client_secret in OpenHab (e.g. through the rest API)"

When I send a command it takes a good 5 seconds for anything to happen. Is this normal or is there an issue with my setup? If so any ideas what could be making it slow?

I have a problem with the “spotify action rule”. For some reason it doesn’t send the python command to the spotify-script. The spotify_action-string is updating with every press on my touchscreen, but nothing happens. Here is my .rules file:

rule "Spotify run script"
when
	Time cron "0/30 * * * * ? *" or
	Item spotify_forceupadte received update
then		
	val resp =  executeCommandLine("/usr/bin/python /volume1/@appstore/openHAB/conf/scripts/spotify.py", 5000)
	logInfo("Spotify", resp)
end


rule "Spotify Action"
when
	Item spotify_action received update
then		
	val resp =  executeCommandLine("/usr/bin/python /volume1/@appstore/openHAB/conf/scripts/spotify.py" + spotify_action.state.toString, 5000)
	logInfo("Spotify", resp)
end

The “Spotify run script rule” is working fine! Does anybody know the solution?

That looks fine but I often have issue when using an items state, I always try and use receivedCommand if possible. You can throw a loginfo in to see what command it’s trying to perform

rule "Spotify Action"
when
    Item spotify_action received command
then
logInfor("Spotify", "command {}", receivedCommand.toString)
val resp =  executeCommandLine("/usr/bin/python /etc/openhab2/scripts/spotify.py " + receivedCommand.toString, 500)
end

@thejayo It should be pretty much straight away. Play/Pause shouldn’t take much more than a second. Try running the script directly to see where the delay is.

Thanks for the integration!
Everything is working so far, except that it isn’t showing the last conncetion date.

<div class="widget" ng-click="sendCmd('spotify_forceupadte', 'ON')">
			<div class="icon off"><svg viewBox="0 -30 220 220"><use xlink:href="/static/matrix-theme/icons.svg#refresh"></use></svg></div>
			<div class="name">Update</div>
     	<div class="valueGroup"><div class="value">{{itemValue('spotify_lastConnectionDateTime') | date:'MMM d, HH:mm:ss'}}</div></div>			
		</div>

Hi all,
@pmpkk thanks very much for this!

I am getting the following error

[12:12:54] openhabian@openHABianPi:~$ /usr/bin/python /etc/openhab2/scripts/spotify.py
Traceback (most recent call last):
File “/etc/openhab2/scripts/spotify.py”, line 10, in
from myopenhab import openhab
ImportError: No module named myopenhab

pip install myopenhab doesn’t find any package named that

Any ideas?

Edit:

Nevermind, got it sorted by putting myopen.hab from matrix-theme github in the /etc/openhab2/scripts folder as well

@tr1cks I’ma lso having this problem where the device list goes away on refresh. Then if I edit layout and click run, it comes back.

Any idea what’s causing this?

Hi Guys,

I am able to execute with full success the script (/usr/bin/python /etc/openhab2/scripts/spotify.py) and it updates everything in HabPanel however it doesnt keep all the playlists and devices there, they seem to disappear after a while.

This was working quite well before but seems to have broken when I reinstalled OH2.3.

My persistance is fine, as the file is the same as what I was using before also.

Any thoughts?

@unparagoned you’re quite experienced on this :slight_smile:
Any thoughts

Script appears to work all fine from the console but HabPanel wont hold the display of the devices, playlists etc.

No sound comes out either . I used to see logs in the console constantly of things being refreshed and updated but now only when I execute the script manually do i see that " /usr/bin/python /etc/openhab2/scripts/spotify.py"

Thoughts?

The sound comes out of the Spotify program, so that needs to be running. Start playing music with Spotify and then start controlling it with openhab.

I added refresh buttons for Spotify, playlists and devices. So when you press the refresh button it assigns the item to the display item again. There should be code in my previous posts