Spotify is the renowned music streaming service that needs no introduction. Therefore you will not be surprised to learn that there is a Spotify integration within Home Assistant.
In this tutorial we will learn how to set up the Spotify integration in Home Assistant. The integration is very easy to implement with a few options accessible on the Spotify developer page.
You can use a free Spotify account but the control in Home Assistant will be very limited. Ideally you need a premium account.
You should have Home Assistant installed and running and a basic understanding of YAML including how to edit configuration.yaml. You should also understand how to use a service and entity within Home Assistant.
If you don’t yet have a Spotify account setup, head over to the site and setup a free account. You can get things up and running with the free account, however you will likely want to upgrade to their premium offering for full functionality to get the most out of Home Assistant.
Set up Spotify
Firstly we need to set up Spotify so that we can connect Home Assistant. This is done by creating an app on the Spotify developer page.
The process is very simple and completely free, so no need to panic! Just head over to the Spotify developer dashboard and log in.
Create Spotify developer application
First we need to click on create client ID and fill out the form. The app name and description doesn’t matter so much, I just chose the name of my Home Assistant server.
You can choose “I don’t know” for the application type and ignore the warning about commercial apps as we aren’t building one.
Click next and then go ahead and accept all of the terms and conditions on the following page and click submit.
Copy the client ID
Once done you will be taken to a page that confirms that your app has successfully been created.
We need the Client ID and Client Secret (click show client secret), so go ahead and copy and paste it to a text file. We will need the codes later during the Home Assistant setup.
Add the URI
Click on the edit settings button and scroll down the page to the redirect URIs section.
You will need to enter the address you use to access your Home Assistant server followed by /auth/external/callback
You can use your IP address or DuckDNS address. Don’t forget to include the port and either HTTP or HTTPS depending on whether or not you are using SSL. My URI using SSL is as follows.
Click on add and the URI and you should see it added to the list. Scroll to the bottom of the menu and click save. That’s it! Now we can move on to configure Home Assistant.
Configure Home Assistant
Now that our Spotify account is ready we can set up Home Assistant. First we need to add the integration to the configuration.yaml file.
Add the Spotify integration
Add the spotify integration and specify the client ID and client secret that you made a note of earlier, or just copy and paste them from the developer page.
spotify: client_id: 2ef****************************** client_secret: a4f******************************
Once you have added the code to configuration.yaml go ahead and save it, then restart Home Assistant.
Add HTTP integration
If you are using an external address (usually with DuckDNS) then you will need to set the base_url attribute under the http integration. Simply add it to your configuration.yaml file.
http: base_url: https://<your-user-id>.duckdns.org:8123
If you are using HTTPS then you will probably have already configured this option.
Activate the Spotify integration
Next we will need to activate the Spotify integration. Click on configuration from the side bar and navigate to integrations.
Click on the orange circle with the plus icon in the lower right-hand corner to add a new integration, then search for Spotify.
When you add the integration you will be taken to a page where you can authorise access to your Spotify account. Following authorisation you will be given a success message.
If you receive the error message INVALID_CLIENT: Invalid redirect URI then go back to Spotify developer and double-check that the URI is correct. Remember to include the port and specify HTTP or HTTPS correctly.
Test the Spotify integration
Once you have completed the integration, a media player entity should be added to your overview page. You can go ahead and give it a try!
You can add the card manually by editing the UI and adding an entity card. Simply specify the mediaplayer.<your-spotify> as the entity. You can also move the card to another tab when you edit it in the UI editor.
In order to get the most out of the Spotify integration we are going to want to automate it. It allows wonderful things to occur, such as Home Assistant automatically playing your favorite playlist when you arrive home.
In order to demonstrate the basic functionality, we can create a script that will automatically load and play your favorite playlist.
First we need to create a new script in the configuration.yaml file. If you already have the script integration, simply add this code beneath the existing code and omit the instance of the script integration.
We will create a script called spotify_play_playlist, but you can name however you prefer.
spotify_play_playlist: alias: Spotify Play description: 'Play playlist in Spotify'
Next we will add the media_player.play_media service into the sequence. Note that in order for it work work we need to use the “medial_player.play_media” and not the “medial_player.media_play” service.
spotify_play_playlist: alias: Spotify Play description: 'Play playlist in Spotify' sequence: - alias: Spotify Play service: media_player.play_media
Finally we need to add the data. We need to specify the entity_id as our Spotify media player. The content type needs to be set as playlist and we need to fetch the Spotify user and playlist code for media_content_id.
spotify_play: alias: Spotify Play description: 'Play playlist in Spotify' sequence: - alias: Spotify Play service: media_player.play_media data: entity_id: media_player.spotify_siytek media_content_type: playlist media_content_id: '<spotify-URI>'
We can replace <spotify-URI> with the URI for the playlist that we wish to play. Simply navigate to the playlist and click the three dots. There you will find an option to copy the URI so that you can paste it into the script. Don’t forget to add the ‘quotes.’
Once you have finished entering the code, save the file and restart Home Assistant. We can then give the script a test run!
Simply follow the steps in my script tutorial to add an entity button using the script we have just created for Spotify as the entity, script.spotify_play.
Streaming media has revolutionized the way we consume content and Spotify is at the forefront of streaming music services.
In this tutorial we have learnt how to link a Spotify account to Home Assistant and use a basic script to control it.
Why not check out my movie lights tutorial next and learn how to synchronize your lights with Spotify, and any other media player!