Mi Cloud Connect Library

0.6 · active · verified Thu Apr 16

The `micloud` library provides a Python interface to connect to Xiaomi's cloud services, allowing programmatic interaction with Xiaomi IoT devices registered to an account. It is currently at version 0.6 and has an irregular release cadence based on feature additions and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart logs into the Xiaomi Cloud using environment variables for credentials and then retrieves a list of connected devices. It's crucial to specify the correct `server` region matching your Xiaomi account.

import os
from micloud import MiCloud

# It's highly recommended to specify your server region explicitly.
# Common regions: 'de' (Germany, default), 'cn' (China), 'us' (USA), 'ru' (Russia).
# Replace with the region your Xiaomi account is registered in.
MI_CLOUD_SERVER = os.environ.get('MI_CLOUD_SERVER', 'de') 
MI_CLOUD_USERNAME = os.environ.get('MI_CLOUD_USERNAME', 'your_email@example.com')
MI_CLOUD_PASSWORD = os.environ.get('MI_CLOUD_PASSWORD', 'your_password')

try:
    micloud = MiCloud(MI_CLOUD_USERNAME, MI_CLOUD_PASSWORD, server=MI_CLOUD_SERVER)
    micloud.login()
    print(f"Successfully logged in to Xiaomi Cloud (server: {MI_CLOUD_SERVER}).")
    
    devices = micloud.get_devices()
    if devices:
        print(f"Found {len(devices)} devices:")
        for device in devices:
            print(f"  - Name: {device.get('name', 'N/A')}, Model: {device.get('model', 'N/A')}, ID: {device.get('did', 'N/A')}")
    else:
        print("No devices found associated with this account and server.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please check your credentials, server region, and network connection.")

view raw JSON →