openHAB integration to Spotify Web Connect API (player)

Hey, i have a problem with the testing part.
I get this back:
[23:37:38] openhabian@openHABianPi:~$ /usr/bin/python /etc/openhab2/scripts/spotify.py play
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
{‘Content-Length’: ‘69’, ‘Keep-Alive’: ‘timeout=600’, ‘Server’: ‘nginx’, ‘Connection’: ‘keep-alive’, ‘Date’: ‘Sun, 25 Mar 2018 21:39:11 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’: ‘Sun, 25 Mar 2018 21:39:11 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
Error posting state to OpenHab: spotify_lastConnectionDateTime (HTTP Response State could not be parsed: 2018-03-25T21:39:12+0000)
Done in 1.24915194511 seconds
can anyone help me?

I have same problem

Good day.

Can this integration allow playback of music not in OpenHAB machine itself, but in other output device like Yamaha AVR?

It can Pavel, there is a ‘devices’ option that will allow you to select Spotify connect devices on your LAN - such as the Spotify connect option on the Yamaha AVR or even use Airplay is how I understand it

See the ‘device’ on patricks dashboard.

See this links on ‘devices’ - https://developer.spotify.com/documentation/web-api/guides/using-connect-web-api/

The Yamaha will turn up under devices as both an Airplay device or a Spotify connect device once the AVR is setup in OH2 (with the Yamaha binding id imagine?)

I got some problems …
I set it up and get a positive authentication feedback.
Persistence is working as well.
when i run the python cript in the terminal everythign works but i created a rule to call it every second (for testing now) and i get errors (every second :wink: so the rule fires):

Path/spotify.py: line 6: import: command not found
Path/spotify.py: line 7: import: command not found
Path/spotify.py: line 8: import: command not found
Path/spotify.py: line 9: import: command not found
from: can't read /var/mail/myopenhab
from: can't read /var/mail/myopenhab
from: can't read /var/mail/myopenhab
Path/spotify.py: line 16: ACCOUNT_URL: command not found
Path/spotify.py: line 17: API_ROOT_URL: command not found
Path/spotify.py: line 18: REDIRECT_URI: command not found
Path/spotify.py: line 19: VOL_INCREMENT: command not found
Path/spotify.py: line 21: syntax error near unexpected token `('
Path/spotify.py: line 21: `class spotify(object):'' does not exist.
2018-06-08 12:31:58.313 [WARN ] [rthome.model.script.actions.BusEvent] - Item 'Path/spotify.py: line 5: import: command not found

Thank you for the tips and codes.
Some picture about my solution:


1 Like

The fix to my issue:

You have to make the python script executable - otherwhile the OS will open the script in the terminal and produce the errors i posted above (OS X):

sudo chmod +x file-name.py

After this add

#!/usr/bin/env python2.7

to the top of the python script! (Yes, with # )

:slight_smile:

I’m pretty sure it’s written in python 2. So when you are running it from terminal it’s probably running python2.7, but when you run it from the rule its python 3.

Have a look at your rule. You should be able to specify what version of python. The following works for me

executeCommandLine("/usr/bin/python /etc/openhab2/scripts/spotify.py " + receivedCommand.toString, 500)

But you could specify the version of python

executeCommandLine("/usr/bin/python2.7 /etc/openhab2/scripts/spotify.py " + receivedCommand.toString, 500)
1 Like

oh nice, i’ll try that!

Yeah i installed python 3 the other day (while dealing with grafana) and now i think the highest version of python is used.

I’ll try your answer :slight_smile:

EDIT: Your solution works as well!
So it does not matter where you specify the environment. either the rule that calls the script or the script itself.

Hello,

The design looks really good. Currently thinking about the theme to go and use.
But for now I would like to get the Spotify working without this theme.

I get NULL values.
The connection with Spotify seems not to work.
The Authentication was succesfull though.
Running the spotify.py is giving me errors.

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
{'Content-Length': '70', 'Keep-Alive': 'timeout=600', 'Server': 'nginx', 'Connection': 'keep-alive', 'Date': 'Mon, 11 Jun 2018 17:29:02 GMT', 'Content-Type': 'application/json'}
{u'error_description': u'Invalid client secret', u'error': u'invalid_client'}
Successfully got state from OpenHab: spotify_auth_code
-- Calling Token Service for the first time
{'Content-Length': '70', 'Keep-Alive': 'timeout=600', 'Server': 'nginx', 'Connection': 'keep-alive', 'Date': 'Mon, 11 Jun 2018 17:29:02 GMT', 'Content-Type': 'application/json'}
{u'error_description': u'Invalid client secret', u'error': u'invalid_client'}
-- Calling Service: Update
{u'error': {u'status': 401, u'message': u'Invalid access token'}}
 -> Item node missing from response :(
Successfully posted state to OpenHab: spotify_lastConnectionDateTime = 2018-06-11T17:29:02+0000
Done in 0.625561952591 seconds

Does anyone have any Idea whats going wrong here?

Regards, Edwin

Can anybody provide dashboard from head or similar? It can be exported as json in settings.

Does anyone know if under devices it would list an airplay device on my lan if the habpanel device is an android ? It lists the spotify connect device ok.

Hi!

Is there a way to run the script every second ONLY when spotify is playing?
Maybe we can build a rule that fires every 30 sec to check if it’s playing and if so change the interval to 1 s?
I also don’t get the part of the rule:


rule "Spotify Action"
when
		Item spotify_action received update
	then		
			val resp =  executeCommandLine("/usr/bin/python2.7 /scripts/spotify.py " + spotify_action.state.toString, 5000)
			logInfo("Spotify", resp)
end

what’s that supposed to do?
The first part runs the script, when i push a button …

Thank you!

Any thoughts as to why Playlist, Devices or Volume dont work? Play/Stop work fine.

Devices doesnt update unless I use the Spotify app to actually play something in it, then the device is visible in OH2. It doesnt show a ‘list’ of devices on my LAN, only one device thats playing.

Album art, artist and song all work

@Dries I have the same issue with devices .

I have a LINN DS player on my LAN, it has proper spotfiy connect intergration but only shows in HABPanel when i click play or use it in the Official App

Did you sort this out?

Ideally I should get a list of devices I can choose but I dont

@tr1cks relevant for you

Unfortunately not

Hi!

I tried to keep lists as json in String item. It working well, but only when I open page first time. After refresh they vanish. May be it’s related to ng-init. It’s hard to say for me, because I am almost not familiar with Angular. What can be wrong?

<div class="widget">
    <div class="icon off">
        <svg viewBox="0 0 48 48">
            <use xlink:href="/static/matrix-theme/squidink.svg#box"></use>
        </svg>
    </div>
    <div class="name">Devices</div>
    <div class="sceneGroup" ng-init="deviceList = $eval(itemValue('spotify_device_list'))">
        <div class="scene" ng-class="{true:'on', false:''}[itemValue('spotify_current_device_id')==device.id]"
             ng-repeat="device in deviceList"
             ng-click="sendCmd('spotify_current_device_id', device.id);sendCmd('spotify_action', 'transfer_playback')">
            {{device.name}}
        </div>
    </div>
</div>

I had some issues with the devices and playlists not updating. My current setup works well but it’s a branch and I think the way devices is different to the main branch so you may need to adapt it. There is a refresh button which updates devices, and then I make sure the interface has the latest set of devices with a mouseover function.

Note: The way devices works may be different in the main branch. 

      <div class="widget" ng-init="devices=$eval(itemValue('spotify_devices')).devices">
        <div class="icon off" ng-click="sendCmd('spotify_device_update', 'ON'); devices=$eval(itemValue('spotify_devices')).devices"><svg viewBox="0 0 48 48"><use xlink:href="/static/matrix-theme/squidink.svg#double-arrow"></use></svg></div>
        <div class="nameGroup"><div class="name">Devices</div></div>
        <div class="btn-group" dropdown-append-to-body="true" uib-dropdown >            
          <button id="single-button" type="button" class="btn btn-primary" uib-dropdown-toggle  ng-click="devices=$eval(itemValue('spotify_devices')).devices" ng-mouseover="devices=$eval(itemValue('spotify_devices')).devices">
            {{itemValue('spotify_current_device')}} <span class="caret"></span>
          </button>
          <ul class="mydropdown-menu" uib-dropdown-menu role="menu" style="overflow: auto; max-height:300px; overflow-x:hidden" aria-labelledby="single-button" >   
            <div ng-repeat="dev in devices" ng-if="dev.state!='NULL'" >
              <li role="menuitem"><a ng-click='sendCmd("spotify_action", "play \"" + dev.id + "\"")'>{{dev.name}}</a></li>
            </div>
          </ul>
        </div>
      </div>

How does that look visiually @unparagoned

It’s a circle arrow for refresh and a drop-down to select devices. There is a picture here Matrix Theme for HABPanel