Created
January 18, 2018 16:11
-
-
Save DavidGoodwin/f5a803bc4605fe5fb86c27b5cb4e0206 to your computer and use it in GitHub Desktop.
Revisions
-
DavidGoodwin created this gist
Jan 18, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,118 @@ #!/bin/bash # Bash script to control a TPLink HS110 or HS100 smart plug. # Usage: $0 -u username -p password -d "?" => list devices, and should output token. # Usage: $0 -t <token> -d <device_id> -o off => turn off.. # Usage: $0 -t <token> -d <device_id> -o off => turn on.. # Default action is to turn the first device found off. set -u ON_OFF="off" TOKEN="" DEVICE_ID="" APPSERVERURL="https://eu-wap.tplinkcloud.com" USERNAME="" PASSWORD="" while getopts "o:t:d:u:p:" o; do case "${o}" in o) ON_OFF=${OPTARG} ;; t) TOKEN=${OPTARG} USERNAME="" PASSWORD="" ;; u) TOKEN="" USERNAME=${OPTARG} ;; p) PASSWORD=${OPTARG} ;; d) DEVICE_ID=${OPTARG} ;; *) echo "Usage: $0 -o on|off -t TOKEN -d DEVICE_ID ... skipping token/device_id results in the API being queried more" echo "Usage: $0 -o on|off -u TpLinkUsername -p TpLinkPassword " echo " - no DEVICE_ID -> use the first one from querying" exit 1 esac done if [ "$ON_OFF" != "on" -a "$ON_OFF" != "off" ]; then echo "Usage: $0 -o on|off" exit 1 fi if [ "$TOKEN" = "" ]; then if [ "x$USERNAME" = "x" -o "x$PASSWORD" = "x" ]; then echo "Can't get a token without a username/password; use $0 ... -u [email protected] -p TopSecret " exit 1 fi TOKEN=$(curl -s -XPOST https://wap.tplinkcloud.com -H "Content-Type: application/json" -d ' { "method" : "login", "params": { "appType": "Kasa_Android", "cloudPassword": "'$PASSWORD'", "cloudUserName": "'$USERNAME'", "terminalUUID": "'$(< /proc/sys/kernel/random/uuid)'" } } ' | jq -r ".result.token" ) echo "Retrieved token of : $TOKEN from API " fi if [ "$DEVICE_ID" = "" -o "$DEVICE_ID" = "?" ]; then DEVICE_LIST=$(curl -s -XPOST "https://wap.tplinkcloud.com?token=$TOKEN" -H "Content-Type: application/json" -d ' { "method" : "getDeviceList" } ' ) if [ "$DEVICE_ID" = "?" ]; then echo "Device List -> " echo $DEVICE_LIST | jq . exit 0 fi APPSERVERURL=$(echo $DEVICE_LIST | jq -r .result.deviceList[0].appServerUrl) DEVICE_ID=$(echo $DEVICE_LIST | jq -r .result.deviceList[0].deviceId) echo "APP Server URL : $APPSERVERURL " echo "DEVICE ID: $DEVICE_ID" fi # Response like: # {"error_code":0,"result":{"responseData":"{\"system\":{\"set_relay_state\":{\"err_code\":0}}}"}} WHAT=0 if [ "$ON_OFF" = "on" ]; then echo "Turn plug on ... " WHAT=1 fi echo "Turn plug -> $WHAT ... " CHECK=$(curl -s -XPOST "$APPSERVERURL/?token=$TOKEN" -H "Content-Type: application/json" -d ' { "method": "passthrough", "params": { "deviceId": "'$DEVICE_ID'", "requestData": "{\"system\": {\"set_relay_state\":{\"state\":'$WHAT'}}}" } }') if [ $(echo $CHECK | jq -r .error_code) = "0" ]; then echo "Worked..." exit 0 fi echo "Check: $CHECK" exit 1