ha_xiaomi_home README

The project's README from github.com/XiaoMi/ha_xiaomi_home. As one markdown file: README.md.

Xiaomi Home Integration for Home Assistant

English | 简体中文

Xiaomi Home Integration is an integrated component of Home Assistant supported by Xiaomi official. It allows you to use Xiaomi IoT smart devices in Home Assistant.

Installation

Home Assistant version requirement:

  • Core $\geq$ 2024.4.4
  • Operating System $\geq$ 13.0

Method 1: Git clone from GitHub

cd config
git clone https://github.com/XiaoMi/ha_xiaomi_home.git
cd ha_xiaomi_home
./install.sh /config

We recommend this installation method, for it is convenient to switch to a tag when updating xiaomi_home to a certain version.

For example, update to version v1.0.0

cd config/ha_xiaomi_home
git fetch
git checkout v1.0.0
./install.sh /config

Method 2: HACS

One-click installation from HACS:

Open your Home Assistant instance and open the Xiaomi Home integration inside the Home Assistant Community Store.

Or, HACS > In the search box, type Xiaomi Home > Click Xiaomi Home, getting into the detail page > DOWNLOAD

Method 3: Manually installation via Samba / FTPS

Download and copy custom_components/xiaomi_home folder to config/custom_components folder in your Home Assistant.

Configuration

Login

Settings > Devices & services > ADD INTEGRATION > Search Xiaomi Home > NEXT > Click here to login > Sign in with Xiaomi account

Open your Home Assistant instance and start setting up a new Xiaomi Home integration instance.

Add MIoT Devices

After logging in successfully, a dialog box named "Select Home and Devices" pops up. You can select the home containing the device that you want to import in Home Assistant.

Multiple User Login

After a Xiaomi account login and its user configuration are completed, you can continue to add other Xiaomi accounts in the configured Xiaomi Home Integration page.

Method: Settings > Devices & services > Configured > Xiaomi Home > ADD HUB > NEXT > Click here to login > Sign in with Xiaomi account

Open your Home Assistant instance and show Xiaomi Home integration.

Update Configurations

You can change the configurations in the "Configuration Options" dialog box, in which you can update your user nickname and the list of the devices importing from Xiaomi Home APP, etc.

Method: Settings > Devices & services > Configured > Xiaomi Home > CONFIGURE > Select the option to update

Debug Mode for Action

You can manually send Action command message with parameters to the device when the debug mode for action is activated. The user interface for sending the Action command with parameters is shown as a Text entity.

Method: Settings > Devices & services > Configured > Xiaomi Home > CONFIGURE > Debug mode for action

Security

Xiaomi Home Integration and the affiliated cloud interface is provided by Xiaomi officially. You need to use your Xiaomi account to login to get your device list. Xiaomi Home Integration implements OAuth 2.0 login process, which does not keep your account password in the Home Assistant application. However, due to the limitation of the Home Assistant platform, the user information (including device information, certificates, tokens, etc.) of your Xiaomi account will be saved in the Home Assistant configuration file in clear text after successful login. You need to ensure that your Home Assistant configuration file is properly stored. The exposure of your configuration file may result in others logging in with your identity.

If you suspect that your OAuth 2.0 token has been leaked, you can revoke the login authorization of your Xiaomi account by the following steps: Xiaomi Home APP -> Profile -> Click your username and get into Xiaomi Account management page -> Basic info: Apps -> Xiaomi Home (Home Assistant Integration) -> Remove

FAQ

Principle of Messaging

Control through the Cloud

Image 1: Cloud control architecture

Xiaomi Home Integration subscribes to the interested device messages on the MQTT Broker in MIoT Cloud. When a device property changes or a device event occurs, the device sends an upstream message to MIoT Cloud, and the MQTT Broker pushes the subscribed device message to Xiaomi Home Integration. Because Xiaomi Home Integration does not need to poll to obtain the current device property value in the cloud, it can immediately receive the notification message when the properties change or the events occur. Thanks to the message subscription mechanism, Xiaomi Home Integration only queries the properties of all devices from the cloud once when the integration configuration is completed, which puts little access pressure on the cloud.

Xiaomi Home Integration sends command messages to the devices via the HTTP interface of MIoT Cloud to control devices. The device reacts and responds after receiving the downstream message sent forward by MIoT Cloud.

Control locally

Image 2: Local control architecture

