How to Connect and Control Mi IoT Devices in Python

Step 1

Ensure your devices can connect to your smartphone or computer using WIFI. Using other protocol may need to use a Bridge / Gateway.

Step 2

Find your device’s mode, and find the correspondingly API in MIIO package.

Step 3

If you fail to find the APIs of your devices, try to search your devices mode in MIIO’s Github Issue.

If you cannot find any solution and your device supports WIFI connection. Then try the solution in Step 4.

Step 4

If your devices are not supported by MIIO API now, e.g.,MI Smart Power Plug 2, you can try the solution I post on Github Issue.

You can check your devices’ json file using the link. First, search the device in the home page, e.g., Mi Smart Plug (WiFi), then click the mode link in the page, e.g., chuangmi.plug.hmi206. Then you can see the SIID which indicates the service ID, and the PIID, which indicates property ID.

With SIID and PIID, you can set status of devices in python using MIIO’s Device Class and send function. e.g.,
if you want to open the socket, use the following codes:

from miio.device import Device 
plug = Device("DEVICE_IP", "DEVICE_TOKEN") 
print(plug.send("set_properties", [{'did': 'MYDID', 'siid': 2, 'piid': 1, 'value':True}]))