SwitchBot设备监控服务

一个Node.js MCP服务器,用于通过Claude Desktop或Home Assistant监控SwitchBot温度传感器和其他设备。

  • by caseman72
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 caseman72/switchr-mcp

使用说明

切换器MCP服务器

一个Node.js MCP(模型上下文协议)服务器,通过Claude Desktop或Home Assistant公开SwitchBot温度传感器进行监控。

特性

  • 设备发现:自动发现所有SwitchBot设备(Meter、MeterPlus、WoIOSensor、Plug Mini、Bot)
  • 温度监测:从任何传感器读取温度和湿度
  • 能源监测:从Plug Mini设备读取功率、电压和电流(UPS/负载监测)
  • 切换:打开/关闭插头,按下机器人手指模拟器
  • 灵活单位:支持华氏和摄氏温度
  • 设备查找:按ID或昵称查找设备(不区分大小写)
  • 双重运输:支持stdio(克劳德桌面)和HTTP/SSE(家庭助理)
  • 请求日志记录:可选记录所有调试工具调用

安装

cd switchr-mcp
npm install

配置

SwitchBot凭据

SwitchBot API凭据由管理 @caseman72/切换器api 通过 .env.local。在以下位置搜索文件:

  1. 当前工作目录
  2. ~/.config/switchr-api/.env.local
  3. ~/.switchbot.env.local

创建一个 .env.local 包含您的SwitchBot凭据的文件:

SWITCHBOT_TOKEN=your-switchbot-token
SWITCHBOT_SECRET=your-switchbot-secret

访问 SwitchBot开发人员门户 以获取您的API证书。

服务器配置(可选)

复制 config.example.json 到 config.json 要自定义服务器设置,请执行以下操作:

{
  "server": {
    "transport": "stdio",
    "httpPort": 8001,
    "httpHost": "127.0.0.1"
  },
  "devices": {
    "refreshIntervalMinutes": 60
  },
  "monitoring": {
    "enabled": false,
    "logFile": "./switchr-mcp-requests.log"
  }
}

用法

stdio传输(克劳德桌面)

node src/index.js

HTTP传输(家庭助理)

HA自定义组件要求MCP服务器通过HTTP/SSE公开。使用 mcp-proxy 桥接stdio服务器。

安装mcp代理

brew install mcp-proxy

启动代理

# Binds to all interfaces so Docker can reach it
mcp-proxy --port 8082 --host 0.0.0.0 -- node /path/to/switchr-mcp/src/index.js

家庭助理集成

  1. 将自定义组件复制到HA配置目录:

    cp -r custom_components/switchr_mcp ~/.home-assistant/custom_components/
    
  2. 重新启动家庭助理

  3. 添加集成:设置→ 设备和服务→ 添加集成→ “切换器MCP”

  4. 输入连接详细信息:

    • 主持人: host.docker.internal (适用于Docker)或Mac的IP
    • 端口: 8082

创建的实体

  • 温度传感器 (Meter, MeterPlus, WoIOSensor):每个设备一个组合实体,温度为原始值,湿度/电池为属性。
  • 迷你插头:四个传感器加一个开关-- <name> Power W <name> Voltage 五、 <name> Current (mA), <name> Energy (kWh),以及 switch.<name> (开/关控制)。Energy实体通过以下方式在轮询之间集成瞬时电源,并在HA重启期间持续供电 RestoreEntity,因此它可以直接在HA Energy仪表板中使用,而无需Riemann助手。
  • 机器人 (手指模拟器): button.<name>_press --发送瞬时按压(伸出然后缩回)。适用于 pressMode 机器人。

使用launchd自动启动mcp代理

创建 ~/Library/LaunchAgents/com.switchr.mcp-proxy.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.switchr.mcp-proxy</string>
    <key>ProgramArguments</key>
    <array>
        <string>/opt/homebrew/bin/mcp-proxy</string>
        <string>--port</string>
        <string>8082</string>
        <string>--host</string>
        <string>0.0.0.0</string>
        <string>--</string>
        <string>/opt/homebrew/bin/node</string>
        <string>/path/to/switchr-mcp/src/index.js</string>
    </array>
    <key>WorkingDirectory</key>
    <string>/path/to/switchr-mcp</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/tmp/switchr-mcp-proxy.log</string>
    <key>StandardErrorPath</key>
    <string>/tmp/switchr-mcp-proxy.err</string>
</dict>
</plist>

然后加载它:

launchctl load ~/Library/LaunchAgents/com.switchr.mcp-proxy.plist

要停止/卸载:

launchctl unload ~/Library/LaunchAgents/com.switchr.mcp-proxy.plist

管理服务

# Check status
launchctl list | grep switchr

# View logs
tail -f /tmp/switchr-mcp-proxy.err

# Restart
launchctl unload ~/Library/LaunchAgents/com.switchr.mcp-proxy.plist
launchctl load ~/Library/LaunchAgents/com.switchr.mcp-proxy.plist

# Stop
launchctl unload ~/Library/LaunchAgents/com.switchr.mcp-proxy.plist

Claude桌面集成

添加 ~/.claude/claude_desktop_config.json:

{
  "mcpServers": {
    "switchr": {
      "command": "node",
      "args": ["/path/to/switchr-mcp/src/index.js"],
      "env": {}
    }
  }
}

MCP工具

list_devices

列出所有发现的SwitchBot设备。可选过滤器,仅显示温度传感器。

参数:

  • sensorsOnly (可选):如果为真,则仅返回温度传感器(Meter、MeterPlus、WoIOSensor)
  • refresh (可选):从SwitchBot API强制刷新设备列表

get_device_status

获取任何SwitchBot设备的详细状态。返回设备特定的属性,如电源状态、电池电量等。

参数:

  • deviceId:设备ID或设备名称

get_temperature

从特定的SwitchBot温度传感器获取温度和湿度读数。

参数:

  • deviceId:温度传感器的设备ID或设备名称
  • unit (可选):温度单位- F 对于华氏温度(默认), C 摄氏度

答复包括:

  • temperature:请求单位中的当前温度
  • humidity:当前湿度百分比
  • battery:电池电量百分比

get_all_temperatures

一次从所有SwitchBot温度传感器获取温度和湿度读数。

参数:

  • unit (可选):温度单位- F 对于华氏温度(默认), C 摄氏度

get_plug_status

从SwitchBot Plug Mini获取电源状态和能量数据。用于UPS/能源监控。

参数:

  • deviceId:插头的设备ID或设备名称

答复包括:

  • power: "on" 或 "off"
  • voltage:伏特
  • watts:瞬时功率消耗(W)
  • currentMilliamps:电流消耗(mA)
  • electricityOfDay:设备今天的准时蓄能器

get_all_plugs

同时从所有SwitchBot Plug Mini设备获取电源和能量读数。

turn_on / turn_off

打开/关闭SwitchBot Plug Mini(或处于切换模式的Bot)。

参数:

  • deviceId:设备ID或设备名称

press_bot

向SwitchBot Bot(手指模拟器)发送瞬时按压。手指伸出然后缩回。

参数:

  • deviceId:Bot的设备ID或设备名称

get_api_status

获取SwitchBot API速率限制状态。返回剩余调用、重置时间和缓存信息。

答复包括:

  • rate_limit.remaining:剩余API调用
  • rate_limit.reset_by:当速率限制重置时
  • cache.last_refresh:上次刷新设备的时间
  • cache.device_count:发现的设备总数
  • cache.sensor_count:发现温度传感器

请求监控

在config.json中启用请求日志:

{
  "monitoring": {
    "enabled": true,
    "logFile": "./switchr-mcp-requests.log"
  }
}

日志以JSON Lines格式编写,包含时间戳、工具名称、参数和结果。

许可证

麻省理工学院