Hit the Sesami API in Python

What i did

Control your home smart lock (Sesami) from your Raspberry Pi.

reference

Sesame API Version 3 Tutorial

Creation part

Use a unique token.

[STEP1] Get device_id

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]

[STEP2] Unlock or lock the 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"]

[STEP3] Check if the command was executed normally

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

Hit the Sesami API in Python
Hit the web API in Python
Hit Mastodon's API in Python
Hit the Firebase Dynamic Links API in Python
Getting the arXiv API in Python
[Python] Hit the Google Translation API
Hit the Etherpad-lite API with Python
Access the Twitter API in Python
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Tweet using the Twitter API in Python
Try hitting the YouTube API in Python
Evernote API in Python
C API in Python 3
An easy way to hit the Amazon Product API in Python
Hit the New Relic API in Python to get the server status
Try using the BitFlyer Ligntning API in Python
Tips for hitting the ATND API in Python
Try using the DropBox Core API in Python
Download the file in Python
Blender Python API in Houdini (Python 3)
Call the API with python3.
Initial settings when using the foursquare API in python
PHP and Python samples that hit the ChatWork API
Using the National Diet Library Search API in Python
Hit a command in Python (Windows)
Get your heart rate from the fitbit API in Python!
Python in the browser: Brython's recommendation
Save the binary file in Python
Get the desktop path in Python
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
Create Gmail in Python without API
Use the Flickr API from Python
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Get the desktop path in Python
Quickly implement REST API in Python
Get the host name in Python
The first step in Python Matplotlib
I wrote the stack in Python
Master the weakref module in Python
Regularly upload files to Google Drive using the Google Drive API in Python
Python beginners hit the unofficial API of Google Play Music to play music
Play by hitting the Riot Games API in Python First half
Learn the design pattern "Prototype" in Python
Learn the design pattern "Builder" in Python
Mouse operation using Windows API in Python
Load the remote Python SDK in IntelliJ
Check the behavior of destructor in Python
Learn the design pattern "Observer" in Python
Learn the design pattern "Proxy" in Python
Write the test in a python docstring
Learn the design pattern "Command" in Python
OR the List in Python (zip function)
[FX] Hit oanda-API in Python using Docker
Display Python 3 in the browser with MAMP
Learn the design pattern "Visitor" in Python
Get Google Fit API data in Python