Last active
April 7, 2025 12:34
-
-
Save mbierman/dd45821b53d5d22147cef217f0c0fe95 to your computer and use it in GitHub Desktop.
Restart Homebridge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# v 2.7 | |
# https://gist.github.com/mbierman/dd45821b53d5d22147cef217f0c0fe95 | |
# Variables | |
update="false" | |
reboot="false" | |
hostname=$(hostname) | |
docker="/.dockerenv" | |
dir=$(dirname "$0") | |
IFTTTKEY="$(cat $dir/rebootdata.txt | grep IFTTTKEY | cut -f2 -d "=" )" | |
IFTTTTrigger="$(cat $dir/rebootdata.txt | grep IFTTTTrigger | cut -f2 -d "=" )" | |
hbuser="$(cat $dir/rebootdata.txt | grep hbuser | cut -f2 -d "=" )" | |
hbpass="$(cat $dir/rebootdata.txt | grep hbpass | cut -f2 -d "=" )" | |
fuser="$(cat $dir/rebootdata.txt | grep fuser | cut -f2 -d "=" )" | |
fpass="$(cat $dir/rebootdata.txt | grep fpass | cut -f2 -d "=" )" | |
unsplashkey="$(cat $dir/rebootdata.txt | grep unsplash | cut -f2 -d "=" )" | |
version=latest | |
imagesearch="wallpaper,galaxies,webb,hubble" | |
user="nasa" | |
NOW=$(date "+%a %b %d %I:%M %p %Y") | |
pushAlert () { | |
# This requires an IFTTT pro key | |
if [ -n "IFTTTKEY" -a -n "$PACKAGE" ]; then | |
curl -X POST -H "Content-Type: application/json" --data '{"value1":"'$1'","value2":"'$URL'","value3":"'$IMAGE'"}' \ | |
https://maker.ifttt.com/trigger/$IFTTTTrigger/with/key/$IFTTTKEY | |
fi | |
} | |
# Get image | |
NOW=$(date "+%a %b %d %I:%M %p %Y") | |
echo "$NOW updating login image..." | |
image=$(curl --location --request GET 'https://api.unsplash.com/photos/random?orientation=landscape&client_id='$unsplashkey'&query='$imagesearch'&username='$user'&count=1'| jq -r '.[].urls.full') | |
echo $image | |
# curl -k -s -L "https://source.unsplash.com/user/$imagesearch/1680x1050/daily?orientation=landscape" -o ${dir}/ui-wallpaper.jpg | |
curl -L "$image" -o ${dir}/ui-wallpaper.jpg | |
# Get homebridge creds | |
if [ -z "$hbuser" -o -z "$hbpass" ]; then | |
echo "Sorry, $0 is not configured. You have to set your homebridge username and password in $dir/rebootdata.txt" | |
exit | |
fi | |
if [ "$hostname" = "homebridge" ]; then | |
homebridgeIP="192.168.0.19:8581" | |
IMAGE="https://sflanders.net/wp-content/uploads/2015/11/synology-150x150.png" | |
elif [ "$hostname" = "pigpen" ]; then | |
homebridgeIP="192.168.0.19:8581" | |
IMAGE="https://sflanders.net/wp-content/uploads/2015/11/synology-150x150.png" | |
elif [ "$hostname" = "firewalla" ]; then | |
homebridgeIP="192.168.0.1:8581" | |
IMAGE="https://avatars2.githubusercontent.com/u/21322342?s=200&v=4" | |
else | |
echo "unknown host" | |
exit | |
fi | |
URL=http://${homebridgeIP} | |
hbtoken=$(curl -X POST -d '{"username":"'$hbuser'", "password":"'$hbpass'"}' \ | |
-H "Content-Type: application/json" -H "accept: */*" http://${homebridgeIP}/api/auth/login | | |
cut -f2 -d":" | cut -f1 -d"," | sed -e 's/"//g') | |
for i in "$@" ; do | |
case $i in | |
--update|-u) | |
update="true" | |
;; | |
--reboot|-r) | |
reboot="true" | |
;; | |
--flo|-f) | |
reboot="true" | |
flo="true" | |
;; | |
--version|-v) | |
v1=$version | |
read -p "What version of homebridge to install? [$v1] " version | |
version=${version:-$v1} | |
;; | |
--node|-n) | |
node="true" | |
reboot="true" | |
;; | |
*) | |
printf "Usage: $0 [options [parameters]]\n\n" | |
printf "Options:\n" | |
printf " -r|--reboot , Reboot \n" | |
printf " -v|--version, version\n" | |
printf " -u|--update, Update\n" | |
return 0 | |
;; | |
esac | |
done | |
if [ -n "$fuser" ] && [ "$flo" = "true" ] ; then | |
NOW=$(date "+%a %b %d %I:%M %p %Y") | |
echo "$NOW updating Flo credentials..." | |
token=$(curl -s -X POST -d '{"username":"'$fuser'", "password":"'$fpass'"}' \ | |
-H "Content-Type: application/json" \ | |
https://api.meetflo.com/api/v1/users/auth | cut -f1 -d','| cut -f2 -d':' | sed -e 's/"//g') | |
# current=$(grep -m1 'authorization' $dir/config.json) | |
current=$(cat $dir/config.json | jq -r '.accessories[] | select(.name == "Flo").onUrl.headers.authorization') | |
echo $current | |
exit | |
new=$(echo " \"authorization\": '${token}'," | sed -e "s|'|\"|g") | |
sed -e "s|${current}|${new}|g" ${dir}/config.json > ${dir}/config.json.tmp && \ | |
mv $dir/config.json.tmp $dir/config.json | |
# curl -s -X POST \ | |
# -H 'accept: */*' \ | |
# -H 'Content-Type: application/json' \ | |
# -H "Authorization: Bearer ${hbtoken}" \ | |
# http://${homebridgeIP}/api/config-editor -d '{$config}' | |
# exit | |
fi | |
if [ "$update" = "true" ] ; then | |
# if [ "$update" = "true" ] && [ -f "$docker" ] ; then | |
# echo -e "Running in docker! \n\n" | |
echo " | |
Checking for updates... | |
" | |
log="$dir/update.log" | |
plugins="$(curl -s -X GET -H "Authorization: Bearer ${hbtoken}" -H 'accept: */*' \ | |
http://${homebridgeIP}/api/plugins | jq -r '.[].name')" | |
for plugin in ${plugins}; do | |
PACKAGE=$plugin | |
PACKAGEN=$(echo $PACKAGE | sed -e 's|@|%40|g' -e 's|/|%2F|g') | |
info="$(curl -s -X GET -H "Authorization: Bearer ${hbtoken}" http://${homebridgeIP}/api/plugins/lookup/$PACKAGEN)" | |
upgrade=$(echo $info | jq -r '.updateAvailable') | |
if [ $PACKAGE != "homebridge-config-ui-x" ] ; then | |
if [ "$upgrade" = "true" ]; then | |
currentversion=$(echo $info | jq -r '.installedVersion') | |
newversion=$(echo $info | jq -r '.latestVersion') | |
echo upgrade $PACKAGE is: $upgrade $currentversion $newversion | |
echo "cleaning up junk..." | |
cd $dir | |
find ./node_modules -type d -name '\.homebridge*' -exec rm -rf {} \; | |
NOW=$(date "+%a %b %d %H:%M %Y") | |
echo "$NOW updating... $PACKAGE..." | tee -a $log | |
hb-service add ${PACKAGE}@$newversion | |
pushAlert $PACKAGE $newversion $URL $IMAGE | |
reboot="true" | |
else | |
NOW=$(date "+%a %b %d %H:%M %Y") | |
echo "$NOW $PACKAGE is good..." | |
fi | |
else | |
echo "UI Found!!! do nothing." | |
fi | |
done | |
NOW=$(date "+%a %b %d %I:%M %p %Y") | |
echo "$NOW Plugin updates complete" | tee -a $log | |
echo "" | |
echo "" | |
# Check homebridge-ui | |
echo "checking homebridge-ui" | |
PACKAGE="homebridge-config-ui-x" | |
info="$(curl -s -X GET -H "Authorization: Bearer ${hbtoken}" \ | |
http://${homebridgeIP}/api/plugins/lookup/${PACKAGE})" | |
upgrade=$(echo $info | jq -r '.updateAvailable') | |
if [ "$upgrade" = "true" ] ; then | |
echo -e "\n\nUI Thing\n\n" | |
currentversion=$(echo $info | jq -r '.installedVersion') | |
newversion=$(echo $info | jq -r '.latestVersion') | |
echo upgrade $PACKAGE is: $upgrade $currentversion $newversion | |
NOW=$(date "+%a %b %d %I:%M %p %Y") | |
echo "$NOW updating... $PACKAGE..." | tee -a $log | |
upgrade="/opt/homebridge/lib/node_modules/homebridge-config-ui-x/upgrade-install.sh" | |
if [ -f "$upgrade" ] ; then | |
$upgrade $newversion /opt/homebridge | |
fi | |
# hb-service add $PACKAGE@$newversion | |
# cd /opt/homebridge | |
# npm --prefix "/var/lib/homebridge" add homebridge-config-ui-x@$newversion | |
pushAlert $PACKAGE $newversion $URL $IMAGE | |
reboot="true" | |
else | |
NOW=$(date "+%a %b %d %I:%M %p %Y") | |
echo "$NOW $PACKAGE is good..." | |
echo "SKIPPING" | |
fi | |
# Check homebridge itself | |
echo "checking homebridge" | |
PACKAGE="homebridge" | |
info="$(curl -s -X GET -H "Authorization: Bearer ${hbtoken}" http://${homebridgeIP}/api/status/${PACKAGE}-version)" | |
upgrade=$(echo $info | jq -r '.updateAvailable') | |
if [ "$upgrade" = "true" ] ; then | |
currentversion=$(echo $info | jq -r '.installedVersion') | |
newversion=$(echo $info | jq -r '.latestVersion') | |
echo upgrade $PACKAGE is: $upgrade $currentversion $newversion | |
NOW=$(date "+%a %b %d %I:%M %p %Y") | |
echo "$NOW updating... $PACKAGE..." | tee -a $log | |
npm install --save $PACKAGE@$newversion | |
pushAlert $PACKAGE $URL $IMAGE | |
reboot="true" | |
else | |
NOW=$(date "+%a %b %d %I:%M %p %Y") | |
echo "$NOW $PACKAGE is good..." | |
fi | |
node="true" | |
else | |
echo "Skipping updates." | |
fi | |
if [ "node" = "true" ] ; then | |
hb-service update-node | |
echo "Updating node..." | |
NOW=$(date "+%a %b %d %I:%M %p %Y") | |
echo "$NOW updating node..." >> ${dir}/update.log | |
tail -220 ${dir}/update.log > ${dir}/update.log.tmp | |
mv ${dir}/update.log.tmp ${dir}/update.log | |
fi | |
if [ "$reboot" = "true" ] ; then | |
echo "Rebooting..." | |
NOW=$(date "+%a %b %d %I:%M %p %Y") | |
echo "$NOW restarting homebridge container..." >> ${dir}/update.log | |
tail -220 ${dir}/update.log > ${dir}/update.log.tmp | |
mv ${dir}/update.log.tmp ${dir}/update.log | |
curl -s -X PUT -H "Authorization: Bearer ${hbtoken}" http://${homebridgeIP}/api/platform-tools/docker/restart-container | |
else | |
NOW=$(date "+%a %b %d %I:%M %p %Y") | |
echo "$NOW No reboot required..." >> ${dir}/update.log | |
tail -220 ${dir}/update.log > ${dir}/update.log.tmp | |
mv ${dir}/update.log.tmp ${dir}/update.log | |
fi | |
echo $(date) Done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IFTTTKEY=[insertyour IFTTKEY here] | |
IFTTTTrigger=homebridgeupdate | |
hbuser=[your homebridge user id] | |
hbpass=[your homebridge password] | |
fuser=[this is the Flo username. leave blank if you don't have a flo] | |
fpass=[this is the Flo password. leave blank if you don't have a flo] |
This is excellent, thank you!
The latest update:
- takes care of the "ENOTEMPTY" error.
- automatically checks currently installed plugins and updates them.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is what I use to reboot my homebridge. I run it with cron nightly.
2 1 * * * /homebridge/reboot.sh -u -r
What this script does
Plugin configuration
Note configure the passwords, ip addresses as needed. Currently configured for two homebridges.
Configuration instructions
In your /homebridge directory save the script above and
rebootdata.txt
. Inrebootdata.txt
add the following:No spaces after "=" and your data. Each piece of data must be on a separate line.
These credentials are kept separate so I can easily post updates to this script without accidentally leaving my credentials in there.
This script now requires jq. IF you use one of these homebridge startup scripts, jq will be installed when required.
Note, this is in sh, not bash. This wasn't by choice. The Homebridge docker images only support sh.
Update