python-miio is a Python library and accompanying command-line tool (`miiocli`) designed to control Xiaomi smart home devices using their miIO and MIoT protocols. It provides an API to interact with a wide range of devices, including robot vacuums, air purifiers, humidifiers, lights, and smart plugs. The library is actively maintained with ongoing development towards a significant 0.6.0 release that introduces major architectural changes.
pip install python-miioVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to connect to a Xiaomi smart appliance using `DeviceFactory`, which is the recommended approach for modern MIoT devices and future versions of the library (0.6.0+). It attempts to retrieve basic device information and status. Device IP and token are expected from environment variables for security and flexibility.
Migrate to `DeviceFactory.create(ip, token)` for device instantiation. If using a specific device class, verify its new import path under `miio.integrations`. Ensure your Python version is 3.8 or higher.
Use the more generic device class or leverage `DeviceFactory.create()` which handles model-based discovery. For example, instead of `AirHumidifierCA1`, use `AirHumidifier`.
Refer to the official documentation's 'Obtaining tokens' section. The `miiocli cloud` command is often the easiest, but if it fails, 'Legacy methods for obtaining tokens' documentation provides alternatives.
Place the `python-miio` client and the Xiaomi device on the same subnet. Alternatively, if dual-homing is an option, configure your client to be in both subnets. For firewall rules, use `REJECT` instead of `DROP` for outbound connections if you wish to keep the device isolated.