If anyone is interested, I created an squeezebox_alert addon for Hass.io. This was necessary due to shell_commands not getting executed correctly in Hass.io. This addon supports playing mp3 files stored locally on your LMS server and also playing audio generated using the TTS component. See below for instructions to set it up. Note, I have only tested this on Hass.io running on a Pi 3. Note, I used the Samba addon to create the files and sometimes had issues where a “^M” character appeared at the end of each line in the file. I used the SSH addon, edited the file using nano and pasted in the text directly to fix the issue. Reference https://home-assistant.io/developers/hassio/addon_tutorial/ for creating the add-on… it is actually very straight-forward.
-
With Samba addon configured, open HASSIO network share. Create a new folder called “squeezebox_alert” in the addons folder. Create the following 4 files in the squeezebox_alert folder:
Dockerfile
config.json
run.sh
squeezebox_alert.sh
-
Paste the contents below into each of the 4 files.
Dockerfile:
ARG BUILD_FROM
FROM $BUILD_FROM
# Add env
ENV LANG C.UTF-8
# Install requirements for add-on
RUN apk add --no-cache jq curl
# Copy data for add-on
COPY squeezebox_alert.sh /
RUN chmod a+x /squeezebox_alert.sh
COPY run.sh /
RUN chmod a+x /run.sh
CMD [ "/run.sh" ]
config.json (NOTE: Update json_rpc below. This can also be done in the addon Options in the web interface)
{
"name": "Squeezebox Alert",
"version": "1",
"slug": "squeezebox_alert",
"description": "Play audio alerts through LMS",
"startup": "application",
"boot": "auto",
"stdin": true,
"options": {
"json_rpc": "http://user:[email protected]:9000/jsonrpc.js"
},
"schema": {
"json_rpc": "str"
}
}
run.sh
#!/bin/bash
set -e
CONFIG_PATH=/data/options.json
JSONRPC=$(jq --raw-output ".json_rpc" $CONFIG_PATH)
echo "JSONRPC=$JSONRPC"
while read -r input; do
echo "input=$input"
PLAYLIST_NAME="$(echo "$input" | jq --raw-output '.playlist_name')"
echo "[Info] Read playlist_name: $PLAYLIST_NAME"
MAC="$(echo "$input" | jq --raw-output '.mac')"
echo "[Info] Read mac: $MAC"
ALERT_SONG="$(echo "$input" | jq --raw-output '.alert_song')"
echo "[Info] Read alert_song: $ALERT_SONG"
ALERT_VOLUME="$(echo "$input" | jq --raw-output '.alert_volume')"
echo "[Info] Read alert_volume: $ALERT_VOLUME"
./squeezebox_alert.sh $JSONRPC $PLAYLIST_NAME $MAC $ALERT_SONG $ALERT_VOLUME &
done
squeezebox_alert.sh
#!/bin/bash
#squeezebox_alert.sh
JSONRPC=$1
PLAYLIST_NAME=$2
MAC=$3
ALERT_SONG=$4
ALERT_VOLUME=$5
#restore_playlist=${6:-1}
restore_playlist=1
echo "restore_playlist=$restore_playlist"
#get power state
power=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["power","?"]]}' \
$JSONRPC | jq '.result._power')
prev_power=0
if [[ $power =~ .*1.* ]] ; then
prev_power=1
fi
echo "prev_power=$prev_power"
#get play mode
mode=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["mode","?"]]}' \
$JSONRPC | jq '.result._mode')
prev_playmode=0
if [[ $mode =~ .*play.* ]] ; then
prev_playmode=1
fi
echo "prev_playmode=$prev_playmode"
noplay=1
prev_time=0
if [ $prev_playmode -eq 1 ] ; then
noplay=0
# pause currently playing song
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["pause"]]}' $JSONRPC
echo "pause currently playing song"
# get paused time
prev_time=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["time","?"]]}' \
$JSONRPC | jq '.result._time')
echo "prev_time=$prev_time"
fi
# save current playlist
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["playlist","save","'"$PLAYLIST_NAME"'","silent:1"]]}' $JSONRPC
echo "save current playlist"
# GET SETTINGS TO RESTORE AFTER PLAYING ALERT SONG
#get current volume
prev_volume=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["mixer","volume","?"]]}' \
$JSONRPC | jq '.result._volume')
echo "prev_volume=$prev_volume"
#get current repeat setting
prev_repeat=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["playlist","repeat","?"]]}' \
$JSONRPC | jq '.result._repeat')
echo "prev_repeat=$prev_repeat"
# SET SETTINGS FOR ALERT SONG
#set ALERT_VOLUME to command argument value
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["mixer","volume",'$ALERT_VOLUME']]}' $JSONRPC
echo "set ALERT_VOLUME"
#set repeat setting to 0
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["playlist","repeat",0]]}' $JSONRPC
echo "set repeat_setting to 0"
# if ALERT_SONG is null, wait a second for song to start playing externally
if [ $ALERT_SONG = "null" ] ; then
echo "waiting for external song to start playing"
cur_mode="pause"
while [[ $cur_mode != *play* ]]; do
cur_mode=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["mode","?"]]}' \
$JSONRPC | jq '.result._mode')
sleep 0.2
done
else
#play alert song (this will clear current playlist)
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["playlist","play","'"$ALERT_SONG"'"]]}' $JSONRPC
echo "play alert song"
fi
# WAIT FOR ALERT SONG TO STOP PLAYING
echo "wait for alert song to stop playing"
cur_mode="play"
while [[ $cur_mode =~ .*play.* ]]; do
sleep 0.2
cur_mode=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["mode","?"]]}' \
$JSONRPC | jq '.result._mode')
done
echo "alert song stopped playing"
# RESTORE PREVIOUS SETTINGS
#restore prev_volume setting
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["mixer","volume",'"$prev_volume"']]}' $JSONRPC
echo "restore prev_volume setting"
#restore prev_repeat setting
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["playlist","repeat",'"$prev_repeat"']]}' $JSONRPC
echo "restore prev_repeat setting"
# resume previous playlist (always resume if previously playing)
if [ $prev_playmode -eq 1 ] || [ $restore_playlist -eq 1 ] ; then
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["playlist","resume","'"$PLAYLIST_NAME"'","noplay:'$noplay'"]]}' $JSONRPC
echo "resume previous playlist"
fi
# RESUME PREVIOUSLY PLAYING MUSIC
if [ $prev_playmode -eq 1 ] ; then
#skip ahead in song to prev_time
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["time",'"$prev_time"']]}' $JSONRPC
echo "skip ahead in song to prev_time"
fi
#restore prev_power setting
if [ $prev_power -eq 0 ] ; then
echo "prev_power setting was off, power off"
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["power",0]]}' $JSONRPC
fi
-
Perform the necessary steps to install the squeezebox_alert addon in Hass.io. See here:
https://home-assistant.io/developers/hassio/addon_tutorial/#step-2-installing-and-testing-your-add-on
-
Add the necessary lines to your configuration.yaml file (not complete).
example uses in configuration.yaml (1.8 KB)