Xiaomi central hub gateway contains a standard MQTT Broker, which implements a complete subscribe-publish mechanism. Xiaomi Home Integration subscribes to the interested device messages through Xiaomi central hub gateway. When a device property changes or a device event occurs, the device sends an upstream message to Xiaomi central hub gateway, and the MQTT Broker pushes the subscribed device message to Xiaomi Home Integration.

When Xiaomi Home Integration needs to control a device, it publishes a device command message to the MQTT Broker, which is then forwarded to the device by Xiaomi central hub gateway. The device reacts and responds after receiving the downstream message from the gateway.

Mapping Relationship between MIoT-Spec-V2 and Home Assistant Entity

MIoT-Spec-V2 is the abbreviation for MIoT Specification Version 2, which is an IoT protocol formulated by Xiaomi IoT platform to give a standard functional description of IoT devices. It includes function definition (referred to as data model by other IoT platforms), interaction model, message format, and encoding.

In MIoT-Spec-V2 protocol, a product is defined as a device. A device contains several services. A service may have some properties, events and actions. Xiaomi Home Integration creates Home Assistant entities according to MIoT-Spec-V2. The conversion relationship is as follows.

General Conversion

access format value-list value-range Entity in Home Assistant
writable string - - Text
writable bool - - Switch
writable not string & not bool existent - Select
writable not string & not bool non-existent existent Number
not writable - - - Sensor

MIoT-Spec-V2 event is transformed to Event entity in Home Assistant. The event's parameters are also passed to entity's _trigger_event.

MIoT-Spec-V2 event's arguments field is the list of parameters of the event. The list elements represent the piid of the property in the same service. For example, the MIoT-Spec-V2 of the Xiaomi Wireless Double-key Switch contains the siid=2 Switch Sensor service. The eiid=1014 Long Press event of the service is triggered when a button is long pressed. The debug level log will print Press and hold, attributes: {'Button Type': 1}. This is an example log that the button type is 1, which means the right button is long pressed.

in Entity in Home Assistant
empty Button
not empty Notify

If the debug mode for action is activated, the Text entity will be created when the "in" field in the action spec is not empty.

The "Attribute" item in the entity details page displays the format of the input parameter which is an ordered list, enclosed in square brackets []. The string elements in the list are enclosed in double quotation marks "".

For example, the "Attributes" item in the details page of the Notify entity converted by the "Intelligent Speaker Execute Text Directive" action of xiaomi.wifispeaker.s12 siid=5, aiid=5 instance shows the action params as [Text Content(str), Silent Execution(bool)]. A properly formatted input is ["Hello", true].

Specific Conversion

MIoT-Spec-V2 uses URN for defining types. The format is urn:<namespace>:<type>:<name>:<value>[:<vendor-product>:<version>], in which name is a human-readable word or phrase describing the instance of device, service, property, event and action. Xiaomi Home Integration first determines whether to convert the MIoT-Spec-V2 instance into a specific Home Assistant entity based on the instance's name. For the instance that does not meet the specific conversion rules, general conversion rules are used for conversion.

namespace is the namespace of MIoT-Spec-V2 instance. When its value is miot-spec-v2, it means that the specification is defined by Xiaomi. When its value is bluetooth-spec, it means that the specification is defined by Bluetooth Special Interest Group (SIG). When its value is not miot-spec-v2 or bluetooth-spec, it means that the specification is defined by other vendors. If MIoT-Spec-V2 namespace is not miot-spec-v2, a star mark * is added in front of the entity's name .

The conversion follows SPEC_DEVICE_TRANS_MAP.

{
    '<device instance name>':{
        'required':{
            '<service instance name>':{
                'required':{
                    'properties': {
                        '<property instance name>': set<property access: str>
                    },
                    'events': set<event instance name: str>,
                    'actions': set<action instance name: str>
                },
                'optional':{
                    'properties': set<property instance name: str>,
                    'events': set<event instance name: str>,
                    'actions': set<action instance name: str>
                }
            }
        },
        'optional':{
            '<service instance name>':{
                'required':{
                    'properties': {
                        '<property instance name>': set<property access: str>
                    },
                    'events': set<event instance name: str>,
                    'actions': set<action instance name: str>
                },
                'optional':{
                    'properties': set<property instance name: str>,
                    'events': set<event instance name: str>,
                    'actions': set<action instance name: str>
                }
            }
        },
        'entity': str
    }
}

The "required" field under "device instance name" indicates the required services of the device. The "optional" field under "device instance name" indicates the optional services of the device. The "entity" field indicates the Home Assistant entity to be created. The "required" and the "optional" field under "service instance name" are required and optional properties, events and actions of the service respectively. The value of "property instance name" under "required" "properties" field is the access mode of the property. The condition for a successful match is that the value of "property instance name" is a subset of the access mode of the corresponding MIoT-Spec-V2 property instance.

