openHAB integration to Spotify Web Connect API (player)


(Jonas) #74

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?


(Lorenzo Giordano) #75

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


(Lorenzo Giordano) #76

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


(Andrew Pawelski) #77

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


(Alan Fenwick) #78

UPDATED - this works just great :slight_smile:


(Bas) #79

@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!


Custom Widget: Spotify Playlists for Spotify Binding
(Andrew Pawelski) #80

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


(Alan Fenwick) #81

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.


(Andrew Pawelski) #82

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


(Alan Fenwick) #83

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


(Patrick) #84

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!


(Thomas Binder) #85

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?


(Bas) #86

@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)


(Bas) #87

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?


(Patrick) #88

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.


(Alan Fenwick) #89

Thanks unparagoned,

I have been using your version of the Spotify.py script, since my Amazon echo device keeps changing ID - and therefore I had to remap the device_id each time.
Since python is a new dark art to me, I decided to debug as much as I can.

Your code has one minor bug though.
When attempting to using …/spotify.py play devicename
it bugs out if there are devices on the spotify network that have special chars

In other words, I was trying spotify.py play Speakers (and various variations thereon) but whenever my iPhone or wife’s iphone was online, it would bug out…since the default identifier is something like this

“Bob’s iPhone”

My lazy bodge for now was to rename my iPhone, but this will not help when someone else is on my household network with an iPhone, running Spotify.

Here’s the capture:

Match : Speakers  or  None index:  -1
Speakers
"Speakers"
1 Device :  SOCKMONKEY id : daa6bbe58a66fa94a6b61431aaad3489a339e1a6
2 Device :  Amazon FireTV Stick id : 35c7b98e202a04dddddef3e59f224df6233623dc
3 Device :   -> Failure:  <type 'exceptions.UnicodeEncodeError'>
-- Calling Service: Play device

When parsing the JSON, I could see that device 3 was:

{“name”: “Alan\u2019s iPhone”, “volume_percent”: null, “is_active”: false, “is_restricted”: false, “type”: “Smartphone”, “id”: “65176c3f445fa6ee9a2102c75555554001ca9f8”},

If you do update this at any point, please post the update.
Thanks


(Andrew Pawelski) #90

So should the detection of a song playing happen without any refresh to then fire the rule?


(Jonas) #91

Hi! I followed your tutorial on github and i got everything done. now i don’t know how to change values (for example just play/pause).
in the tut you write that this is done with the python script. can you please explain that a little bit for me? i can see the items in the habpanel but i created a button and mapped play/pause spotify current playing to it (seems like it’s only a read variable) so it does not work.
what do i to do need if i want to control things?

thanks a lot!

addition:
i successfully created a dummy label that shows me the current track.
it does show the last track played when i did run the python script in the terminal.
how can i execute the script from a rule? is that the way it should be working? :wink:
and at last of course: how can i actually control stuff. Thanks! :slight_smile:


(Andrew Pawelski) #92

So it looks like there is no detection of when Spotify starts playing - are people just running the script every 10 seconds to capture changes in state?


(sceppi) #93

I’m having the same issue. When I play from a device, the label and the title shows up fine on my HabPanel. I can see which track is playing, and also the progress bar. Only: I cannot control thingsfrom Habpanel. For example:

  • When I change my volume from 100 to 50 in habpanel nothing happens
  • However when I change the volume in spotify itself for example from 100 to 50 and then press update in HabPane, then the volume is updated from 100 to 50.

–> So it retrieves the information correctly from spotify, but I cannot control or send actions from HabPanel