SwitchBot 智能家居控制
通过 Cloud API v1.1 控制 SwitchBot 设备。
首次设置指南:
请让您的用户:
打开 SwitchBot App
进入 Profile(底部右侧)
点击 Preferences(或 Settings)
找到 About → Developer Options
复制 Token 和 Secret Key
mkdir -p ~/.config/switchbot
chmod 700 ~/.config/switchbot
cat > ~/.config/switchbot/credentials.json << 'EOF'
{
"token": "YOUR_TOKEN_HERE",
"secret": "YOUR_SECRET_HERE"
}
EOF
chmod 600 ~/.config/switchbot/credentials.json
运行发现脚本以找到所有设备:
python3 /scripts/switchbot.py list
在发现后,在 TOOLS.md 中记录设备 ID 以便快速参考:
SwitchBot 设备
| 设备 | ID | 类型 |
|--------|-----|------|
| 客厅窗帘 | ABC123 | Curtain3 |
| 卧室灯 | DEF456 | Plug Mini |
使用方法
列出所有设备
python3 /scripts/switchbot.py list
窗帘控制
# 打开窗帘(位置 0 = 全开)
python3 /scripts/switchbot.py curtain open
# 关闭窗帘(位置 100 = 全闭)
python3 /scripts/switchbot.py curtain close
# 设置特定位置(0-100)
python3 /scripts/switchbot.py curtain 50
插座/灯控制
python3 /scripts/switchbot.py plug on
python3 /scripts/switchbot.py plug off
检查传感器状态
python3 /scripts/switchbot.py status
通用命令
python3 /scripts/switchbot.py command [parameter]
支持的设备
设备类型 命令
Curtain / Curtain3 open, close, setPosition
Plug Mini / Plug turnOn, turnOff, toggle
Bot press, turnOn, turnOff
Light / Strip Light turnOn, turnOff, setBrightness, setColor
Lock lock, unlock
Humidifier turnOn, turnOff, setMode
Meter / MeterPlus (只读:温度,湿度)
Hub / Hub Mini / Hub 2 (网关)
错误处理
状态码 含义
100 成功
151 设备离线
152 命令不支持
160 未知命令
161 参数无效
190 内部错误
代理提示
首次交互:如果没有凭证,请引导用户完成设置
设备别名:在 TOOLS.md 中创建友好名称(例如,“客厅”→ 设备 ID)
批量操作:可以顺序控制多个设备
状态检查:在报告传感器读数之前使用状态命令
错误恢复:如果设备离线(151),建议检查 Hub 连接
安全说明
凭证文件应 chmod 600
永远不要记录或显示 token/secret
API 调用通过 HTTPS 发送到 api.switch-bot.com