Several years ago, I abandoned my locally stored MP3 collection for Spotify’s multi-device cloud solution. Unknown to me at the time, my switch would one day open an array of options for controlling playback and routing my music through a variety of devices. But why would I want to control my music through Home Assistant? The reason I personally wanted to was because of all those scenarios where you want to change the song… while cooking, doing dishes, showering, or one of many other times it’s just not practical, or desirable to pick up your phone. Enabling Home Assistant to control your music, also allows you to enable something else to, like in my case, a Flic – Smart Bluetooth Button.

To make all this happen, you’ll need a premium Spotify account and a handful of new automations and scripts for Home Assistant. In this post we’ll be integrating our Spotify with Home Assistant and writing the scripts. I’ll cover integrating Flic with Home Assistant and triggering the scripts in another post.

Before you try setting this up, make sure you have a Premium Spotify account. If you do not have a Premium account, Home Assistant will not be able to control your Spotify music (play/pause, next, previous, etc).

Creating a Spotify Application

To start, you’ll need to create a Spotify Application, which is not as hard as it sounds. To do so, you’ll need to…

  1. Log into the Spotify Developer website and go to the My Applications page.
  2. Select Create An App.
  3. Enter anything you want into Application Name and Application Description.
  4. Take note of the Client ID and Client Secret. You’ll need those for your configuration in Home Assistant.
  5. Select Edit Settings and add a Redirect URI.
    • No SSL: http://[your_home_assistant_url_or_local_ip]:[port]/api/spotify
    • If using SSL: https://[your_home_assistant_url_or_local_ip]:[port]/api/spotify
  6. Select Save and you’re done with the Spotify Developer site.

Configuring Home Assistant

Now that we have a Spotify Application, we can add the Spotify component to Home Assistant in configuration.yaml. This is where you’ll use the Client ID and Client Secret you noted when you created your Spotify Application. Once you’ve added the configuration, restart your Home Assistant server for it to recognize the new component. If you’re having trouble getting Home Assistant to communicate with Spotify, you may also need to set the  base_url  attribute of the HTTP Component.

A Couple Future Assumptions

This script is set up with the assumption that you’ll have players in multiple rooms and the simplest scenario is having a permanent device in each room that has Spotify Connect. For simplicity, we’ll assume each room has an Amazon Echo.

We also know that we will eventually be triggering these scripts using a Flic – Smart Bluetooth Button. Each button has the ability to trigger a different action on single press, double press and on hold. So we’ll be setting up three trigger points (play/pause, next track, previous track). In my setup, I’ve chosen to assign “play/pause” to “hold”, “next track” to “single press”, and “previous track” to “double press”.

Triggering the Actions

The scripts at the beginning of our chain are responsible for communicating which type of button press was received. To avoid later confusion, I’ve named each one according to the Flic button press they will later be linked to.

The first two scripts are pretty straight forward and directly call the services available in the media_player  component. The one bit of polish we’ve added here is a condition  to make sure Spotify is actually playing music. If it’s not, we discard the event. This keeps music from starting if someone accidentally presses the button. These scripts also do not call out a specific player, so that you’re able to skip tracks from a button in any room.

The third script, for triggering play/pause, has a bit more going on. It calls another script and passes in a variable letting the script.spotify_script_select_by_condition  know which device we’re targeting. Let’s get into that.

To Play, Pause, or Simply Change the Source

This script gets called from a hold, or long press, on the Flic button, signaling we are looking for a play/pause action. On a high level, all we are doing in this script is checking to see if Spotify is currently playing music and if that music is playing on the device in the room we pressed the button in and then triggering an action.

Now why would we care to know those things? Why aren’t we simply triggering the  media_play_pause  service in the same way we did for  media_next_track  and media_previous_track ?

I’ll tell you why we care. Say you’re in the kitchen making breakfast before work, listening to music. You finish up and it’s time to brush your teeth and anything else you do before you leave for work. Now you find yourself in the bathroom, but your music is still playing in the kitchen. That just won’t do, so you long press the Flic button in the bathroom to play your music in the same room you’re in, but all it does is pause. That’s annoying, and that’s why we need to know these things.

There are three potential outcomes based on the data we’re looking at:

  1. Music is already playing, but it is playing in a different room. This is when we want the music to continue playing without interruption and simply switch to the room we pressed the button in.
  2. Music isn’t playing, but Spotify’s most recent source was in a different room. This is when we want to change the source to the room we pressed the button in and start playing the music.
  3. Music is already playing and it is playing in the room we pressed the button in. This is when we want the music to stop playing.

For each of these three potential outcomes, we have a corresponding end-of-line script that triggers our final action. For each of these scripts, you’ll also notice we’re including a shuffle_set  service to ensure our music is always set to play on shuffle.

Testing the Setup

If everything is configured correctly, you should now be able to successfully test your setup by manually triggering the Flic Kitchen Single , Flic Kitchen Double , and  Flic Kitchen Hold  scripts in your Home Assistant UI.

2 Comments

  1. Janus Hansen February 19, 2019 at 6:31 am

    Hi Alexander

    can you share how you triggering the Flic Kitchen Single , Flic Kitchen Double , and Flic Kitchen Hold scripts in your Home Assistant UI?

    I think we need to include all the new .yalm files.. and i think you forgot to add the extension to “spotify_changing_source”

    Thanks in advance

    Reply
    1. Alexander James - Site Author February 19, 2019 at 9:38 am

      Hey Janus–

      In the HA side menu, there’s an <> icon at the bottom. On that page you’ll see all the entities in your system. Once you locate the script you want to manually trigger in that list, click the icon to the left of the entity’s name. In that dialog will be a button that says “EXECUTE”. Clicking that button will manually trigger your script.

      Reply

Leave A Comment

Your email address will not be published. Required fields are marked *