Last active
October 6, 2018 09:59
-
-
Save shijaz/7f5d21939cbb84f3551107983cc61b28 to your computer and use it in GitHub Desktop.
Lambda function to toggle a TP-Link HS100 smart switch
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
import requests | |
import uuid | |
import json | |
import random | |
import os | |
USERNAME = 'your_kasa_user@email.com' | |
PASSWORD = os.environ['PASSWORD'] # not ideal but there aren't many options | |
def SwitchOn(): | |
# Get a token by authenticating with your Kasa username email and password. We'll generate a uuid4. | |
payload = {"method": "login","params":{"appType": "Kasa_Android","cloudUserName": USERNAME,"cloudPassword": PASSWORD,"terminalUUID": str(uuid.uuid4())}} | |
response = requests.post("https://wap.tplinkcloud.com/", json=payload) | |
obj = json.loads(response.content) | |
token = obj["result"]["token"] | |
# Find the plug we want to turn ON or OFF | |
payload = {"method": "getDeviceList"} | |
response = requests.post("https://wap.tplinkcloud.com?token={0}".format(token), json=payload) | |
# The response contains a list of devices, we're just going to use the first as I have only one device. You can filter by name using some of the other values like "alias" | |
obj = json.loads(response.content) | |
plug = obj["result"]["deviceList"][0] | |
# The app_server_url contains a regional address like eu-wap.tplinkcloud.com - we will use that | |
app_server_url = plug["appServerUrl"] | |
# Get the plug's deviceId | |
device_id = plug["deviceId"] | |
print (device_id) | |
# Create the payload for your POST request | |
payload={ | |
"method":"passthrough", | |
"params": { | |
"deviceId": device_id, | |
"requestData": "{\"system\":{\"get_sysinfo\":null},\"emeter\":{\"get_realtime\":null}}" }} | |
#Post the request to the app_server_url along with the token and grab the response | |
response = requests.post("{0}?token={1}".format(app_server_url, token), json=payload) | |
jresponse = json.loads(response.text) | |
print (jresponse) | |
d= json.loads(jresponse['result']['responseData']) | |
#Figure out if the switch is ON or OFF by checking the relay_state key in the response, and set the desired_state to the opposite (to toggle the switch) | |
if d['system']['get_sysinfo']['relay_state']==0: | |
print("Switch is OFF") | |
desired_state=1 | |
else: | |
print("Switch is ON") | |
desired_state=0 | |
# Send a command to the plug to toggle its status | |
plug_command = { | |
"system": { | |
"set_relay_state": { | |
"state": desired_state | |
} | |
} | |
} | |
# Create the payload with the plug_command so we can POST it | |
payload = { | |
"method": "passthrough", | |
"params": { | |
"deviceId": device_id, | |
"requestData": json.dumps(plug_command) # Request data needs to be escaped, it's a string! | |
} | |
} | |
# Print the JSON payload for debugging in CloudWatch | |
print (payload) | |
response = requests.post("{0}?token={1}".format(app_server_url, token), json=payload) | |
# Print the results in CloudWatch. error_code:0 means everything is OK. | |
print (response.content) | |
# TODO implement | |
def lambda_handler(event, context): | |
SwitchOn() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment