Python library for interfacing with Xiaomi smart appliances

0.5.12 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

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.

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}")

view raw JSON →