Skip to content

pmpkk/openhab_spotify-webconnect-api

Repository files navigation

openhab_spotify-webconnect-api

A simple Python script to integrate Spotify's Web Connect API (https://developer.spotify.com/web-api/web-api-connect-endpoint-reference/) to OpenHab (openhab.org)

Prerequisite

  • 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/

Instructions

  1. Add the spotify.items file into openhab-conf/items and verify they have been added to your setup in Paper UI
  1. Place the following files in the folders:
  • matrix-theme.css into openhab-conf/html/matrix-theme
  • AppIcons.svg into openhab-conf/html/matrix-theme
  • spotify-auth.html into openhab-conf/html
  • myopenhab.py into openhab-conf/scripts
  • spotify.py into openhab-conf/scripts
  • spotify.rules into openhab-conf/rules
  • spotify.items into openhab-conf/items
  1. Verify you can reach spotify-auth.html
  1. After registering as a Spotify developer, create a new App and retrieve client id and secret:
  1. 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)
  1. Authenticate your New App for your Spotify User
  • Go to: http://openhabianpi.local:8080/static/spotify-auth.html
  • You should see a page called "Spotify Integration" with a status message "Click here to Authenticate"
  • Click the link and authenticate your app
  • When done, the page returns and displays "New Auth Code successfully saved to OpenHab!"
  • Check that the spotify_auth_code is set in OpenHab
  1. Test spotify.py in SSH
  • Set the REDIRECT_URI in spotify.py to the right value
  • Run this in terminal /usr/bin/python /etc/openhab2/scripts/spotify.py

You should see this:

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
Successfully posted state to OpenHab: spotify_current_track
Successfully posted state to OpenHab: spotify_current_artist
Successfully posted state to OpenHab: spotify_current_cover
Successfully posted state to OpenHab: spotify_current_duration
Successfully posted state to OpenHab: spotify_current_progress
Successfully posted state to OpenHab: spotify_current_progress
Successfully posted state to OpenHab: spotify_current_playing
Successfully posted state to OpenHab: spotify_current_device
Error posting state to OpenHab: spotify_current_volume (HTTP Response 400)
Successfully posted state to OpenHab: spotify_current_device_id
 -> Success
Successfully posted state to OpenHab: spotify_lastConnectionDateTime
Done in 0.761183977127 seconds
  1. You can now hook up OpenHab rules or HabPanel with the data in the spotify items.

Use

  • spotify.py
  • parameters:
    • none = refresh data
    • play = resume playing
    • play uri = play suported uris (album, artist or playlist), e.g. spotify:user:spotify:playlist:37i9dQZF1DX5OepaGriAIm
    • pause = pause
    • next = next track
    • previous = previous track
    • volume_up = 10% up
    • volume_down = 10% down
    • get_devices = get available devices and safe to openhab
    • transfer_playback = transfer playback to device set in openhab

About

A simple Python script to integrate Spotify's Web Connect API (https://developer.spotify.com/web-api/web-api-connect-endpoint-reference/) to OpenHab (openhab.org)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published