Preface
Around last year or the year before, after switching phone to iOS, no Xiaoai classmate anymore, using Mi Home App to control Mi Home devices was really painful. So at that time, I tinkered with HomeAssistant solution, and wrote such an article.
Zhihu Column: Home Assistant Smart Home System Setup Guide (Mi Home + Siri)
This solution with HomePod speaker basically met my needs. Later, home basically had various devices, HomePod was basically the whole house central control.
Home Assistant running on home host 24/7, basically no issues, so didn’t tinker for a long time.
Another “New Requirement”
At the beginning of the year, a big shot contacted me, asking if based on current Mi Home or other hardware devices, can do apartment smart home solution.
Based on the above experience, I gave some suggestions, but didn’t research deeply.
At that time, let a certain 20-year teacher tinker with DIY control Mi Home devices solution, this friend’s progress not ideal, finally no results.
Just at that time knew there is a github.com/rytilahti/python-miio good tool.
- Python library & console tool for controlling Xiaomi smart appliances
- A Python library and console tool for controlling Xiaomi smart home devices
Interesting, Mark it.
Recent Practice
Recently, again because of some strange requirements, need to “seriously” tinker with Mi Home device control solution.
Earliest when doing solution verification, even used Appium to simulate operating Mi Home App, but lag, slow, unstable…
To these two weeks entering formal practice stage, decided to use python-miio, although Hack, but definitely more stable than Appium.
So, let’s start.
Getting Started with python-miio
First, install python-miio locally
| |
After installing, local command line should have miiocli command line tool.
| |
Normally, we should be able to control our Mi Home devices through miiocli.
First, we need to find our device’s IP address and Token.
All subsequent operations are based on these two pieces of information.
Get Device IP Address and Token
| |
Here basically can see devices under your account, then we can control these devices through miiocli commands.
Control Devices
| |
Here need to pay attention to Supported by genericmiot, if have this, basically can control this device through miiocli genericmiot.
| |

The following documentation excerpted and translated from official docs.
Control Modern (MIoT) Devices
Most modern (MIoT) devices will automatically be supported by genericmiot integration.
Internally, it uses (“miot spec”) files to understand supported features like sensors, settings, and actions.
This device model specific file will be downloaded (and cached locally) when you first use genericmiot integration.
All supported device features can be controlled using common commands: status (show device status), set (change settings), actions list available actions, and call execute actions.
Device Status
Executing status will show current device status, and acceptable values for settings (marked with access RW):
| |
Change Settings
To change settings, you need to provide the setting name (e.g., in the above example light:brightness):
| |
Use Actions
Most devices will also provide actions:
| |
These can be executed using the call command:
| |
Use miiocli genericmiot –help for more available commands.
Detailed docs here:
Control Your Mi Home Devices with One Line of Code
Based on the above operations, this one line of code came out.
| |
Of course if you need to write some code yourself, it’s like this.
| |
Basically, as above.
Summary
python-miio is a good tool for controlling Mi Home devices
- Through miiocli can conveniently control devices
- Through miiocli genericmiot can control modern devices
- Through miiocli genericmiot actions can view device supported actions
- Through miiocli genericmiot status can view device status
- Through miiocli genericmiot set can change device properties
- Through miiocli genericmiot call can execute device actions
- Through miiocli genericmiot –help can view more commands
At the same time can write code to tinker with specific devices, making some automation things also simple.
Wish everyone have fun~
