Python library for interfacing with Xiaomi smart appliances
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.
Warnings
- breaking Starting with 0.6.0, major breaking changes include moving device integrations into `miio.integrations` (e.g., `miio.vacuum` becomes `miio.integrations.vacuum.roborock`). Direct imports of device classes are discouraged in favor of `DeviceFactory.create()` for automatic discovery. Python 3.7 support will be dropped.
- deprecated Specific device classes (e.g., `AirFreshVA4`, `AirHumidifierCA1/CB1/CB2`, `Vacuum`) have been deprecated in favor of more generic classes (e.g., `AirFresh`, `AirHumidifier`) and model-based discovery. The `Vacuum` class itself is deprecated and will be reused as a common interface.
- gotcha Obtaining device tokens can be challenging. While `miiocli cloud` (requires `micloud` package) can fetch tokens from your Xiaomi cloud account, many users still resort to legacy methods like inspecting Mi Home app logs or backups for older versions.
- gotcha Devices may not respond or exhibit intermittent connection issues if the client and the Xiaomi device are on different subnets. The device might reject requests if the source IP is not in its local subnet.
Install
-
pip install python-miio -
pip install --pre python-miio -
pip install 'python-miio[micloud]'
Imports
- DeviceFactory
from miio import DeviceFactory
- RoborockVacuum
from miio.vacuum import RoborockVacuum
from miio import RoborockVacuum
Quickstart
import os
from miio import DeviceFactory
from miio.exceptions import DeviceException
DEVICE_IP = os.environ.get('MIIO_DEVICE_IP', '192.168.1.100')
DEVICE_TOKEN = os.environ.get('MIIO_DEVICE_TOKEN', 'YOUR_DEVICE_TOKEN') # 32-character hex token
if DEVICE_TOKEN == 'YOUR_DEVICE_TOKEN':
print("WARNING: Replace 'YOUR_DEVICE_TOKEN' with your actual device token or set MIIO_DEVICE_TOKEN environment variable.")
print(" You can obtain tokens using 'miiocli cloud' (requires micloud) or legacy methods outlined in the documentation.")
exit(1)
if __name__ == '__main__':
try:
# For modern MIoT devices or future-proof instantiation (requires python-miio 0.6.0.dev0+ for best results)
# For 0.5.x, direct class import like `from miio import RoborockVacuum` is more typical.
dev = DeviceFactory.create(DEVICE_IP, DEVICE_TOKEN)
print(f"Connected to device: {dev.info.model} ({dev.info.firmware_version})")
status = dev.status()
if hasattr(status, 'power'):
print(f"Device power: {status.power}")
elif hasattr(status, 'temperature'):
print(f"Device temperature: {status.temperature}°C")
else:
print(f"Device status: {status.data}")
except DeviceException as e:
print(f"Error connecting to device: {e}")
print("Ensure the IP address and token are correct and the device is on the same network or subnet.")
except Exception as e:
print(f"An unexpected error occurred: {e}")