Home Assistant entity will not be created if MIoT-Spec-V2 device instance does not contain all required services, properties, events or actions.

The conversion follows SPEC_SERVICE_TRANS_MAP.

{
    '<service instance name>':{
        'required':{
            'properties': {
                '<property instance name>': set<property access: str>
            },
            'events': set<event instance name: str>,
            'actions': set<action instance name: str>
        },
        'optional':{
            'properties': set<property instance name: str>,
            'events': set<event instance name: str>,
            'actions': set<action instance name: str>
        },
        'entity': str
    }
}

The "required" field under "service instance name" indicates the required properties, events and actions of the service. The "optional" field indicates the optional properties, events and actions of the service. The "entity" field indicates the Home Assistant entity to be created. The value of "property instance name" under "required" "properties" field is the access mode of the property. The condition for a successful match is that the value of "property instance name" is a subset of the access mode of the corresponding MIoT-Spec-V2 property instance.

Home Assistant entity will not be created if MIoT-Spec-V2 service instance does not contain all required properties, events or actions.

The conversion follows SPEC_PROP_TRANS_MAP.

{
    'entities':{
        '<entity name>':{
            'format': set<str>,
            'access': set<str>
        }
    },
    'properties': {
        '<property instance name>':{
            'device_class': str,
            'entity': str
        }
    }
}

The "format" field under "entity name" represents the data format of the property, and matching with one value indicates a successful match. The "access" field under "entity name" represents the access mode of the property, and matching with all values is considered a successful match.

The "entity" field under "property instance name", of which value is one of entity name under "entities" field, indicates the Home Assistant entity to be created. The "device_class" field under "property instance name" indicates the Home Assistant entity's _attr_device_class.

The conversion follows SPEC_EVENT_TRANS_MAP.

{
    '<event instance name>': str
}

The value of the event instance name indicates _attr_device_class of the Home Assistant entity to be created.

MIoT-Spec-V2 Filter

spec_filter.yaml is used to filter out the MIoT-Spec-V2 instance that will not be converted to Home Assistant entity.

The format of spec_filter.yaml is as follows.

<MIoT-Spec-V2 device instance urn without the version field>:
    services: list<service_iid: str>
    properties: list<service_iid.property_iid: str>
    events: list<service_iid.event_iid: str>
    actions: list<service_iid.action_iid: str>

The key of spec_filter.yaml dictionary is the urn excluding the "version" field of the MIoT-Spec-V2 device instance. The firmware of different versions of the same product may be associated with the MIoT-Spec-V2 device instances of different versions. It is required that the MIoT-Spec-V2 instance of a higher version must contain all MIoT-Spec-V2 instances of the lower versions when a vendor defines the MIoT-Spec-V2 of its product on MIoT platform. Thus, the key of spec_filter.yaml does not need to specify the version number of MIoT-Spec-V2 device instance.

The value of "services", "properties", "events" or "actions" fields under "device instance" is the instance id (iid) of the service, property, event or action that will be ignored in the conversion process. Wildcard matching is supported.

Example:

urn:miot-spec-v2:device:television:0000A010:xiaomi-rmi1:
    services:
    - '*'   # Filter out all services. It is equivalent to completely ignoring the device with such MIoT-Spec-V2.
urn:miot-spec-v2:device:gateway:0000A019:xiaomi-hub1:
    services:
    - '3'   # Filter out the siid=3 service.
    properties:
    - '4.*' # Filter out all properties in the siid=4 service.
    events:
    - '4.1' # Filter out the eiid=1 event in the siid=4 service.
    actions:
    - '4.1' # Filter out the aiid=1 action in the siid=4 service.

Device information service (urn:miot-spec-v2:service:device-information:00007801) of all devices will never be converted to Home Assistant entity.

Multiple Language Support

There are 13 languages available for selection in the config flow language option of Xiaomi Home, including Simplified Chinese, Traditional Chinese, English, Spanish, Russian, French, German, Japanese, Italian, Dutch, Portuguese, Brazilian Portuguese, and Turkish. The config flow page in Simplified Chinese and English has been manually reviewed by the developer. Other languages are translated by machine translation or community contributions. If you want to modify the words and sentences in the config flow page, you need to modify the json file of the certain language in custom_components/xiaomi_home/translations/ and custom_components/xiaomi_home/miot/i18n/ directory.

