openHAB integration to Spotify Web Connect API (player)


(Unparagoned) #226

Google Home essentially sleeps or turns off Spotify after a while. There is no way via the Spotify to play on as device which isn’t running Spotify. So you need to send a command to google home to turn on Spotify first.
I don’t know if google has an api that lets you do that. The alternative is trying to send keep alive commands over the Spotify api, so google doesn’t kill Spotify.


(David Verbeeck) #227

When hitting auth i get this; INVALID_CLIENT: Invalid client
anyone know why this is?

EDIT: never mind, created a new app in spotify dev and replaced id/secret + reboot did it


(David Verbeeck) #229

Persistance is working in my setup ( loggin temperatures from hue motion sensor) but not for this…


    ==> /var/log/openhab2/openhab.log <==
    2018-03-19 15:44:54.556 [ERROR] [sql.internal.MysqlPersistenceService] - mySQL: Could not create table for item 'spotify_auth_code' with statement 'CREATE TABLE Item9 (Time DATETIME, Value VARCHAR(20000), PRIMARY KEY(Time));': Column length too big for column 'Value' (max = 16383); use BLOB or TEXT instead
    2018-03-19 15:44:54.561 [ERROR] [sql.internal.MysqlPersistenceService] - mySQL: Item 'spotify_auth_code' was not added to the table - removing index
    2018-03-19 15:44:54.584 [ERROR] [sql.internal.MysqlPersistenceService] - mySQL: Could not store item 'spotify_auth_code' in database with statement 'INSERT INTO Item9 (TIME, VALUE) VALUES(?,?) ON DUPLICATE KEY UPDATE VALUE=?;': Table 'OpenHAB.Item9' doesn't exist

ANyone got an idea?
screenshot of database


(Paul Neumeyer) #230

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 :frowning:
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?


(Patrik Patrik) #231

I use an Alexa dot to play spotify on. However, I have noticed that the spotify_current_device_id for the alexa dot changes over time - resulting in that I have to change the id in the code in order for spotify to work. Is there some way to automatically read the spotify_current_device_id into a variable and replace the id number with the variable .
spotify_current_device_id.sendCommand(id_Alexa_Dot)


(Clinton De Goede) #232

Thank you, Spotify integration is great.

I can get Spotify working correctly but I cannot seem to get a device list below is the response I get when I run the Spotify.py file it doesn’t seem to get the list of devices.

