mcpField
H
home-assistant

home-assistant

github.com/sundial-org其他67249 次安装34 周前更新
代码仓库

关于 home-assistant

home-assistant 是通过 Home Assistant REST API 和 Webhook 控制智能家居的技能,面向需要查询设备状态、调用设备服务或接收自动化事件的用户。它也提供 `ha.sh` 命令行封装。

主要功能

  1. 01查询实体及其状态
  2. 02控制灯、开关和其他设备
  3. 03触发脚本与自动化
  4. 04激活场景
  5. 05通过 Webhook 接收事件
  6. 06使用 `ha.sh` 命令行操作

适用场景

  1. 01查看 Home Assistant 中的实体和状态
  2. 02远程控制灯光、温控或媒体设备
  3. 03触发回家脚本或自动化
  4. 04接收运动检测等自动化事件

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/sundial-org/awesome-openclaw-skills --skill home-assistant

安装后在 Claude Code 中输入/home-assistant确认已加载。

附带文件references/api.mdscripts/ha.sh

详细介绍

Home Assistant

通过 Home Assistant 的 REST API 和 Webhook 控制智能家居。

设置

选项 1:配置文件(推荐)

创建 ~/.config/home-assistant/config.json:

{
  "url": "https://your-ha-instance.duckdns.org",
  "token": "your-long-lived-access-token"
}
选项 2:环境变量
export HA_URL="http://homeassistant.local:8123"
export HA_TOKEN="your-long-lived-access-token"
获取长期访问令牌
  1. 打开 Home Assistant → 配置文件(左下角)
  2. 向下滚动到“长期访问令牌”
  3. 点击“创建令牌”,为其命名(例如“Clawdbot”)
  4. 立即复制令牌(仅显示一次)

快速参考

列出实体
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"
控制设备
# Turn on
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"}'

# Turn off
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"}'

# Set brightness (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}'
运行脚本和自动化
# Trigger script
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"}'

# Trigger automation
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
lightturn_on、turn_off、togglelight.kitchen
switchturn_on、turn_off、toggleswitch.fan
climateset_temperature、set_hvac_modeclimate.thermostat
coveropen_cover、close_cover、stop_covercover.garage
media_playerplay_media、media_pause、volume_setmedia_player.tv
sceneturn_onscene.relax
scriptturn_onscript.welcome_home
automationtrigger、turn_on、turn_offautomation.sunrise

入站 Webhook(HA → Clawdbot)

要接收来自 Home Assistant 自动化的事件:

1. 创建带 Webhook 操作的 HA 自动化
# In HA automation
action:
  - service: rest_command.notify_clawdbot
    data:
      event: motion_detected
      area: living_room
2. 在 HA 中定义 REST 命令
# 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 }}"}'
3. 在 Clawdbot 中处理

Clawdbot 会接收 Webhook,并可根据事件通知你或采取操作。

CLI 封装脚本

scripts/ha.sh CLI 提供了便捷方式,可访问所有 HA 功能:

# Test connection
ha.sh info

# List entities
ha.sh list all          # all entities
ha.sh list lights       # just lights
ha.sh list switch       # just switches

# Search entities
ha.sh search kitchen    # find entities by name

# Get/set state
ha.sh state light.living_room
ha.sh states light.living_room   # full details with attributes
ha.sh on light.living_room
ha.sh on light.living_room 200   # with brightness (0-255)
ha.sh off light.living_room
ha.sh toggle switch.fan

# Scenes & scripts
ha.sh scene movie_night
ha.sh script goodnight

# Climate
ha.sh climate climate.thermostat 22

# Call any service
ha.sh call light turn_on '{"entity_id":"light.room","brightness":200}'

故障排除

  • 401 未授权:令牌已过期或无效。请生成新令牌。
  • 连接被拒绝:检查 HA_URL,确保 HA 正在运行且可访问。
  • 未找到实体:列出实体以查找正确的 entity_id。

API 参考

如需高级用法,请参阅 references/api.md。

评论

支持 Markdown