杰瑞科技汇

Python python-miio如何连接小米设备?

Of course! Let's break down the relationship between python and python-miio. This is a common point of confusion, so I'll explain it clearly.

Python python-miio如何连接小米设备?-图1
(图片来源网络,侵删)

The Short Answer

  • python: This is the programming language itself. It's the engine, the set of rules for writing code.
  • python-miio: This is a library (a specific package of code) written in the Python language. Its purpose is to allow you to control Xiaomi devices (like vacuum cleaners, air purifiers, smart bulbs, etc.) from your Python scripts.

Analogy: Think of python as a car engine. It's powerful but doesn't do much on its own. python-miio is like a specific car body, steering wheel, and dashboard built for that engine, designed to perform one specific task: driving a car (controlling a Xiaomi device).


Detailed Breakdown

python (The Programming Language)

This is the foundational element.

  • What it is: A high-level, general-purpose programming language. It's versatile and used for web development, data science, automation, artificial intelligence, and much more.
  • What it does: It provides the syntax and structure to write programs. It has a standard library with many built-in functions, but it's not specialized for any single task.
  • How it relates: python-miio is built using the Python language. You need to have Python installed on your computer to be able to use and run scripts that depend on the python-miio library.

python-miio (The Library)

This is the tool that bridges the gap between your Python code and your Xiaomi smart home devices.

  • What it is: A third-party Python library (hosted on PyPI, the Python Package Index).
  • What it does:
    • It discovers Xiaomi devices on your local network.
    • It communicates with these devices using the Mi Home protocol (which is often reverse-engineered, as it's not officially public).
    • It provides a simple Python interface (classes and functions) to send commands to the devices and receive status updates.
    • For example, it has a RoborockVacuum class that has methods like start(), stop(), home(), get_status(), etc.
  • How it relates: You install it into your Python environment, and then you import it into your scripts to use its functionality.

How They Work Together: A Practical Example

Let's say you want to start your Roborock vacuum cleaner from a Python script.

Python python-miio如何连接小米设备?-图2
(图片来源网络,侵删)

Step 1: Install Python First, you need the Python language installed on your system. You can download it from python.org. Make sure Python is added to your system's PATH.

Step 2: Install the python-miio Library Open your terminal or command prompt and use pip (Python's package installer) to install the library.

pip install python-miio

Step 3: Write a Python Script Now, create a Python file (e.g., vacuum_control.py) and use the library.

# vacuum_control.py
# 1. Import the necessary class from the library
from miio import RoborockVacuum
# --- Configuration ---
# You need to find your vacuum's IP address and token.
# The token is a long hexadecimal string found in the Mi Home app.
# You can also use the `miiocli` command-line tool to discover devices.
VACUUM_IP = "192.168.1.100"
VACUUM_TOKEN = "a1b2c3d4e5f67890" # Replace with your actual token
# 2. Create an instance of the vacuum object
vacuum = RoborockVacuum(ip=VACUUM_IP, token=VACUUM_TOKEN)
# 3. Use the methods provided by the library to control the device
print(f"Connecting to vacuum at {VACUUM_IP}...")
try:
    # Get the current status of the vacuum
    status = vacuum.status()
    print(f"Vacuum status: {status}")
    print(f"Battery level: {status.battery} %")
    # Start the cleaning
    print("Starting the vacuum...")
    vacuum.start()
    print("Vacuum has started!")
except Exception as e:
    print(f"An error occurred: {e}")

Step 4: Run the Script Execute the script from your terminal:

Python python-miio如何连接小米设备?-图3
(图片来源网络,侵删)
python vacuum_control.py

What's Happening Here?

  1. python vacuum_control.py: The Python interpreter (the python program) reads your script.
  2. from miio import RoborockVacuum: The script tells Python to load the RoborockVacuum class from the python-miio library.
  3. vacuum.start(): Your script calls the start() method, which is part of the python-miio library. This library then handles the low-level communication of sending the "start cleaning" command to your vacuum over the network.

Key Takeaways

Feature python python-miio
Type Programming Language Python Library (Package)
Purpose General-purpose programming; building any kind of software. Specific task: controlling Xiaomi smart home devices.
Dependency Required to run any Python code. Requires Python to be installed. It's a package for Python.
Installation Download from python.org. Installed via pip install python-miio.
Usage You write code in .py files. You import it into your Python files to use its functions.

In summary, python is the foundation, and python-miio is a specialized tool built on top of that foundation to solve a specific problem. You cannot use python-miio without Python, just as you cannot drive a car without an engine.

分享:
扫描分享到社交APP
上一篇
下一篇