Last active
January 20, 2023 10:20
-
-
Save mbierman/a586ff9052bb475de2fb31e6d7407a0d to your computer and use it in GitHub Desktop.
grabs the token stored in homebridge for flo and sees if it needs updating. See also reboot.sh
This file contains 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/sh | |
# v 1.2 | |
# https://gist.github.com/mbierman/dd45821b53d5d22147cef217f0c0fe95 | |
# Variables | |
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 | |
NOW=$(date "+%a %b %d %I:%M %p") | |
preserve=219 | |
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 | |
} | |
doit () { | |
$dir/reboot.sh -r | |
echo "Rebooting..." | |
NOW=$(date "+%a %b %d %I:%M %p") | |
tail -${preserve} ${dir}/update.log > ${dir}/update.log.tmp | |
echo "$NOW ❎ Updating Flo credential" >> ${dir}/update.log.tmp | |
rm ${dir}/update.log.tmp | |
} | |
# Flo | |
if [ -n "$fuser" ]; then | |
NOW=$(date "+%a %b %d %I:%M %p") | |
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') | |
fi | |
cd $dir && auth=$(grep -m1 'authorization' config.json | cut -f2 -d ":" | sed -e 's/"//g' -e 's|,||' ) | |
echo $auth | |
echo $(pwd) | |
# get current status to test auth credential | |
# This line would have gotten the current credential but we want the old one as a test. | |
# status=$(curl --location --request GET "https://api-gw.meetflo.com/api/v2/locations/a6d74a0b-5274-4937-ae73-58da042acbd3" \ | |
# --header "Authorization: $token " \ | |
status=$(curl --location --request GET "https://api-gw.meetflo.com/api/v2/locations/a6d74a0b-5274-4937-ae73-58da042acbd3" \ | |
--header "Authorization: $auth " \ | |
--header "Content-Type: application/json" | jq -r '.systemMode.target') | |
if [ $1 = "-f" ]; then | |
doit | |
echo done! | |
exit | |
fi | |
echo $status | |
if [ $status = "home" ] || [ $status = "away" ] || [ $status = "sleep" ] ; then | |
NOW=$(date "+%a %b %d %I:%M %p") | |
echo "$NOW nothing to do" | |
# tail -1 update.log | grep fine && (sed '$d' update.log ) && echo yes | |
grep -v fine ${dir}/update.log > ${dir}/update.log.tmp | |
echo "$NOW ✅ Flo credential is fine." >> ${dir}/update.log.tmp | |
tail -${preserve} ${dir}/update.log.tmp > ${dir}/update.log | |
rm ${dir}/update.log.tmp | |
exit | |
else | |
doit | |
fi | |
echo $(date) Done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment