Control your home smart lock (Sesami) from your Raspberry Pi.
Use a unique token.
python
#Get device information with str by specifying key
# args: device_id, serial, nickname
def get_device_info(key):
url = "https://api.candyhouse.co/public/sesames"
headers = {"Authorization": token}
r = requests.get(url, headers=headers)
json_data = r.json()
return json_data[0][key]
Note that the URL is slightly different from STEP1. STEP1:https://api.candyhouse.co/public/sesames STEP2:https://api.candyhouse.co/public/sesame
#Unlock if the key status is lock,unlock if unlock
def toggle_device_key(device_id):
url = "https://api.candyhouse.co/public/sesame/" + device_id
headers = {"Authorization": token}
if key_is_locked(device_id) is True: # locked
r = requests.post(url, headers=headers, data='{"command":"unlock"}')
elif key_is_locked(device_id) is False: # unlocked
r = requests.post(url, headers=headers, data='{"command":"lock"}')
else:
print("error! toggle_device_key")
return
json_data = r.json()
return json_data["task_id"]
python
# task_Specify id and return execution result as boolean
def check_task_state(task_id):
url = "https://api.candyhouse.co/public/action-result?"
headers = {"Authorization": token}
# task_Do nothing if id is empty
if task_id is None:
return
r = requests.get(url + "task_id=" + task_id, headers=headers)
json_data = r.json()
#status completed(terminated)Wait until
while json_data["status"] != "terminated":
# wait
time.sleep(3)
# retry
r = requests.get(url + "task_id=" + task_id, headers=headers)
json_data = r.json()
else:
#Returns the status of the execution result as boolean
return json_data["successful"]
Recommended Posts