When displaying Home Assistant entity name, Xiaomi Home downloads the multiple language file configured by the device vendor from MIoT Cloud, which contains translations for MIoT-Spec-V2 instances of the device. multi_lang.json is a locally maintained multiple language dictionary, which has a higher priority than the multiple language file obtained from the cloud and can be used to supplement or modify the multiple language translation of devices.

The format of multi_lang.json is as follows.

{
    "<MIoT-Spec-V2 device instance>": {
        "<language code>": {
            "<instance code>": <translation: str>
        }
    }
}

The key of multi_lang.json dictionary is the urn excluding the "version" field of the MIoT-Spec-V2 device instance.

The language code is zh-Hans, zh-Hant, en, es, ru, fr, de, ja, it, nl, pt, pt-BR, or tr, corresponding to the 13 selectable languages mentioned above.

The instance code is the code of the MIoT-Spec-V2 instance, which is in the format of:

service:<siid>                  # service
service:<siid>:property:<piid>  # property
service:<siid>:property:<piid>:valuelist:<index> # The index of a value in the value-list of a property
service:<siid>:event:<eiid>     # event
service:<siid>:action:<aiid>    # action

siid, piid, eiid, aiid and value are all decimal three-digit integers.

Example:

{
    "urn:miot-spec-v2:device:health-pot:0000A051:chunmi-a1": {
        "zh-Hant": {
            "service:002": "養生壺",
            "service:002:property:001": "工作狀態",
            "service:002:property:001:valuelist:000": "待機中",
            "service:002:action:002": "停止烹飪",
            "service:005:event:001": "烹飪完成"
        }
    }
}

If you edit any files in the custom_components/xiaomi_home/miot/specs directory (spec_filter.yaml, spec_modify.yaml, multi_lang.json, etc.) in your Home Assistant, you need to update the entity conversion rule in the integration's CONFIGURE page to take effect. Method: Settings > Devices & services > Configured > Xiaomi Home > CONFIGURE > Update entity conversion rules

Documents

Directory Structure

ha_xiaomi_home (XiaoMi/ha_xiaomi_home)

Searching Database…

这是干什么的?这是 Home Assistant 的官方 Xiaomi Home 集成:它把你的小米 IoT 智能设备接入 Home Assistant,让你在 Home Assistant 里查看和控制这些设备。
它是 Home Assistant 自带的,还是需要我自己添加?它是一个自定义集成,需要你自己添加到已有的 Home Assistant 安装里,不是 HA 核心自带的。它的代码存放在你 HA 配置目录的 custom_components 文件夹中。
使用前需要准备什么——特定版本的 Home Assistant 还是小米账号?需要 Home Assistant Core >= 2024.4.4(或 HA OS >= 13.0),还需要一个小米账号:通过 OAuth 2.0 登录后,集成会从小米云端拉取你的设备列表。上手不需要额外的网关。
我所有的小米设备都能用吗?大多数设备类别都支持,但蓝牙设备、红外设备和虚拟设备不支持。
所有操作都走小米的云,还是可以本地控制设备?默认是走云的:设备状态通过 MQTT 从小米云端传来,指令则通过 HTTP 下发。真正的本地控制需要小米中枢网关,而且只在中国大陆销售;可选的小米局域网控制只支持 IP 设备——连小米自己都不建议使用。
我的小米账号凭据会怎么处理?登录用的是 OAuth 2.0,所以你的密码不会保存在 Home Assistant 里。但令牌、证书和设备信息会以明文写入 HA 的配置文件——请保管好这个文件,如果令牌泄露,可以在米家 App 里撤销。
小米设备在 Home Assistant 里是怎么显示的?集成会读取每个设备的 MIoT-Spec-V2 定义并自动映射:可写属性变成 Switch、Text、Select 或 Number 实体,只读属性变成传感器(Sensor),事件变成 Event 实体,动作变成 Button 或 Notify 实体。
能把几个小米账号或不同地区的设备混在一起用吗?可以——它支持多个小米账号,来自不同账号或地区(中国大陆、欧洲、印度、俄罗斯、新加坡、美国)的设备可以放在同一个 Home Assistant 区域里。每个地区的小米云数据是相互隔离的,所以导入设备时需要选择地区。
MIoT-Spec-V2 是什么?这是小米的 IoT 协议,把每个设备描述成一组包含属性、事件和动作的服务。集成根据它来决定每个功能对应哪种 Home Assistant 实体;不是小米定义的规格会在实体名前面加一个星号标记。
Creator
XiaoMi
Size
1 MB
Stars
22.2K
Age
2y
Commits · 90 Days