Control Hue Smart Plug using Spotify
This post shows a small Bash script that switches a Hue smart plug on while Spotify plays on a Raspberry Pi and off when it stops. It is for anyone who wants an old stereo to follow their Spotify playback without modifying it.
How to make a dumb stereo smart?#
My stereo is connected to a Hue smart plug, so I can switch it on and off remotely. Music comes from a Raspberry Pi running Raspotify. The plug also saves power if I fall asleep listening to an audiobook at night. A simple timer would solve that, but I want the stereo to be on only when I am actually using it.
No modifications thanks to a smart plug#
To do that, the Raspberry Pi has to detect whether librespot, the software behind Raspotify, has an ALSA sound device open and is therefore playing music. If it is, the plug is switched on; otherwise, it is switched off.
librespot runs permanently as a daemon in the background, so the first step is to find its PID:
pgrep librespot
The second step is the tricky one. We collect the PIDs of all processes that currently have an ALSA sound device open. A PID can appear more than once if a process uses several sound cards (for example HDMI and the audio jack), so the list is de-duplicated. If the librespot PID is in this list, librespot is playing music through a sound card.
sudo lsof +D /dev -F rt | awk '/^p/ {pid=$1} /^t/ {type=$1} /^r0x(74|e)..$/ && type == "tCHR" {print pid}' | cut -c 2- | sort -u
The following script runs on the Raspberry Pi with Raspotify. It checks the state of the Hue smart plug and switches it on or off when needed. My plug is light ID 38; the Hue developer documentation explains how to get an API key (the “username”).
The script uses the original Hue API (v1, /api/<key>/lights).
Signify’s newer API v2 (/clip/v2/) is where new features go, and according to the openHAB documentation, Signify has said that v1 will eventually be removed.
#!/bin/bash
apikey="********"
ip="hue.zaage.it"
light=38
hue_put() {
curl -ks -X PUT -H 'Content-Type: application/json' -d "$1" \
"http://$ip/api/$apikey/lights/$light/state" >/dev/null
}
plug_state="false"
while true; do
alsa_pids=$(sudo lsof +D /dev -F rt | awk '/^p/ {pid=$1} /^t/ {type=$1} /^r0x(74|e)..$/ && type == "tCHR" {print pid}' | cut -c 2- | sort -u)
spot_pid=$(pgrep -o librespot)
# OSRAM Lightify plugs don't report their state reliably;
# comment out the next line if you use one:
plug_state=$(curl -ks "http://$ip/api/$apikey/lights/$light" | jq '.state.on')
if [[ -n "$spot_pid" ]] && grep -qx "$spot_pid" <<< "$alsa_pids"; then
if [[ $plug_state == "false" ]]; then
hue_put '{ "on": true }'
plug_state="true"
fi
elif [[ $plug_state == "true" ]]; then
hue_put '{ "on": false }'
plug_state="false"
fi
sleep 1
done
Result#
The stereo now switches on within about a second of starting playback in Spotify and switches off when playback stops, without any change to the stereo itself.
Sources#
- Raspotify: https://dtcooper.github.io/raspotify/
- librespot: https://github.com/librespot-org/librespot
- Philips Hue developer documentation, getting started (API key): https://developers.meethue.com/develop/get-started-2/
- Philips Hue API v2: https://developers.meethue.com/develop/hue-api-v2/
- openHAB Hue binding, on API v1 and v2: https://www.openhab.org/addons/bindings/hue/