Version 2 of squeezebox_alert addon:
- Updated so delay is no longer needed in configuration.yaml for TTS.
- <player_name>.lock file is used to only permit 1 audio alert per player at a time (created under config dir)
- The following configuration.yaml file entries have changed (see example configuration.yaml for details):
‘playlist_name’ => ‘player_name’ (NOTE: For TTS, the string value for ‘player_name’ must match your squeezebox media_player name exactly)
‘alert_song’ => ‘alert_mp3’
Added: ‘tts_message’ (NOTE: ‘tts_message’ is ignored if ‘alert_mp3’ is defined. I have not tested passing templates or other weird things in as the ‘tts_message’)
Dockerfile (same as previous version):
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": "2",
"slug": "squeezebox_alert",
"description": "Play audio alerts through LMS",
"startup": "application",
"boot": "auto",
"homeassistant_api": true,
"stdin": true,
"map": ["config:rw"],
"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"
PLAYER_NAME="$(echo "$input" | jq --raw-output '.player_name')"
echo "[Info] Read player_name: $PLAYER_NAME"
MAC="$(echo "$input" | jq --raw-output '.mac')"
echo "[Info] Read mac: $MAC"
ALERT_MP3="$(echo "$input" | jq --raw-output '.alert_mp3')"
echo "[Info] Read alert_mp3: $ALERT_MP3"
#NOTE: tts_message is ignored if alert_mp3 is defined
TTS_MSG="$(echo "$input" | jq --raw-output '.tts_message')"
echo "[Info] Read tts_message: $TTS_MSG"
ALERT_VOLUME="$(echo "$input" | jq --raw-output '.alert_volume')"
echo "[Info] Read alert_volume: $ALERT_VOLUME"
./squeezebox_alert.sh $JSONRPC $PLAYER_NAME $MAC "$ALERT_MP3" "$TTS_MSG" $ALERT_VOLUME &
done
squeezebox_alert.sh
#!/bin/bash
#squeezebox_alert.sh
JSONRPC=$1
PLAYER_NAME=$2
MAC=$3
ALERT_MP3=$4
TTS_MSG=$5
ALERT_VOLUME=$6
#restore_playlist=${7:-1}
restore_playlist=1
echo "restore_playlist=$restore_playlist"
#exit script if a lock file named $PLAYER_NAME exists, which signifies this player is being used
LOCK_FILE=/config/$PLAYER_NAME.lock
if [ -f $LOCK_FILE ] ; then
echo "script is locked...exiting."
exit 0
fi
#create a lock file named $PLAYER_NAME to signify this player is being used
echo "lock" >> $LOCK_FILE
#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
echo "pause currently playing song"
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["pause"]]}' $JSONRPC
#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
echo "save current playlist"
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["playlist","save","'"$PLAYER_NAME"'","silent:1"]]}' $JSONRPC
#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
echo "set ALERT_VOLUME"
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["mixer","volume",'$ALERT_VOLUME']]}' $JSONRPC
#set repeat setting to 0
echo "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
#if ALERT_MP3 is null, play alert using TTS
if [ $ALERT_MP3 = "null" ] ; then
echo "play TTS alert"
curl -X POST -H "x-ha-access: API_TOKEN" -H "Content-Type: application/json" \
-d '{"entity_id": "media_player.'$PLAYER_NAME'", "message": "'"$TTS_MSG"'"}' \
http://hassio/homeassistant/api/services/tts/google_say
else
#play alert mp3
echo "play alert mp3"
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["playlist","play","'"$ALERT_MP3"'"]]}' $JSONRPC
fi
#WAIT FOR ALERT SONG TO STOP PLAYING
echo "wait for alert song to stop playing"
cur_mode="play"
#TODO: Timeout after 10 seconds if song doesn't stop playing
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
echo "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
#restore prev_repeat setting
echo "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
#resume previous playlist (always resume if previously playing)
if [ $prev_playmode -eq 1 ] || [ $restore_playlist -eq 1 ] ; then
echo "resume previous playlist"
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["playlist","resume","'"$PLAYER_NAME"'","noplay:'$noplay'"]]}' $JSONRPC
fi
#RESUME PREVIOUSLY PLAYING MUSIC
if [ $prev_playmode -eq 1 ] ; then
#skip ahead in song to prev_time
echo "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
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
#Wait a couple of seconds and then delete lock file (this seems to be necessary to allow curl commands to complete)
sleep 2
rm $LOCK_FILE
exit 0
New example configuration.yaml file (not complete):
example uses in configuration.yaml (1.5 KB)