高木のブログ

SwitchBot のスマートロックを API で操作する

2022/07/24

SwitchBotのスマートロックを買ったので、APIから操作をサクッと試してみた記録

手順

ドキュメント: SwitchBotAPI/README.md at main · OpenWonderLabs/SwitchBotAPI

APIキーは公式アプリから取得できる。(「SwitchBotのスマート加湿器をAPIで操作する」に書いた)

デバイスを探す(スマートロックのdevice IDを取得)

$ curl -s -X GET -H 'Authorization: token' https://api.switch-bot.com/v1.0/devices | jq '.body.deviceList[] | select(.deviceType == "Smart Lock")'
{
  "deviceId": "EX0DEVICE0ID",
  "deviceName": "ロック A8",
  "deviceType": "Smart Lock",
  "enableCloudService": true,
  "hubDeviceId": "EX0HUB0DEVICE"
}

たくさんデバイスが出てくるので、deviceTypeで絞り込みをする

enableCloudServiceがfalseになっている場合、アプリからクラウドサービスを有効にしておく

ステータスを確認する

$ curl -s -X GET -H 'Authorization: token' https://api.switch-bot.com/v1.0/devices/EX0DEVICE0ID/status | jq
{
  "statusCode": 100,
  "body": {
    "deviceId": "EX0DEVICE0ID",
    "deviceType": "Smart Lock",
    "hubDeviceId": "EX0HUB0DEVICE",
    "lockState": "locked",
    "doorState": "closed",
    "calibrate": true
  },
  "message": "success"
}

解錠・施錠する

セキュリティの都合か何かでドキュメントに書いてなかったけど、それっぽいコマンドを叩いたらできた(やる時は自己責任で)

解錠

$ curl -s -X POST -H 'Authorization: token' https://api.switch-bot.com/v1.0/devices/EX0DEVICE0ID/commands -H 'Content-Type: application/json' -d '{"command": "unlock","parameter": "default","commandType": "command"}' | jq
{
  "statusCode": 100,
  "body": {},
  "message": "success"
}

施錠

$ curl -s -X POST -H 'Authorization: token' https://api.switch-bot.com/v1.0/devices/EX0DEVICE0ID/commands -H 'Content-Type: application/json' -d '{"command": "lock","parameter": "default","commandType": "command"}' | jq
{
  "statusCode": 100,
  "body": {},
  "message": "success"
}

SNS でシェアする


ytkg

Written by ytkg, Twitter, GitHub