Home Assistant 集成#
1.
Gateway 平台 — 通过 WebSocket 订阅实时状态变更并响应事件
2.
智能家居工具 — 四个可供 LLM 调用的工具,通过 REST API 查询和控制设备
1. 创建长期访 问令牌#
2. 配置环境变量#
设置 HASS_TOKEN 后,homeassistant 工具集将自动启用。Gateway 平台和设备控制工具均通过这一个令牌激活。
3. 启动 Gateway#
Home Assistant 将作为已连接平台出现,与其他消息平台(Telegram、Discord 等)并列显示。可用工具#
Hermes Agent 注册了四个智能家居控制工具:ha_list_entities#
列出 Home Assistant 实体,可按域(domain)或区域(area)过滤。domain (可选) — 按实体域过滤:light、switch、climate、sensor、binary_sensor、cover、fan、media_player 等。
area (可选) — 按区域/房间名称过滤(与友好名称匹配):living room、kitchen、bedroom 等。
List all lights in the living room
ha_get_state#
获取单个实体的详细状态,包括所有属性(亮度、颜色、温度设定值、传感器读数等)。entity_id (必填) — 要查询的实体,例如 light.living_room、climate.thermostat、sensor.temperature
What's the current state of climate.thermostat?
ha_list_services#
列出可用于设备控制的服务(操作)。显示每种设备类型可执行的操作及其接受的参数。domain (可选) — 按域过滤,例如 light、climate、switch
What services are available for climate devices?
ha_call_service#
调用 Home Assistant 服务以控制设备。domain (必填) — 服务域:light、switch、climate、cover、media_player、fan、scene、script
service (必填) — 服务名称:turn_on、turn_off、toggle、set_temperature、set_hvac_mode、open_cover、close_cover、set_volume_level
entity_id (可选) — 目标实体,例如 light.living_room
data (可选) — 以 JSON 对象形式传入的附加参数
Turn on the living room lights
→ ha_call_service(domain="light", service="turn_on", entity_id="light.living_room")
Set the thermostat to 22 degrees in heat mode
→ ha_call_service(domain="climate", service="set_temperature",
entity_id="climate.thermostat", data={"temperature": 22, "hvac_mode": "heat"})
Set living room lights to blue at 50% brightness
→ ha_call_service(domain="light", service="turn_on",
entity_id="light.living_room", data={"brightness": 128, "color_name": "blue"})
Gateway 平台:实时事件#
Home Assistant gateway 适配器通过 WebSocket 连接并订阅 state_changed 事件。当设备状态发生变更且符合过滤条件时,该事件将作为消息转发给 agent。事件过滤#
默认情况下,不转发任何事件。您必须配置 watch_domains、watch_entities 或 watch_all 中的至少一项才能接收事件。若未设置过滤器,启动时将记录警告日志,所有状态变更将被静默丢弃。
在 ~/.hermes/config.yaml 中,于 Home Assistant 平台的 extra 部分配置 agent 接收的事件:| 设置 | 默认值 | 说明 |
|---|
watch_domains | (无) | 仅监听这些实体域(例如 climate、light、binary_sensor) |
watch_entities | (无) | 仅监听这些特定实体 ID |
watch_all | false | 设为 true 以接收所有状态变更(不推荐用于大多数场景) |
ignore_entities | (无) | 始终忽略这些实体(在域/实体过滤器之前应用) |
cooldown_seconds | 30 | 同一实体两次事件之间的最小间隔秒数 |
从一组精简的域开始 — climate、binary_sensor 和 alarm_control_panel 已覆盖最常用的自动化场景。按需添加更多域。使用 ignore_entities 屏蔽 CPU 温度或运行时间计数器等噪声传感器。
事件格式化#
| 域 | 格式 |
|---|
climate | "HVAC mode changed from 'off' to 'heat' (current: 21, target: 23)" |
sensor | "changed from 21°C to 22°C" |
binary_sensor | "triggered" / "cleared" |
light、switch、fan | "turned on" / "turned off" |
alarm_control_panel | "alarm state changed from 'armed_away' to 'triggered'" |
| (其他) | "changed from 'old' to 'new'" |
Agent 响应#
Agent 发出的消息将以 Home Assistant 持久通知的形式推送(通过 persistent_notification.create),标题为"Hermes Agent",显示在 HA 通知面板中。连接管理