名称: home-assistant
描述: 通过 Home Assistant 控制智能家居设备、运行自动化任务并接收 Webhook 事件。适用于控制灯光、开关、气候、场景、脚本或任何 HA 实体。支持通过 REST API(出站)和 Webhook(来自 HA 自动化的入站触发)进行双向通信。
元数据: {"clawdbot":{"emoji":"🏠","requires":{"bins":["jq","curl"]}}}
通过 Home Assistant 的 REST API 和 Webhook 控制您的智能家居。
创建 ~/.config/home-assistant/config.json:
{
"url": "https://your-ha-instance.duckdns.org",
"token": "your-long-lived-access-token"
}
export HA_URL="http://homeassistant.local:8123"
export HA_TOKEN="your-long-lived-access-token"
curl -s -H "Authorization: Bearer $HA_TOKEN" "$HA_URL/api/states" | jq '.[].entity_id'
curl -s -H "Authorization: Bearer $HA_TOKEN" "$HA_URL/api/states/light.living_room"
# 开启
curl -X POST -H "Authorization: Bearer $HA_TOKEN" -H "Content-Type: application/json" \
"$HA_URL/api/services/light/turn_on" -d '{"entity_id": "light.living_room"}'
# 关闭
curl -X POST -H "Authorization: Bearer $HA_TOKEN" -H "Content-Type: application/json" \
"$HA_URL/api/services/light/turn_off" -d '{"entity_id": "light.living_room"}'
# 设置亮度 (0-255)
curl -X POST -H "Authorization: Bearer $HA_TOKEN" -H "Content-Type: application/json" \
"$HA_URL/api/services/light/turn_on" -d '{"entity_id": "light.living_room", "brightness": 128}'
# 触发脚本
curl -X POST -H "Authorization: Bearer $HA_TOKEN" "$HA_URL/api/services/script/turn_on" \
-H "Content-Type: application/json" -d '{"entity_id": "script.goodnight"}'
# 触发自动化
curl -X POST -H "Authorization: Bearer $HA_TOKEN" "$HA_URL/api/services/automation/trigger" \
-H "Content-Type: application/json" -d '{"entity_id": "automation.motion_lights"}'
curl -X POST -H "Authorization: Bearer $HA_TOKEN" "$HA_URL/api/services/scene/turn_on" \
-H "Content-Type: application/json" -d '{"entity_id": "scene.movie_night"}'
| 领域 | 服务 | 示例 entity_id |
|---|---|---|
light |
turn_on, turn_off, toggle |
light.kitchen |
switch |
turn_on, turn_off, toggle |
switch.fan |
climate |
set_temperature, set_hvac_mode |
climate.thermostat |
cover |
open_cover, close_cover, stop_cover |
cover.garage |
media_player |
play_media, media_pause, volume_set |
media_player.tv |
scene |
turn_on |
scene.relax |
script |
turn_on |
script.welcome_home |
automation |
trigger, turn_on, turn_off |
automation.sunrise |
接收来自 Home Assistant 自动化的事件:
# 在 HA 自动化中
action:
- service: rest_command.notify_clawdbot
data:
event: motion_detected
area: living_room
# configuration.yaml
rest_command:
notify_clawdbot:
url: "https://your-clawdbot-url/webhook/home-assistant"
method: POST
headers:
Authorization: "Bearer {{ webhook_secret }}"
Content-Type: "application/json"
payload: '{"event": "{{ event }}", "area": "{{ area }}"}'
Clawdbot 接收 Webhook,并可根据事件通知您或采取相应操作。
scripts/ha.sh CLI 工具提供了访问所有 HA 功能的简便方式:
# 测试连接
ha.sh info
# 列出实体
ha.sh list all # 所有实体
ha.sh list lights # 仅灯光
ha.sh list switch # 仅开关
# 搜索实体
ha.sh search kitchen # 按名称查找实体
# 获取/设置状态
ha.sh state light.living_room
ha.sh states light.living_room # 包含属性的完整详情
ha.sh on light.living_room
ha.sh on light.living_room 200 # 带亮度设置 (0-255)
ha.sh off light.living_room
ha.sh toggle switch.fan
# 场景与脚本
ha.sh scene movie_night
ha.sh script goodnight
# 气候控制
ha.sh climate climate.thermostat 22
# 调用任意服务
ha.sh call light turn_on '{"entity_id":"light.room","brightness":200}'
高级用法请参阅 references/api.md。