控制 Home Assistant 智能家居设备,运行自动化,接收 webhook 事件。用于控制灯光、开关、空调、场景、脚本等。
家庭助理 : 通过家庭助理的 REST API 和 Webhoks 控制您的智能家庭是一项面向实际任务的技能,主要用于Setup. 备选案文1: 配置文件( 建议). 创建 ~/.config/ home-assistant/ config.json : $: 1, 备选案文2: 环境Va。它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。
若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;
涉及批量任务时,还应保存进度,避免中断后重复操作。该技能适合用于一次性任务,也可以接入自动化工作流,与其他技能或上层代理配合完成更完整的业务链路;在组合使用时,应明确每一步的输入输出关系,并避免不同步骤之间出现参数冲突。
通过 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"}'
| Domain | Service | Example 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 提供对所有 Home Assistant 功能的便捷访问:
# 测试连接
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}'
HA_URL 配置是否正确,并确认 Home Assistant 正在运行且可正常访问。entity_id。高级用法请参阅 references/api.md。
相关专题
热门下载
相关下载
精品课程
共6课时 | 54.7万人学习
共89课时 | 134.1万人学习
共49课时 | 82.6万人学习
最新文章