Last active
September 4, 2018 18:03
-
-
Save Flavien06/ff387e99637b3cf897766e2ed926efa3 to your computer and use it in GitHub Desktop.
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/bash | |
# ------------------------------------------------------------------------------------------ | |
# This script mimics the usage of the Home Wizard Lite app | |
# https://gathering.tweakers.net/forum/list_message/52988479#52988479 | |
# ------------------------------------------------------------------------------------------ | |
# 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: | |
# 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" "plugid" | |
# For learning purposes you can also use the script without parameters. | |
# ------------------------------------------------------------------------------------------ | |
# 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 save sessionid | |
# ------------------------------------------------------------------------------------------ | |
source $file 2>/dev/null #find the sessionid | |
# Checking the parameters | |
# ----------------------------------- | |
searchswitch=$1 # e.g. SmartSwitch1 | |
searchdevice=$2 # e.g. Controller1 | |
doaction=$3 # On, Off, or one of the other applicable actions | |
timeout=$4 # in seconds (default 10) | |
if ! [[ "$timeout" =~ ^[0-9]+$ ]]; then timeout=10; fi | |
deviceid=$5 | |
plugid=$6 | |
while [[ $failed -lt "3" ]] && [[ ! $success = "1" ]] ; do | |
# Login to HomeWizard cloud | |
# ----------------------------------- | |
if [ ! -f "$file" ] || [ "$sessionid" = "" ]; 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 | |
rm $file | |
echo -e "Login failed ... Did you enter correctly your username and password_sha1 in the script?\nExiting ..." | |
exit | |
fi | |
fi | |
if [ "$sessionid" = "" ]; then echo "find sessionid ..." && 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 "Your plugid is : $plugid" | |
fi | |
if [ "$plugid" = "" ]; then #plug not found -> choose from list | |
chosen="1" | |
plugnames=($(echo $alljson | jq -r '.[].name')) | |
#echo $plugnames | |
echo "SmartSwitch \"$searchswitch\" not found" | |
PS3="Please select a SmartSwitch or quit: " | |
select opt in "${plugnames[@]}" "quit"; do | |
if (( $REPLY <= ${#plugnames[@]} )); then | |
searchswitch=$opt | |
plugid=$(echo $alljson | jq --arg ss $searchswitch -r '.[] | select(.name==$ss) | .id') | |
break | |
fi | |
case $REPLY in | |
$(( ${#plugnames[@]}+1 )) ) echo "Goodbye!"; break;; | |
*) echo "Invalid option. Try another one.";continue;; | |
esac | |
done | |
rm $file | |
#echo -e "$searchswitch not found ... Is the name exactly as in the app?\nExiting ..." | |
if [ "$plugid" = "" ]; then exit; fi | |
echo "" | |
fi | |
if [ "$deviceid" = "" ]; then | |
echo "find deviceid..." | |
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 devices is : $deviceid" | |
fi | |
if [ "$deviceid" = "" ]; then #device not found -> choose from list | |
chosen="1" | |
devicenames=($(echo $devices | jq -r '.[].name')) | |
#echo $devicenames | |
echo "Device \"$searchdevice\" not found" | |
PS3="Please select a device or quit: " | |
select opt in "${devicenames[@]}" "quit"; do | |
if (( $REPLY <= ${#devicenames[@]} )); then | |
searchdevice=$opt | |
deviceid=$(echo $devices | jq --arg sd $searchdevice -r '.[] | select(.name==$sd) | .id') | |
break | |
fi | |
case $REPLY in | |
$(( ${#devicenames[@]}+1 )) ) echo "Goodbye!"; break;; | |
*) echo "Invalid option. Try another one.";continue;; | |
esac | |
done | |
rm $file | |
#echo -e "$searchdevice not found ... Is the name exactly as in the app?\nExiting ..." | |
if [ "$deviceid" = "" ]; then exit; fi | |
echo "" | |
fi | |
actions="On, Off, Up, Down, Left, Right, Stop, Favorite, Pair, ManualMode, AutomaticMode, DayMode, NightMode, GetState, Range, Open, Close" | |
if [[ ! ", $actions, " = *", $doaction, "* ]]; then #action not found -> choose from list | |
chosen="1" | |
echo "Action not in list (not all may apply)" | |
PS3="Please select an action or quit: " | |
actionlist=($( echo $actions | tr ", " " " )) | |
select opt in "${actionlist[@]}" "quit"; do | |
if (( $REPLY <= ${#actionlist[@]} )); then | |
doaction=$opt | |
break | |
fi | |
case $REPLY in | |
$(( ${#actionlist[@]}+1 )) ) echo "Goodbye!"; break;; | |
*) echo "Invalid option. Try another one.";continue;; | |
esac | |
done | |
if [[ ! ", $actions, " = *", $doaction, "* ]]; then exit; fi | |
echo "" | |
fi | |
if [[ "$chosen" = "1" ]]; then | |
echo "Next time you may use the following command line with parameters:" | |
echo "${0} \"$searchswitch\" \"$searchdevice\" \"$doaction\" $4" | |
echo "" | |
fi | |
# Sending the action | |
# ----------------------------------- | |
echo "Sending {"$searchswitch", "$searchdevice", "$doaction"} during max. "$timeout" seconds ..." | |
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 | |
success="1" | |
echo "sessionid=$sessionid" > $file | |
break | |
elif [[ "$status" = *"Unauthorized"* ]] ; then | |
if [ -f "$file" ];then rm $file ; fi | |
unset sessionid | |
failed=$(($failed+1)) | |
echo -e "Login $failed failed ..." | |
break | |
fi | |
done | |
# if [[ ! "$success" = "1" ]]; then | |
# printf "To:root\nFrom:pi\nSubject:HomeWizard-actie niet succesvol\n\nDe HomeWizard-actie {$searchswitch, $searchdevice, $doaction} kon niet verzonden worden met de volgende foutmelding:\n $status" | sendmail -t | |
# fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment