SwitchBot OpenAPI 控制:物联网自动化 - Openclaw Skills

作者:互联网

2026-04-18

AI教程

什么是 SwitchBot OpenAPI 控制?

SwitchBot OpenAPI 技能使 AI 代理能够使用官方 v1.1 API 与 SwitchBot 生态系统直接交互。通过在 Openclaw Skills 中利用此集成,开发人员可以通过编程方式控制智能设备、检索实时传感器数据并执行复杂的家庭自动化序列。

该技能通过提供优化的 Node.js 和 Bash 脚本自动处理身份验证和签名生成,消除了手动计算 HMAC 签名的复杂性。它充当您的 AI 编码代理与物理硬件之间的桥梁,允许在开发人员友好的环境中进行无缝设备管理。

下载入口:https://github.com/openclaw/skills/tree/main/skills/woan1105/switchbot-openapi

安装与下载

1. ClawHub CLI

从源直接安装技能的最快方式。

npx clawhub@latest install switchbot-openapi

2. 手动安装

将技能文件夹复制到以下位置之一

全局模式 ~/.openclaw/skills/ 工作区 /skills/

优先级:工作区 > 本地 > 内置

3. 提示词安装

将此提示词复制到 OpenClaw 即可自动安装。

请帮我使用 Clawhub 安装 switchbot-openapi。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。

SwitchBot OpenAPI 控制 应用场景

  • 通过语音或聊天命令自动化家庭任务,如拉开窗帘或开灯。
  • 监控 SwitchBot 传感器的温度和湿度等环境条件。
  • 通过检查门锁状态或远程锁定/解锁门来管理家庭安全。
  • 通过 API 执行在 SwitchBot 应用中创建的多设备场景。
  • 将智能家居数据集成到更大的数据分析或生产力仪表板中。
SwitchBot OpenAPI 控制 工作原理
  1. AI 代理使用存储在环境变量中的 API 令牌和密钥向 SwitchBot OpenAPI 进行身份验证。
  2. 该技能为每个请求生成唯一的 HMAC-SHA256 签名,以确保 API 要求的安全通信。
  3. 代理查询设备端点以映射可用硬件并识别特定设备功能。
  4. 通过 POST 请求向 v1.1 端点发送 turnOn、press 或 setPosition 等命令到特定设备 ID。
  5. 该技能处理响应代码,并为不支持直接命令调用的设备提供场景执行回退。

SwitchBot OpenAPI 控制 配置指南

要在 Openclaw Skills 库中开始使用此技能,您必须首先在网关或容器中配置环境变量:

export SWITCHBOT_TOKEN='your_api_token'
export SWITCHBOT_SECRET='your_api_secret'
export SWITCHBOT_REGION='global' # 选项:global, na, eu, jp

通过运行设备列表脚本测试连接:

scripts/list_devices.sh

注意:请确保您的 SwitchBot 设备在移动应用中已启用云服务,并已连接到 SwitchBot 中控枢纽。

SwitchBot OpenAPI 控制 数据架构与分类体系

该技能按照 SwitchBot OpenAPI v1.1 规范管理和解释数据:

参数 描述
deviceId 每个硬件单元的唯一标识符
deviceType 设备类别(例如 Hub、Bot、Curtain、Lock、Plug)
hubDeviceId 云端控制所需的父级 Hub ID
status 包含电池、温度和功率在内的实时状态数据
command 要执行的具体动作(例如 press, setPosition, setTemperature)
commandType 标准操作通常设置为 command
sign 用于 API 安全的计算出的 HMAC-SHA256 签名
name: switchbot-openapi
description: Control and query SwitchBot devices using the official OpenAPI (v1.1). Use when the user asks to list SwitchBot devices, get device status, or send commands (turn on/off, press, set mode, lock/unlock, set temperature, curtain open %, etc.). Requires SWITCHBOT_TOKEN and SWITCHBOT_SECRET.

SwitchBot OpenAPI Skill

This skill equips the agent to operate SwitchBot devices via HTTPS requests to the official OpenAPI. It includes ready-to-run scripts and curl templates; use these instead of re-deriving the HMAC signature each time.

Quick Start (Operator)

  1. Set environment variables in the OpenClaw Gateway/container:
  • SWITCHBOT_TOKEN: your OpenAPI token
  • SWITCHBOT_SECRET: your OpenAPI secret
  • SWITCHBOT_REGION (optional): default global (api.switch-bot.com). Options: global, na, eu, jp.
  1. Test a call (list devices):
  • Bash: scripts/list_devices.sh
  • Node: node scripts/switchbot_cli.js list
  1. Common tasks:
  • Get a device status: node scripts/switchbot_cli.js status
  • Turn on: node scripts/switchbot_cli.js cmd turnOn
  • Turn off: node scripts/switchbot_cli.js cmd turnOff
  • Press (bot): node scripts/switchbot_cli.js cmd press
  • Curtain 50%: node scripts/switchbot_cli.js cmd setPosition --pos 50
  • Lock/Unlock (Lock): node scripts/switchbot_cli.js cmd lock / unlock

API Notes (concise)

Base URL by region:

  • global: https://api.switch-bot.com
  • na: https://api.switch-bot.com
  • eu: https://api.switch-bot.com
  • jp: https://api.switch-bot.com

Use path prefix /v1.1.

Headers (required):

  • Authorization:
  • sign: HMAC-SHA256 of (token + timestamp + nonce) using SECRET, Base64-encoded
  • t: milliseconds epoch as string
  • nonce: random UUID
  • Content-Type: application/json

Key endpoints:

  • GET /v1.1/devices
  • GET /v1.1/devices/{deviceId}/status
  • POST /v1.1/devices/{deviceId}/commands
    • body: { "command": "turnOn|turnOff|press|lock|unlock|setPosition|setTemperature|setMode|setVolume", "parameter": "", "commandType": "command" }
  • Scenes (fallback when a model has no public commands):
    • GET /v1.1/scenes
    • POST /v1.1/scenes/{sceneId}/execute

Notes on limitations:

  • Some models (e.g., certain Robot Vacuum lines) do NOT expose direct commands in OpenAPI v1.1. When a command returns {statusCode:160, message:"unknown command"}, create a Scene in the SwitchBot app (e.g., "Vacuum Start") and execute it via the Scenes API.

For command parameters, see references/commands.md. Scenes usage examples are in references/examples.md.

How the Agent Should Use This Skill

  • Prefer running the provided scripts. They compute signatures and handle retries.
  • Preflight guard: the CLI checks device capabilities before sending commands. For Bluetooth-class devices (e.g., Bot/Lock/Curtain), it requires enableCloudService=true and a non-empty hubDeviceId. If missing, it aborts with a clear fix (bind a Hub and enable Cloud Services in the SwitchBot app).
  • If environment variables are missing, ask the user to provide/define them securely (do not log secrets).
  • For sensitive actions (e.g., unlock), require explicit confirmation and optionally a one-time code if the user enables it.
  • On errors with code 190/TokenInvalid or 100/Unauthorized: re-check token/secret, time drift, or signature composition.

Files

  • scripts/switchbot_cli.js — Node CLI for list/status/commands
  • scripts/list_devices.sh — curl listing
  • scripts/get_status.sh — curl status
  • scripts/send_command.sh — curl command
  • references/commands.md — parameters for common devices
  • references/examples.md — example invocations and JSON outputs

Keep this SKILL.md lean; consult references/ for details.

相关推荐