While I was setting up my local instance of Home Assistant, I discovered a really awesome media component for my Plex media server. Since I thought this Home Assistant component was pretty awesome, I searched high and low for similar Spotify capability. What I found out was that Spotify does not support getting current track information through their web API, so no one bothered creating a component. So this post is the first part of a three part quest of what I set out to do…

  1. Develop a Node module to control a local client.
  2. Setup a headless Spotify server.
  3. Create a Home Assistant Spotify media component.

Discovery Work

As I explored Spotify's Web API, I could not find a single mention or endpoint that provided current track metadata. I continued to explore the documentation for about twenty minutes until I started to Google. What I found was an issue was raised on Spotify's github account back in March of 2015 regarding the matter. This is what the team had to say:

yes, we'd like to expose this in the Web API. How and when is uncertain and I urge you not to expect this feature being available in the short term.

I am not one to stop at such a finding, so I thought to myself someone else has had to run into this problem in the past. As I continued to scour the web for a solution, I stumbled across an interesting article regarding Spotify clients implementing a local HTTP server. I suggest you read Carl Byström's article about Deconstructing Spotify's built-in HTTP server as it is very interesting.

Anyway, loaded with this information I decided the answer to my pursuit of creating a Home Assistant Spotify media component was to create a headless Spotify client and write a node module to communicate with it. This would then expose the necessary metadata and hooks to Home Assistant.

The Module

So if you read the article by Carl Byström, you will have read that he developed a python library to interact with the local client HTTP server. Since I love JavaScript, I decided to port the python library and create a node module dubbed spotify-locally. It is a nice lightweight module that works on both Windows and Linux (Maybe OSX).

Installing

npm install spotify-locally

Simple Example

const SpotifyLocally = require('spotify-locally');

let spotify = new SpotifyLocally();

spotify.getStatus().then((status)=>{
    console.log(status); // Retrieve current track information
}).catch((err)=>{
    console.log(err);
});

Module Methods

This module exposes the SpotifyWebHelper object, which exposes the following methods:

  • getStatus - get current status information (name of song/artist which is currently playing, etc..)
  • pause - pause currently playing song
  • unpause - unpause currently playing song
  • play - play the given spotify url
  • Constructor - Creates a new Spotify Service object, default port to communicate with the SpotifyWebHelper is 4370, other ports can be specified when creating the object.

That takes care of interacting with the local HTTP server using my module.