SuperAPI AI 工具集

ha-entity-platforms

编程与开发

查看项目

更新于 10-15 23:48

技能介绍

用途:帮助在 Home Assistant 自定义集成中创建实体平台(sensor、binary_sensor、switch、light、cover、climate、button、number、select 等),适用于新增实体或扩展集成时使用。

主要能力:

  1. 平台搭建模式:每个平台实现 async_setup_entry,从 config entry 的 runtime_data 取得 coordinator,再按设备与描述批量生成实体并调用 async_add_entities。
  2. EntityDescription 模式:用 dataclass 声明式定义传感器描述,设置 key、translation_key、device_class、state_class、native_unit_of_measurement 以及 value_fn 取值函数。
  3. 基础实体类:继承 CoordinatorEntity,设置 _attr_has_entity_name,通过 device_info 提供稳定 identifiers 与设备名称、厂商、型号、固件版本,并用 available 处理设备数据缺失。
  4. 具体平台实现:传感器用 native_value 返回取值函数结果;开关用 is_on 与 async_turn_on/async_turn_off,把库错误转换为 HomeAssistantError,并在命令后调用 async_request_refresh()。
  5. 平台速查表:列出各平台基类、关键属性与常见设备类。

使用方式:按资料提供的模式实现 async_setup_entry、EntityDescription、基础实体类与具体平台类;完整设备类参考见 reference/device-classes.md。

关键规则:始终设置 _attr_has_entity_name = True;始终提供稳定的 unique_id;始终提供带稳定 identifiers 的 device_info;名称使用 translation_key 而非硬编码 _attr_name;数值传感器添加 state_class(MEASUREMENT、TOTAL、TOTAL_INCREASING);使用 native_unit_of_measurement 而非 unit_of_measurement;命令后调用 async_request_refresh()。

限制:资料未提供安装命令、版本要求、收费信息或适用 Home Assistant 版本,暂未提供。