[22:26:32] openhabian@openHABianPi:~$ sudo /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
{u'progress_ms': 22927, u'timestamp': 1525343295096L, u'repeat_state': u'off', u'shuffle_state': False, u'item': {u'album': {u'                                                                                                              album_type': u'album', u'name': u'Illuminate (Deluxe)', u'external_urls': {u'spotify': u'https://open.spotify.com/album/2AJaA0i                                                                                                              s5qbaqFy77tPl7B'}, u'release_date': u'2016-09-23', u'uri': u'spotify:album:2AJaA0is5qbaqFy77tPl7B', u'href': u'https://api.spot                                                                                                              ify.com/v1/albums/2AJaA0is5qbaqFy77tPl7B', u'artists': [{u'name': u'Shawn Mendes', u'external_urls': {u'spotify': u'https://ope                                                                                                              n.spotify.com/artist/7n2wHs1TKAczGzO7Dd2rGr'}, u'uri': u'spotify:artist:7n2wHs1TKAczGzO7Dd2rGr', u'href': u'https://api.spotify                                                                                                              .com/v1/artists/7n2wHs1TKAczGzO7Dd2rGr', u'type': u'artist', u'id': u'7n2wHs1TKAczGzO7Dd2rGr'}], u'images': [{u'url': u'https:/                                                                                                              /i.scdn.co/image/0c1fe77b4d329bebc8c8e17cc448101a966e1307', u'width': 640, u'height': 640}, {u'url': u'https://i.scdn.co/image/                                                                                                              8d4845f9e802d35c6229e268360af91286ad384c', u'width': 300, u'height': 300}, {u'url': u'https://i.scdn.co/image/ed959ddbc275cbd4b                                                                                                              cf9a7c77140779e0714b5b4', u'width': 64, u'height': 64}], u'type': u'album', u'id': u'2AJaA0is5qbaqFy77tPl7B', u'available_marke                                                                                                              ts': [], u'release_date_precision': u'day'}, u'is_local': False, u'name': u'Mercy', u'uri': u'spotify:track:7evPpWWu01Klxi0KlPx                                                                                                              R17', u'external_urls': {u'spotify': u'https://open.spotify.com/track/7evPpWWu01Klxi0KlPxR17'}, u'popularity': 25, u'explicit':                                                                                                               False, u'preview_url': None, u'track_number': 2, u'disc_number': 1, u'href': u'https://api.spotify.com/v1/tracks/7evPpWWu01Klx                                                                                                              i0KlPxR17', u'artists': [{u'name': u'Shawn Mendes', u'external_urls': {u'spotify': u'https://open.spotify.com/artist/7n2wHs1TKA                                                                                                              czGzO7Dd2rGr'}, u'uri': u'spotify:artist:7n2wHs1TKAczGzO7Dd2rGr', u'href': u'https://api.spotify.com/v1/artists/7n2wHs1TKAczGzO                                                                                                              7Dd2rGr', u'type': u'artist', u'id': u'7n2wHs1TKAczGzO7Dd2rGr'}], u'duration_ms': 208733, u'external_ids': {u'isrc': u'USUM7160                                                                                                              3531'}, u'type': u'track', u'id': u'7evPpWWu01Klxi0KlPxR17', u'available_markets': []}, u'context': {u'href': u'https://api.spo                                                                                                              tify.com/v1/users/spotify/playlists/37i9dQZF1DWTwnEm1IYyoj', u'type': u'playlist', u'uri': u'spotify:user:spotify:playlist:37i9                                                                                                              dQZF1DWTwnEm1IYyoj', u'external_urls': {u'spotify': u'https://open.spotify.com/user/spotify/playlist/37i9dQZF1DWTwnEm1IYyoj'}},                                                                                                               u'device': {u'name': u'Dining room speaker', u'volume_percent': None, u'is_active': True, u'is_restricted': False, u'type': u'                                                                                                              CastAudio', u'id': u'505ffdaa7e9e4dad9e78d132b8060d3f'}, u'is_playing': True}
Successfully posted state to OpenHab: spotify_current_track = Mercy
Successfully posted state to OpenHab: spotify_current_artist = Shawn Mendes
Successfully posted state to OpenHab: spotify_current_cover = https://i.scdn.co/image/8d4845f9e802d35c6229e268360af91286ad384c
Successfully posted state to OpenHab: spotify_current_duration = 208733
Successfully posted state to OpenHab: spotify_current_progress = 22927
Successfully posted state to OpenHab: spotify_current_playing = ON
Successfully posted state to OpenHab: spotify_current_device = Dining room speaker
Successfully posted state to OpenHab: spotify_current_volume = NULL
Successfully posted state to OpenHab: spotify_current_context_uri = spotify:user:spotify:playlist:37i9dQZF1DWTwnEm1IYyoj
Successfully posted state to OpenHab: spotify_current_device_id = 505ffdaa7e9e4dad9e78d132b8060d3f
Successfully posted state to OpenHab: spotify_current_progress_percent = 10.98
 -> Success
Successfully posted state to OpenHab: spotify_lastConnectionDateTime = 2018-05-03T10:28:29.00+0000
Done in 0.742801189423 seconds


(James) #233

That fixed my issue too, thank you!

now have fully working script, now to get the UI sorted :slight_smile:


(Jonas) #234

hi!

3 days ago the item spotify_current_progress_percent stopped working.
did spotify change anything?
my item is at 0.0 although spotify is playing.
cover, track and the other values are displayed correctly … i did not make any changes on the script …

another question: is it possible to tell spotify on which device to play or is this item only for displaying the current playing device?

Thanks!


(Unparagoned) #235

In my branch I’ve got it working based on names. It’s kind of fuzzy so you don’t even have to be super accurate. It would be something like spotify.py play livingroon echo Since the name of the device doesn’t change it would use that to lookup the id and then play that. I designed it that way so I could use voice commands to change device.


(James) #236

Hi all, which persistent service do i need to have running to be able to keep the api key and secret current, currently have mqtt & rrd.

thanks


(Szabi) #237

Hi,
It works great.
Now I can see the actual device, such as myphone or web player.
But how can I set my RPi as device?
Thx in advance.


(Andrew Pawelski) #238

I was struggling with this error as well - this worked for me as well!!!


(Szabi) #239

Hi,

  1. I made a sketch about my idea.
    spotify

I can not set any device, because I get this:

 /usr/bin/python /etc/openhab2/scripts/spotify.py get_devices
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: Get Devices
Successfully posted state to OpenHab: spotify_device_list = []
[]
Successfully posted state to OpenHab: spotify_lastConnectionDateTime = 2018-05-29T09:05:42+0000
Done in 0.751863956451 seconds

I would like to set my speaker (with RPi) as a device.

  1. And the “next” parameter produces this:
Successfully posted state to OpenHab: spotify_token_issued = 2018-05-29T11:31:45
-- Calling Service: Next
403
-- Calling Service: Update

Please, can anyone help me?


(Szabi) #240

I have same problem. Did you solve the “403” problem?


(Paul Neumeyer) #241

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?


(Szabi) #242

I have same problem


(Pavel) #243

Good day.

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


(Kris K) #244

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


(Jonas) #245

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


(Szabi) #246

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



Matrix Theme for HABPanel