Code: Github

In this tutorial, you will learn step-by-step how to connect and control Xiaomi IoT devices using Python.

Step 1: Connect Devices to WiFi

Ensure your devices can connect to your smartphone or computer using WiFi. Using another protocol may require a Bridge or Gateway.

Step 2: Find Device Model & MIIO API

Find your device’s model and locate the corresponding API in the MIIO package.

Step 3: Search for Device APIs

If you cannot find the APIs for your device, try searching for your device model in the MIIO GitHub Issues.

If this does not work, proceed to Step 4.

Step 4: Unsupported Devices

For devices not supported by the MIIO API, such as Mi Smart Power Plug 2, you can try solutions like the one I posted on GitHub.

To set up:

  1. Check your device’s JSON file here. Search for the device, e.g., “Mi Smart Plug (WiFi)”.

  2. Click the model link, e.g., “chuangmi.plug.hmi206”.

(3) Identify SIID (Service ID) and PIID (Property ID).

Example Code Snippet

With SIID and PIID, you can control the device status in Python using MIIO’s Device class and send() function. For instance, to turn on a socket:

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

Replace "DEVICE_IP", "DEVICE_TOKEN", and other parameters with the relevant values for your device.

This guide helps automate device control through Python for smart home applications.

Conclusion

By following these steps, you can connect and control Xiaomi IoT devices seamlessly, even if they are not directly supported by the MIIO library.