ha-integration-knowledge
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFile Locations
文件位置
- Integration code:
./homeassistant/components/<integration_domain>/ - Integration tests:
./tests/components/<integration_domain>/
- 集成代码:
./homeassistant/components/<integration_domain>/ - 集成测试:
./tests/components/<integration_domain>/
General guidelines
通用准则
- When looking for examples, prefer integrations with the platinum or gold quality scale level first.
- Polling intervals are NOT user-configurable. Never add scan_interval, update_interval, or polling frequency options to config flows or config entries.
- Do NOT allow users to set config entry names in config flows. Names are automatically generated or can be customized later in UI. Exception: helper integrations may allow custom names.
- For entity actions and entity services, avoid requesting redundant defensive checks for fields already enforced by Home Assistant validation schemas and entity filters; only request extra guards when values bypass validation or are transformed unsafely.
- When validation guarantees a key is present, prefer direct dictionary indexing () over
data["key"]so invalid assumptions fail fast..get("key") - Integrations should be thin wrappers. Protocol parsing, device state machines, or other domain logic belong in a separate PyPI library, not in the integration itself. If unsure, ask before inlining.
- Integrations should not implement fixes or workarounds for limitations in libraries. Instead, the library should be updated to fix the issue.
The following platforms have extra guidelines:
- Diagnostics: for diagnostic data collection
platform-diagnostics.md - Repairs: for user-actionable repair issues
platform-repairs.md
- 查找示例时,优先选择达到铂金或黄金质量等级的集成。
- 轮询间隔不可由用户配置。切勿在配置流程或配置项中添加scan_interval、update_interval或轮询频率选项。
- 不允许用户在配置流程中设置配置项名称。名称会自动生成,或之后可在UI中自定义。例外情况:辅助集成可允许自定义名称。
- 对于实体操作和实体服务,避免对Home Assistant验证模式和实体过滤器已强制要求的字段进行冗余的防御性检查;仅当值绕过验证或被不安全转换时,才需要额外的防护。
- 当验证保证某个键存在时,优先使用直接字典索引()而非
data["key"],这样错误的假设会快速暴露。.get("key") - 集成应作为轻量包装器。协议解析、设备状态机或其他领域逻辑应放在独立的PyPI库中,而非集成本身。若不确定,先咨询再内联实现。
- 集成不应为库的局限性实现修复或变通方法。相反,应更新库来解决问题。
以下平台有额外准则:
- 诊断:用于诊断数据收集
platform-diagnostics.md - 修复:用于用户可执行的修复问题
platform-repairs.md
Entity platforms
实体平台
- Ensure and
async_added_to_hass()have symmetrical behavior. For example, if a subscription is created inasync_will_remove_from_hass(), it should be unsubscribed inasync_added_to_hass(). Also, if something is torn down inasync_will_remove_from_hass(), it should be set up inasync_will_remove_from_hass().async_added_to_hass()
- 确保和
async_added_to_hass()的行为对称。例如,若在async_will_remove_from_hass()中创建了订阅,则应在async_added_to_hass()中取消订阅。同样,若在async_will_remove_from_hass()中销毁了某些内容,则应在async_will_remove_from_hass()中进行设置。async_added_to_hass()
Integration Quality Scale
集成质量等级
- When validating the quality scale rules, check them at https://developers.home-assistant.io/docs/core/integration-quality-scale/rules
- When implementing or reviewing an integration, always consider the quality scale rules, since they promote best practices.
Template scale file:
./script/scaffold/templates/integration/integration/quality_scale.yaml- 验证质量等级规则时,请访问 https://developers.home-assistant.io/docs/core/integration-quality-scale/rules 进行查看
- 实现或评审集成时,始终考虑质量等级规则,因为它们能推广最佳实践。
模板等级文件:
./script/scaffold/templates/integration/integration/quality_scale.yamlHow Rules Apply
规则应用方式
- Check : Look for
manifest.jsonkey to determine integration level"quality_scale" - Bronze Rules: Always required for any integration with quality scale
- Higher Tier Rules: Only apply if integration targets that tier or higher
- Rule Status: Check in integration folder for:
quality_scale.yaml- : Rule implemented
done - : Rule doesn't apply (with reason in comment)
exempt - : Rule needs implementation
todo
- 检查:查找
manifest.json键以确定集成等级"quality_scale" - 青铜规则:所有带有质量等级的集成都必须遵守
- 更高等级规则:仅当集成目标为该等级或更高时才适用
- 规则状态:查看集成文件夹中的以了解:
quality_scale.yaml- :规则已实现
done - :规则不适用(注释中需说明原因)
exempt - :规则需要实现
todo
Testing Requirements
测试要求
- Tests should avoid interacting or mocking internal integration details. For more info, see https://developers.home-assistant.io/docs/development_testing/#writing-tests-for-integrations
- 测试应避免交互或模拟集成内部细节。更多信息请查看 https://developers.home-assistant.io/docs/development_testing/#writing-tests-for-integrations