Skip to content

Instantly share code, notes, and snippets.

@Flavien06
Last active July 18, 2021 10:21
Show Gist options
  • Save Flavien06/dacdd6bfae400c167798b2ae36ee8cf8 to your computer and use it in GitHub Desktop.
Save Flavien06/dacdd6bfae400c167798b2ae36ee8cf8 to your computer and use it in GitHub Desktop.
#!/bin/bash
# ------------------------------------------------------------------------------------------
# This script mimics the usage of the Home Wizard Lite app
# https://gathering.tweakers.net/forum/list_messages/1802639
# ------------------------------------------------------------------------------------------
# Dependencies: curl and jq (sudo apt-get install curl jq)
# It needs three parameters:
# - the SmartSwitch you want to control, between quotes (exactly as named in the Home Wizard Lite app)
# - the device you want to control, between quotes (exactly as named in the Home Wizard Lite app)
# - the action you want to perform, between quotes
# Depending on the device you control, one of the following actions may apply:
actionlist="On, Off, Up, Down, Left, Right, Stop, Favorite, Pair, ManualMode, AutomaticMode, DayMode, NightMode, GetState, Range, Open, Close"
# The fourth parameter is optional and indicates the time (in seconds) that the script will keep trying to perform the action.
# Example call: ./send_homewizard.sh "SmartSwitch1" "Controller1" "On" 60 "deviceid"
# ------------------------------------------------------------------------------------------
# You have to fill in your HomeWizard Lite username and the sha1-hash of your password between the quotes:
username=""
password_sha1="" #can be generated on https://hash.online-convert.com/sha1-generator
file="/tmp/homewizard_login" #file to sauve seesion_id and plugid
# ------------------------------------------------------------------------------------------
source $file 2>/dev/null
# Checking the parameters
# -----------------------------------
searchswitch=$1 # e.g. SmartSwitch1
if [ "$searchswitch" = "" ]; then echo -e "Missing parameter SmartSwitch ...\nExiting ..."; exit; fi
searchdevice=$2 # e.g. Controller1
if [ "$searchdevice" = "" ]; then echo -e "Missing parameter Device ...\nExiting ..."; exit; fi
doaction=$3 # On, Off, or one of the other applicable actions
if [ "$doaction" = "" ]; then echo -e "Missing parameter Action ...\nExiting ..."; exit; fi
if [[ ! ", $actionlist, " = *", $doaction, "* ]]; then echo -e "Action not in list {$actionlist}\nExiting ..."; exit; fi
timeout=$4 # in seconds (default 10)
if ! [[ "$timeout" =~ ^[0-9]+$ ]]; then timeout=10; fi
echo "Sending {"$searchswitch", "$searchdevice", "$doaction"} during max. "$timeout" seconds ..."
deviceid=$5
# Login to HomeWizard cloud
# -----------------------------------
if [ ! -f "$file" ]; then
echo "find login..."
login=$(curl -sS -u $username:$password_sha1 "https://cloud.homewizard.com/account/login")
#echo $login
if [[ ! "$(echo $login | jq -r '.status')" = "ok" ]]; then
if [ -f "$file" ];then rm $file ; fi
echo -e "Login failed ... Did you enter correctly your username and password_sha1 in the script?\nExiting ..."
exit
fi
fi
if [ "$sessionid" = "" ]; then sessionid=$(echo $login | jq -r '.session'); fi
#echo $sessionid
# Determining the plugid and deviceid
# -----------------------------------
if [ "$plugid" = "" ] ; then
echo "find plugid..."
alljson=$(curl -sS -H "X-Session-Token: $sessionid" "https://plug.homewizard.com/plugs")
#echo $alljson
plugid=$(echo $alljson | jq --arg ss $searchswitch -r 'select(.[].name==$ss) | .[].id')
#echo $plugid
fi
if [ "$plugid" = "" ]; then
if [ -f "$file" ];then rm $file ; fi
echo -e "$searchswitch not found ... Is the name exactly as in the app?\nExiting ..."
exit
fi
if [ "$deviceid" = "" ]; then
echo "find deviceid..."
alljson=$(curl -sS -H "X-Session-Token: $sessionid" "https://plug.homewizard.com/plugs")
devices=$(echo $alljson | jq --arg ss $searchswitch 'select(.[].name==$ss) | .[].devices')
#echo $devices
deviceid=$(echo $devices | jq --arg sd $searchdevice -r '.[] | select(.name==$sd) | .id')
echo "Your Device ID is : $deviceid"
fi
if [ "$deviceid" = "" ]; then
if [ -f "$file" ];then rm $file ; fi
echo -e "$searchdevice not found ... Is the name exactly as in the app?\nExiting ..."
exit
fi
# Sending the action
# -----------------------------------
startsec=$SECONDS
#echo $startsec
endsec=$(($startsec+$timeout))
#echo $endsec
while [ $SECONDS -lt $endsec ] ; do
status=$(curl -sS -H "X-Session-Token: $sessionid" -H "Content-Type: application/json; charset=utf-8" -X POST -d '{"action": "'$doaction'"}' 'https://plug.homewizard.com/plugs/'$plugid'/devices/'$deviceid'/action')
echo $status
if [[ "$status" = "{\"status\":\"Success\""* ]]; then
break
elif [[ "$status" = *"Unauthorized"* ]] ; then
if [ -f "$file" ];then rm $file ; fi
echo -e "Login failed ... Did you enter correctly your username and password_sha1 in the script?\nExiting ..."
exit
fi
done
if [ ! -f "$file" ];then
echo "plugid=$plugid" >> $file
echo "sessionid=$sessionid" >> $file
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment