openHAB integration to Spotify Web Connect API (player)

Hello everyone, I can not understand how to add spotify to my panel …

I already do the authentication and everything is correct.
But I am still inexperienced and I want to learn how to visualize it in my HABpanel, what should I do?

Add button?
Add Chart?

If someone is so kind I can send the widget because I really do not understand.

regards.

1 Like

Hi everyone again, I know I’ve been a little annoyed asking for help but I have already advanced part of the study.

now I want to know how to delete everything except spotify, is there any program to edit these codes?

Saludos.

i did it

now I need to order, can anyone help me with that?.
add the artist

I am excited, to know nothing to learn a little bit in a short time.

Thx.

Hi all! I followed the instructions but on point 7 (test spotify.py in ssh) it gives me a failure after calling service: update.:
-> Failure: <type ‘exceptions.ValueError’>
can you help me?
on the spotify developers site i did not paste http://openhabianpi.local:8080/static/spotify-auth.html but http://IPADRESSOFMYPI:8080/static/spotify-auth.html because it did not work the other way. i tried both in the spotify.py but i got no luck.

To which value do i have to set the redirect_uri in the spotify.py?

in the doc is written:

  • OpenHab 2.1 installed on Raspberry Pi (or similar)
  • OpenHab REST installed
  • Spotify User Account
  • Spotify Developer Account
  • The script uses OpenHab items to store parameters and credentials. Make sure you have persistance turned on, otherwise all your settingd are lost upon reboot/restart
  • Know your OpenHab root URL/IP and port. In this doc, I assume http://openhabianpi.local:8080/. -> where to put the root? i did http://myip:8080. is this right or do i have to add etc/openhab2 to this url?

how do i have to turn on persistance? i found the menu entry in my paperui and it’s empty. is it turned off or just set to an initial value?

Hi! I got it working now - at least once. after a reboot my data is gone. How do i activate “persistance”? can you link me to a tutorial or make a short explanation, please?

thank you!

hello. i found this and got it working:

As i can see it saves items (in the provided example), so the spotify_client_id and secret should be stored. it does not work for me … after each restart i have to write the values into these 2 items manually to get it working. do i have to add anything else to the persistence file?

Hi Bullet.
I get the same error. I can’t understand what is the right value to set the REDIRECT_URI.
Thanks
Lorenzo

Just one more info.
I get the “import requests” error while in Habpanel

10:58:20.255 [INFO ] [lipse.smarthome.model.script.Spotify] - Traceback (most recent call last):
  File "/etc/openhab2/scripts/spotify.py", line 6, in <module>
    import requests

The Happanel items are not updated, while If I send the command from terminal work propely and the item are correctly updated ?!?

I tried to launch other python script from habpanel but I got the same error !!
maybe any permission issue?

Thanks for your help

Hi is there a way to get a list of my playlists and then dynamically fill a listbox or modal windows?

UPDATED - this works just great :slight_smile:

@pmpkk First of all, thanks for your work on your fantastic ‘matrix’ theme, really nice theme and also a great starting point for me to learn how to customize my own panels/themes.

I also installed your spotify script to enable Spotify playback from HABPanel and it’s working great so far. The only thing I cannot seem to get to work (but this is probably related to the Spotify API) is to get access to my different devices for playback.

I have several Sonos speakers in my house, a spotify client on my Windows PC and several mobile devices with a Spotify app. When I query my devices using the ‘spotify.py’ script I cannot see any of my devices - this is not a problem of the script, when I try the Spotify API web console I get the same result. The permissions are okay (scope) and my access key is also working, but I just cannot see my Sonos devices…

Did you have to perform some additional task to get the Sonos devices to show?

EDIT: To elaborate, when I see the Sonos player data from the Spotify API return, it doesn’t show a device_id (NULL), only a device name.

Successfully posted state to OpenHab: spotify_current_track = Sfumato - Kerri Chandler Remix
Successfully posted state to OpenHab: spotify_current_artist = Matthias Tanzmann
Successfully posted state to OpenHab: spotify_current_cover = https://i.scdn.co/image/17aa60d54abd913270aba8680840ae43c2f237d6
Successfully posted state to OpenHab: spotify_current_duration = 432286
Successfully posted state to OpenHab: spotify_current_progress = 132958
Successfully posted state to OpenHab: spotify_current_playing = ON
Successfully posted state to OpenHab: spotify_current_device = Office
Successfully posted state to OpenHab: spotify_current_volume = 12
Successfully posted state to OpenHab: spotify_current_context_uri =     spotify:user:spotify:playlist:37i9dQZF1DX2TRYkJECvfC
Successfully posted state to OpenHab: spotify_current_device_id = NULL
Successfully posted state to OpenHab: spotify_current_progress_percent = 30.76
 -> Success
Successfully posted state to OpenHab: spotify_lastConnectionDateTime = 2017-09-20T19:05:28+0000
Done in 0.819099903107 seconds

Additionally, when I open a Chrome tab and use the Spotify Web Player, it does show up as a ‘device’ and I can control it using the widget…

Hope you can help!

can you share your rule for the update?
is it correct that the progress bar is not updating every second - only on a refresh?

Hi Andrew,

The status only updates when you execute the .py script again.
I’ve been running mine every 10 seconds for a couple of weeks without issue.

On email, please IGNORE formatting for now

var Timer spotifyTimer = null
rule "Monitor Spotify and move from Echo"
when Item Track_Spotify changed from OFF to ON then
if (spotifyTimer == null)
{
logInfo(“spotifyTimer”, “Schedule timer”)
spotifyTimer = createTimer(now.plusSeconds(10))
[|
// Run your code here
if (Track_Spotify.state == ON)
{

// logInfo(“spotifyTimer”, “Update spotify data and Reschedule timer - Track_Spotify Switch is still ON”)
var resp = executeCommandLine("/usr/bin/python /etc/openhab2/scripts/spotify.py",5000)
if(spotify_current_device.state == “EchoGround” || spotify_current_device.state == “Speakers”){
if(spotify_current_playing_explicit.state == ON){
var resp =1 executeCommandLine("/usr/bin/python /etc/openhab2/scripts/spotify.py next", 5000)
}
if(spotify_current_playing.state == ON){
if(TXNR636GROUND_Zone1_Power.state != ON ){
sendCommand(TXNR636GROUND_Zone1_Power, ON)
sendCommand(TXNR636GROUND_Zone1_Volume, 30)
sendCommand(TXNR636GROUND_Zone2_Power, ON)
sendCommand(TXNR636GROUND_Zone2_Volume, 30)
sendCommand(TXNR636_Zone1_InputSource, 01)
}
if(spotify_current_device.state == “EchoGround”){
logInfo(“WARNING”,“Spotify running on Alexa - yuck . . .move house: " + spotify_current_device)
sendNotification("foo@bar.com”,“Yuck - Spotify on Alexa - migrate!”)
sendCommand(spotify_current_device_id, “81d0483c678992da4554903fc16af85aaaaaaaa”);
var resp = executeCommandLine("/usr/bin/python /etc/openhab2/scripts/spotify.py transfer_playback",5000)
}
}
}

spotifyTimer.reschedule(now.plusSeconds(10))
}
]
}
else
{
// this should not be possible, Sample Rule2 should
prevent this
logInfo(“spotifyTimer”, “Reschedule timer”)
spotifyTimer.reschedule(now.plusSeconds(10))
}
end rule “Monitor Spotify and move from Echo cleanup” when Item
Track_Spotify changed from ON to OFF then
if(spotifyTimer != null)
{
sendNotification("foo@bar.com", “Openhab Spotify Scheduler switched OFF!”)
logInfo(“spotifyTimer”, “Cancel timer”)
spotifyTimer.cancel
spotifyTimer = null
}
end

Obviously edit to manage your AV gear etc.
Track_Spotify is a dummy switch to manage whether the rule runs, I’m about to change that so it only runs when my nest says I am home.

Let me know how you get on.

OK the only bit im not understanding is how does track_spotify change state in the first place?

It’s just a phantom switch to disable / enable the rule.
Map it to your home/away alert or just get rid of it entirely.

Initially I disabled and re-enabled it when I was testing the rule, but now it’s always on

Sorry it took a while to respond with the forum being down.

I also noticed this problem with the API, a simple workaround is to open Spotify (e.g. on your phone) and click on devices. That somehow refreshes the list in the backend. If you call the API again, all devices show up.

Hope this helps!

Sound cool, I do have multiple spotify connect players in the house and would like to use Alexa for playing stuff on the various locations. I understand, presently we’re using python scripts to do the magic, so my question:

  • is it hard to make a binding out of this?

I can imagine there are many OH-users out there using spotify and alexa/google home and spotify for playing music?

@pmpkk Thanks for your reply!

However I tried your solution in the meantime and it doesn’t work for me unfortunately. When I play on one of my Sonos speakers through the official Sonos app on my phone it does show the track being played on my Sonos player in the widget but it doesn’t add the Sonos player to the list of devices (apparently it has device_id ‘NULL’).

I know this is a problem with the API, but I would assume more people are experiencing this problem or maybe found a fix or workaround? I will continue to trouble shoot and see if maybe the Spotify Web Client will work (with regard to your proposed workaround)

Edit: To clarify, I just played on all of my Sonos speakers through the Spotify app (which also correctly shows my devices). When I query the API endpoint for my devices I only get to see my webplayer.

{
  "devices" : [ {
    "id" : "13b1c36edd377e1c1920c35ae061df685316c313",
    "is_active" : false,
    "is_restricted" : false,
    "name" : "Web Player (Chrome)",
    "type" : "Computer",
    "volume_percent" : 100
  } ]
}

I also raised an issue with the Spotify web API on GitHub hoping that they might find a solution. (https://github.com/spotify/web-api/issues/671)

This is the response I got on the GitHub for the Spotify API.

Certain devices, particularly third party speakers and Sonos units, are not currently controllable through the Web API Connect endpoints. The /v1/me/player/devices endpoint will only return information for devices which are both active and controllable.

I thought I saw in one of your screenshots that you had a (Sonos?) Playbar connected through the Spotify widget, which is why I assumed it would work for Sonos devices. Does this mean no one has been able to control other devices through the Spotify widget other than their web clients?

I control my LG SoundBar and my Amazon Echo through the API and it works fine. I don’t understand why Sonos doesn’t support it (maybe a licensing issue).

Sorry! I hope they will support it soon.