mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2026-01-16 23:00:43 +08:00
Compare commits
4 Commits
35f7397c1f
...
04a6b8cfac
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
04a6b8cfac | ||
|
|
11b9559127 | ||
|
|
1e794b7a86 | ||
|
|
ca6fd692ce |
192
CLAUDE.md
Normal file
192
CLAUDE.md
Normal file
@ -0,0 +1,192 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Project Overview
|
||||
|
||||
Xiaomi Home Integration is an official Home Assistant integration for controlling Xiaomi IoT smart devices. It connects to devices via Xiaomi Cloud (MQTT) or locally through Xiaomi Central Hub Gateway. The integration converts MIoT-Spec-V2 device specifications into Home Assistant entities.
|
||||
|
||||
## Development Commands
|
||||
|
||||
### Installation & Setup
|
||||
```bash
|
||||
# Install to Home Assistant config directory
|
||||
./install.sh /path/to/config
|
||||
|
||||
# Install test dependencies
|
||||
pip install pytest pytest-asyncio pytest-dependency zeroconf paho.mqtt psutil cryptography slugify
|
||||
```
|
||||
|
||||
### Testing
|
||||
```bash
|
||||
# Run all tests
|
||||
pytest -v -s -m github ./test/
|
||||
|
||||
# Run specific test files
|
||||
pytest -v -s ./test/test_spec.py
|
||||
pytest -v -s ./test/test_cloud.py
|
||||
pytest -v -s ./test/test_lan.py
|
||||
|
||||
# Check rule format
|
||||
pytest -v -s -m github ./test/check_rule_format.py
|
||||
```
|
||||
|
||||
### Code Quality
|
||||
```bash
|
||||
# Run pylint (follows Google Python Style Guide)
|
||||
pylint $(git ls-files '*.py')
|
||||
|
||||
# Lint specific files
|
||||
pylint custom_components/xiaomi_home/*.py
|
||||
```
|
||||
|
||||
### Validation
|
||||
```bash
|
||||
# HACS validation (run by GitHub Actions)
|
||||
# Uses: hacs/action@main
|
||||
|
||||
# Hassfest validation (run by GitHub Actions)
|
||||
# Uses: home-assistant/actions/hassfest@master
|
||||
```
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
### Core Components (miot/)
|
||||
|
||||
The integration is built around the `miot/` core package:
|
||||
|
||||
**miot_client.py**: Top-level client instance representing a logged-in Xiaomi user. Each user login creates one MIoTClient. Manages authentication, device list, and message routing.
|
||||
|
||||
**miot_cloud.py**: OAuth 2.0 authentication and HTTP API calls to Xiaomi Cloud. Handles token refresh, user info, device control commands, and spec downloads.
|
||||
|
||||
**miot_mips.py**: Message bus (MQTT) for subscribing to device property changes and events. Implements both cloud (MipsCloudClient) and local (MipsLocalClient) message handling.
|
||||
|
||||
**miot_device.py**: Device entity class. Each MIoT device creates multiple MIoTDevice instances (one per Home Assistant entity). Handles property updates, action execution, and event processing.
|
||||
|
||||
**miot_spec.py**: MIoT-Spec-V2 parser. Parses device specifications (URN-based type system) from cloud or local cache. Each spec defines services, properties, events, and actions.
|
||||
|
||||
**miot_lan.py**: Local LAN control for IP devices in same network. Discovery and control without cloud (optional).
|
||||
|
||||
**miot_mdns.py**: mDNS discovery for Xiaomi Central Hub Gateway services.
|
||||
|
||||
**miot_storage.py**: File storage for certificates, device specs, translations, and cached data.
|
||||
|
||||
**miot_network.py**: Network status monitoring and IP address detection.
|
||||
|
||||
**miot_i18n.py**: Multi-language support (13 languages). Manages translations for entity names.
|
||||
|
||||
### Entity Conversion (specs/specv2entity.py)
|
||||
|
||||
MIoT-Spec-V2 instances are converted to Home Assistant entities using three mapping dictionaries:
|
||||
|
||||
- **SPEC_DEVICE_TRANS_MAP**: Whole-device patterns (e.g., vacuum, humidifier, climate)
|
||||
- **SPEC_SERVICE_TRANS_MAP**: Service-level patterns (e.g., battery, air-purifier)
|
||||
- **SPEC_PROP_TRANS_MAP**: Property-level patterns (e.g., temperature, humidity)
|
||||
|
||||
Conversion priority: Device > Service > Property > General rules
|
||||
|
||||
### Spec Customization Files (miot/specs/)
|
||||
|
||||
**spec_filter.yaml**: Filters out MIoT-Spec-V2 instances that should NOT be converted to entities. Uses device URN keys and supports wildcard matching for service/property/event/action IIDs.
|
||||
|
||||
**spec_modify.yaml**: Modifies spec instances before conversion (e.g., changing value ranges, access modes).
|
||||
|
||||
**multi_lang.json**: Local translation overrides with higher priority than cloud translations. Keyed by device URN (without version).
|
||||
|
||||
**spec_add.json**: Additional spec definitions for devices not in cloud database.
|
||||
|
||||
**bool_trans.yaml**: Boolean value translation mappings.
|
||||
|
||||
After editing spec files, you MUST update conversion rules via Integration CONFIGURE page in Home Assistant.
|
||||
|
||||
### Platform Files (custom_components/xiaomi_home/)
|
||||
|
||||
Standard Home Assistant platform files (sensor.py, switch.py, climate.py, etc.) implement entity registration and state management. Each platform imports from miot_device.py and creates entity subclasses.
|
||||
|
||||
**config_flow.py**: Configuration flow for OAuth login and device selection.
|
||||
|
||||
**__init__.py**: Integration setup, entry management, and data structure initialization.
|
||||
|
||||
## MIoT-Spec-V2 Concepts
|
||||
|
||||
**URN Format**: `urn:<namespace>:<type>:<name>:<value>[:<vendor-product>:<version>]`
|
||||
- namespace: miot-spec-v2 (Xiaomi), bluetooth-spec (SIG), or vendor-specific
|
||||
- type: device, service, property, event, action
|
||||
- name: human-readable identifier (used for mapping)
|
||||
|
||||
**IIDs (Instance IDs)**: Decimal identifiers
|
||||
- siid: Service Instance ID
|
||||
- piid: Property Instance ID
|
||||
- eiid: Event Instance ID
|
||||
- aiid: Action Instance ID
|
||||
|
||||
**Instance Code Format**:
|
||||
```
|
||||
service:<siid> # service
|
||||
service:<siid>:property:<piid> # property
|
||||
service:<siid>:property:<piid>:valuelist:<index> # value list item
|
||||
service:<siid>:event:<eiid> # event
|
||||
service:<siid>:action:<aiid> # action
|
||||
```
|
||||
|
||||
## Naming Conventions
|
||||
|
||||
From CONTRIBUTING.md:
|
||||
|
||||
- **Xiaomi**: Always "Xiaomi" in text. Variables: "xiaomi" or "mi"
|
||||
- **Xiaomi Home**: Always "Xiaomi Home" in text. Variables: "mihome" or "MiHome"
|
||||
- **Xiaomi IoT**: Always "MIoT" in text. Variables: "miot" or "MIoT"
|
||||
- **Home Assistant**: Always "Home Assistant" in text. Variables: "hass" or "hass_xxx"
|
||||
|
||||
Mixed Chinese/English: Add space between Chinese and English or use Chinese quotation marks.
|
||||
|
||||
## Commit Message Format
|
||||
|
||||
```
|
||||
<type>: <subject>
|
||||
|
||||
<body>
|
||||
|
||||
<footer>
|
||||
```
|
||||
|
||||
Types: feat, fix, docs, style, refactor, perf, test, chore, revert
|
||||
|
||||
Subject: Imperative, present tense. Not capitalized. No period.
|
||||
|
||||
Body: Detailed description (mandatory except for docs type).
|
||||
|
||||
## Code Style
|
||||
|
||||
Follow [Google Python Style Guide](https://google.github.io/styleguide/pyguide.html). Use the provided `.pylintrc` for linting.
|
||||
|
||||
## Debugging
|
||||
|
||||
Enable debug logging in Home Assistant configuration.yaml:
|
||||
```yaml
|
||||
logger:
|
||||
default: critical
|
||||
logs:
|
||||
custom_components.xiaomi_home: debug
|
||||
```
|
||||
|
||||
## Control Modes
|
||||
|
||||
- **Cloud Control**: MQTT message subscription + HTTP command API
|
||||
- **Local Control**: Via Xiaomi Central Hub Gateway (firmware 3.3.0_0023+) or LAN control (IP devices only, may be unstable)
|
||||
|
||||
Central gateway local control takes priority over LAN control when both are available.
|
||||
|
||||
## Multi-Region Support
|
||||
|
||||
Regions: China (cn), Europe (eu), India (in), Russia (ru), Singapore (sg), USA (us)
|
||||
|
||||
User data is isolated per region. Integration supports multiple regions in same Home Assistant instance.
|
||||
|
||||
## Important Files Location
|
||||
|
||||
- Integration source: `custom_components/xiaomi_home/`
|
||||
- Spec mappings: `custom_components/xiaomi_home/miot/specs/specv2entity.py`
|
||||
- Spec filters: `custom_components/xiaomi_home/miot/specs/spec_filter.yaml`
|
||||
- Translations: `custom_components/xiaomi_home/translations/` and `custom_components/xiaomi_home/miot/i18n/`
|
||||
- Tests: `test/`
|
||||
@ -328,7 +328,7 @@ Device information service (urn:miot-spec-v2:service:device-information:00007801
|
||||
|
||||
## Multiple Language Support
|
||||
|
||||
There are 8 languages available for selection in the config flow language option of Xiaomi Home, including Simplified Chinese, Traditional Chinese, English, Spanish, Russian, French, German, and Japanese. The config flow page in Simplified Chinese and English has been manually reviewed by the developer. Other languages are translated by machine translation. 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.
|
||||
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.
|
||||
|
||||
@ -346,7 +346,7 @@ The format of `multi_lang.json` is as follows.
|
||||
|
||||
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, or ja, corresponding to the 8 selectable languages mentioned above.
|
||||
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:
|
||||
|
||||
|
||||
@ -116,8 +116,8 @@ INTEGRATION_LANGUAGES = {
|
||||
'nl': 'Nederlands',
|
||||
'pt': 'Português',
|
||||
'pt-BR': 'Português (Brasil)',
|
||||
'tr': 'Türkçe',
|
||||
'ru': 'Русский',
|
||||
'tr': 'Türkçe',
|
||||
'zh-Hans': '简体中文',
|
||||
'zh-Hant': '繁體中文'
|
||||
}
|
||||
|
||||
@ -96,6 +96,9 @@ translate:
|
||||
zh-Hant:
|
||||
'false': 假
|
||||
'true': 真
|
||||
tr:
|
||||
'false': Yanlış
|
||||
'true': Doğru
|
||||
contact_state:
|
||||
de:
|
||||
'false': Kein Kontakt
|
||||
@ -133,6 +136,9 @@ translate:
|
||||
zh-Hant:
|
||||
'false': 分離
|
||||
'true': 接觸
|
||||
tr:
|
||||
'false': Temas Yok
|
||||
'true': Temas
|
||||
motion_state:
|
||||
de:
|
||||
'false': Keine Bewegung erkannt
|
||||
@ -170,6 +176,9 @@ translate:
|
||||
zh-Hant:
|
||||
'false': 無人
|
||||
'true': 有人
|
||||
tr:
|
||||
'false': Hareket Algılanmadı
|
||||
'true': Hareket Algılandı
|
||||
open_close:
|
||||
de:
|
||||
'false': Schließen
|
||||
@ -207,6 +216,9 @@ translate:
|
||||
zh-Hant:
|
||||
'false': 關閉
|
||||
'true': 開啟
|
||||
tr:
|
||||
'false': Kapalı
|
||||
'true': Açık
|
||||
yes_no:
|
||||
de:
|
||||
'false': Nein
|
||||
@ -244,3 +256,6 @@ translate:
|
||||
zh-Hant:
|
||||
'false': 否
|
||||
'true': 是
|
||||
tr:
|
||||
'false': Hayır
|
||||
'true': Evet
|
||||
|
||||
@ -330,7 +330,7 @@ urn:miot-spec-v2:device:gateway:0000A019:xiaomi-hub1:
|
||||
|
||||
## 多语言支持
|
||||
|
||||
米家集成配置选项中可选择的集成使用的语言有简体中文、繁体中文、英文、西班牙语、俄语、法语、德语、日语这八种语言。目前,米家集成配置页面的简体中文和英文已经过人工校审,其他语言由机器翻译。如果您希望修改配置页面的词句,则需要修改 `custom_components/xiaomi_home/translations/` 以及 `custom_components/xiaomi_home/miot/i18n/` 目录下相应语言的 json 文件。
|
||||
米家集成配置选项中可选择的集成使用的语言有简体中文、繁体中文、英文、西班牙语、俄语、法语、德语、日语、意大利语、荷兰语、葡萄牙语、巴西葡萄牙语、土耳其语这十三种语言。目前,米家集成配置页面的简体中文和英文已经过人工校审,其他语言由机器翻译或社区贡献。如果您希望修改配置页面的词句,则需要修改 `custom_components/xiaomi_home/translations/` 以及 `custom_components/xiaomi_home/miot/i18n/` 目录下相应语言的 json 文件。
|
||||
|
||||
在显示 Home Assistant 实体名称时,米家集成会从小米云下载设备厂商为设备配置的多语言文件,该文件包含设备 MIoT-Spec-V2 实例的多语言翻译。 `multi_lang.json` 是本地维护的多语言配置字典,其优先级高于从云端获取的多语言文件,可用于补充或修改设备的多语言翻译。
|
||||
|
||||
@ -348,7 +348,7 @@ urn:miot-spec-v2:device:gateway:0000A019:xiaomi-hub1:
|
||||
|
||||
`multi_lang.json` 的键值为 MIoT-Spec-V2 设备实例的 urn (不含版本号“version”字段)。
|
||||
|
||||
language code 为语言代码,取值为 zh-Hans、zh-Hant、en、es、ru、fr、de、ja (对应上述米家集成可选的八种语言)。
|
||||
language code 为语言代码,取值为 zh-Hans、zh-Hant、en、es、ru、fr、de、ja、it、nl、pt、pt-BR、tr(对应上述米家集成可选的十三种语言)。
|
||||
|
||||
instance code 为 MIoT-Spec-V2 实例代码,格式如下:
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user