返回顶部
h

ha-integration-patternsHome Assistant集成模式

Home Assistant custom integration patterns and architectural decisions. Use when building HACS integrations, custom components, or API bridges for Home Assistant. Covers service response data, HTTP views, storage APIs, and integration architecture.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
1,082
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

ha-integration-patterns

Home Assistant 集成模式

服务响应数据模式

问题

默认情况下,HA 服务是即发即弃的,返回空数组 []。

解决方案(HA 2023.7+)

使用 supports_response 注册服务:

python
from homeassistant.helpers.service import SupportsResponse

hass.services.async_register(
domain,
getfullconfig,
handlegetfull_config,
schema=GETCONFIGSCHEMA,
supports_response=SupportsResponse.ONLY, # ← 关键参数
)

使用 ?return_response 标志调用:
bash
curl -X POST $HAURL/api/services/yourdomain/getfullconfig?return_response

响应处理器

python async def handlegetfull_config(hass: HomeAssistant, call: ServiceCall): 处理服务调用并返回数据。 # ... 你的逻辑 ... return {entities: entitydata, automations: automationdata}

HTTP 视图与服务:何时使用

使用场景使用不使用
返回复杂数据HTTP 视图服务(无响应支持)
即发即弃操作
服务 | HTTP 视图 | | 触发自动化 | 服务 | HTTP 视图 | | 查询状态/配置 | HTTP 视图 | 内部存储 API |

HTTP 视图模式

用于数据检索 API:

python
from homeassistant.components.http import HomeAssistantView

class OpenClawConfigView(HomeAssistantView):
用于检索配置的 HTTP 视图。
url = /api/openclaw/config
name = api:openclaw:config
requires_auth = True

async def get(self, request):
hass = request.app[hass]
config = await getconfigdata(hass)
return json_response(config)

在 async_setup 中注册:

hass.http.register_view(OpenClawConfigView())

关键:避免使用内部 API

切勿使用以下划线为前缀的 API——它们是私有的,会在版本之间发生变化。

错误:
python
storagecollection = hass.data[storage_collection]

正确:
python

仅使用公共 API


from homeassistant.helpers.storage import Store
store = Store(hass, STORAGEVERSION, STORAGEKEY)


存储模式

适用于小数据(设置、缓存)

python from homeassistant.helpers.storage import Store

STORAGEKEY = yourdomain.storage
STORAGE_VERSION = 1

store = Store(hass, STORAGEVERSION, STORAGEKEY)

保存

data = {entities: modified_entities} await store.async_save(data)

加载

data = await store.async_load()

适用于大数据(历史记录、日志)

使用外部数据库或文件存储,而非 HA 存储助手。

需关注的破坏性变更

变更版本迁移方式
对话代理2025.x+直接使用 asyncprocess
服务响应数据
2023.7+ | 添加 supportsresponse 参数 | | 配置条目迁移 | 2022.x+ | 使用 asyncmigrateentry |

始终检查: https://www.home-assistant.io/blog/ 以了解你的目标版本范围。



HACS 集成结构

customcomponents/yourdomain/
├── init.py # asyncsetupentry
├── config_flow.py # UI 配置
├── manifest.json # 依赖项、版本
├── services.yaml # 服务定义
└── storage_services.py # 你的存储逻辑

最小化 manifest.json

json { domain: your_domain, name: Your Integration, codeowners: [@yourusername], config_flow: true, dependencies: [], requirements: [], version: 1.0.0 }

测试清单

  • - [ ] 服务调用返回预期数据(使用 ?return_response)
  • [ ] HTTP 视图可通过认证令牌访问
  • [ ] 未使用以下划线为前缀的 API
  • [ ] 存储数据在重启后持续存在
  • [ ] 配置流创建配置条目
  • [ ] 错误处理返回有意义的消息

文档资源

  • - 集成基础:developers.home-assistant.io/docs/creatingintegrationindex
  • 服务调用:developers.home-assistant.io/docs/dev101services
  • HTTP 视图:developers.home-assistant.io/docs/api/webserver
  • 破坏性变更:home-assistant.io/blog/(按版本筛选)
  • HACS 指南:hacs.xyz/docs/publish/start

经验教训

来自 HA-OpenClaw 桥接尝试:

我们 80% 的问题本可以通过 30-60 分钟的前期文档阅读来发现。我们直接基于假设开始编码,而没有阅读 HA 的实际工作原理。

在开始之前使用 skills/pre-coding-research/ 方法论。

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 ha-integration-patterns-1776420052 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 ha-integration-patterns-1776420052 技能

通过命令行安装

skillhub install ha-integration-patterns-1776420052

下载

⬇ 下载 ha-integration-patterns v1.0.0(免费)

文件大小: 2.76 KB | 发布时间: 2026-4-17 19:59

v1.0.0 最新 2026-4-17 19:59
Initial release of Home Assistant Integration Patterns documentation.

- Provides architectural guidance for Home Assistant custom integrations, HACS components, and API bridges.
- Explains how to return data from services with the new `supports_response` parameter.
- Details when to use HTTP views versus services, with code samples.
- Highlights safe use of storage APIs, avoiding internal/private APIs.
- Includes HACS integration file structure and minimal `manifest.json` example.
- Features a practical testing checklist and documentation links.
- Summarizes key lessons learned from real integration scenarios.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部