name: moltbot-ha
description: 通过 moltbot-ha CLI 控制 Home Assistant 智能家居设备、灯光、场景和自动化,支持可配置的安全确认机制。
homepage: https://github.com/iamvaleriofantozzi/moltbot-ha
metadata: {"moltbot":{"emoji":"🏠","requires":{"bins":["moltbot-ha"],"env":["HA_TOKEN"]},"primaryEnv":"HA_TOKEN","install":[{"id":"uv","kind":"uv","package":"moltbot-ha","bins":["moltbot-ha"],"label":"安装 moltbot-ha (uv 工具)"}]}}
使用 moltbot-ha CLI 工具通过 Home Assistant API 控制您的智能家居。
uv tool install moltbot-ha
moltbot-ha config init
初始化过程会交互式地询问:
- Home Assistant URL (例如:http://192.168.1.100:8123)
- 令牌存储偏好(推荐使用环境变量)
设置您的 Home Assistant 长期访问令牌:
export HA_TOKEN="your_token_here"
创建令牌的步骤:
1. 打开 Home Assistant → 左下角个人资料
2. 滚动到“长期访问令牌”
3. 点击“创建令牌”
4. 复制令牌并设置为 HA_TOKEN 环境变量
moltbot-ha test
moltbot-ha list
moltbot-ha list light
moltbot-ha list switch
moltbot-ha list cover
moltbot-ha state light.kitchen
moltbot-ha state sensor.temperature_living_room
# 开启
moltbot-ha on light.living_room
moltbot-ha on switch.coffee_maker
# 关闭
moltbot-ha off light.bedroom
moltbot-ha off switch.fan
# 切换状态
moltbot-ha toggle light.hallway
# 设置亮度 (百分比)
moltbot-ha set light.bedroom brightness_pct=50
# 设置色温
moltbot-ha set light.office color_temp=300
# 设置多个属性
moltbot-ha set light.kitchen brightness_pct=80 color_temp=350
# 激活场景
moltbot-ha call scene.turn_on entity_id=scene.movie_time
# 设置恒温器温度
moltbot-ha call climate.set_temperature entity_id=climate.living_room temperature=21
# 关闭覆盖物 (百叶窗、车库门)
moltbot-ha call cover.close_cover entity_id=cover.garage
# 带参数调用
moltbot-ha call automation.trigger entity_id=automation.morning_routine
# 带 JSON 数据调用
moltbot-ha call script.turn_on --json '{"entity_id": "script.bedtime", "variables": {"brightness": 10}}'
moltbot-ha 实现了三级安全系统以防止误操作:
关键操作需要明确确认:
- lock.: 门锁
- alarm_control_panel.: 安全警报
- cover.***: 车库门、百叶窗
moltbot-ha on cover.garage
⚠️ 关键操作需要确认
操作:对 cover.garage 执行 turn_on
这是一个需要用户明确批准的关键操作。
请询问用户以确认,然后使用 --force 标志重试。
示例:moltbot-ha on cover.garage --force
代理看到此错误并询问您:
“打开车库门是一个关键操作。您确定要继续吗?”
您确认:
“是的,打开它”
代理使用 --force 重试:
moltbot-ha on cover.garage --force
⚠️ 代理的关键规则:
--force 标志--force某些实体可以在配置中被永久阻止:
[safety]
blocked_entities = ["switch.main_breaker", "lock.front_door"]
这些实体即使使用 --force 也无法控制。
编辑 ~/.config/moltbot-ha/config.toml:
[safety]
level = 3 # 0=禁用,1=仅记录日志,2=确认所有写入操作,3=确认关键操作
critical_domains = ["lock", "alarm_control_panel", "cover"]
blocked_entities = [] # 添加永远不应自动化的实体
allowed_entities = [] # 如果设置,则仅可访问这些实体 (支持通配符)
moltbot-ha on light.bedroom brightness_pct=30
moltbot-ha call cover.open_cover entity_id=cover.bedroom_blinds
moltbot-ha call climate.set_temperature entity_id=climate.bedroom temperature=21
moltbot-ha off light.* # 需要未来版本支持通配符
moltbot-ha call scene.turn_on entity_id=scene.goodnight
moltbot-ha call cover.close_cover entity_id=cover.all_blinds
moltbot-ha state sensor.temperature_living_room
moltbot-ha state sensor.humidity_bathroom
moltbot-ha state binary_sensor.motion_hallway
HA_URL 是否与您的 Home Assistant URL 匹配HA_TOKEN 是否正确设置moltbot-ha list 来发现正确的实体 IDdomain.entity_name (例如:light.kitchen,而不是 Light.Kitchen)如果在 Docker 中运行且无法通过 homeassistant.local 访问 Home Assistant:
- 改用 IP 地址:http://192.168.1.100:8123
- 或者使用 Tailscale 以获得可靠的网状网络
完整配置文件 (~/.config/moltbot-ha/config.toml):
[server]
url = "http://homeassistant.local:8123"
# token = "optional_here_prefer_env_var" # 可选,建议使用环境变量
[safety]
level = 3
critical_domains = ["lock", "alarm_control_panel", "cover"]
blocked_entities = []
allowed_entities = []
[logging]
enabled = true
path = "~/.config/moltbot-ha/actions.log"
level = "INFO"
用户:“我有哪些灯?”
代理:moltbot-ha list light
代理:“您有以下这些灯:light.living_room, light.kitchen, light.bedroom”
用户:“打开客厅的灯”
代理:moltbot-ha on light.living_room
代理:“客厅的灯现已打开”
用户:“打开车库”
代理:moltbot-ha on cover.garage
<收到 CriticalActionError>
代理:“⚠️ 打开车库门是一个关键操作。您确定要继续吗?”
用户:“是的,打开它”
代理:moltbot-ha on cover.garage --force
代理:“车库门正在打开”
~/.config/moltbot-ha/actions.log*) 在 allowed_entities 和 blocked_entities 中受支持--json 标志获得 JSON 输出,便于程序化解析