Switchbotをプログラムでコントロールする方法

Switchbot Token

c1a7ec9840d74dabe8ae000a27565174c544b7f0a1ef818169fe62a43207a2aafb44ffc912fd9be40b8819f7a9c35c40

Client Secret

1e164427348ff23eb67dfb1ac2e9c5b5

Postmanで、デバイス一覧からデバイスIDを取得する

1.WebAPI

https://api.switch-bot.com/v1.0/devices

2.headers

key

Authorization

value

c1a7ec9840d74dabe8ae000a27565174c544b7f0a1ef818169fe62a43207a2aafb44ffc912fd9be40b8819f7a9c35c40

3.curl

curl -X GET \
  https://api.switch-bot.com/v1.0/devices \
  -H 'authorization: c1a7ec9840d74dabe8ae000a27565174c544b7f0a1ef818169fe62a43207a2aafb44ffc912fd9be40b8819f7a9c35c40' \
  -H 'cache-control: no-cache' \
  -H 'postman-token: 6d704b01-def8-b2a0-c064-cff3f442309b'

4.return values

{
	"statusCode": 100,
	"body": {
		"deviceList": [
			{
				"deviceId": "6055F9213DB6",
				"deviceName": "ツールライト",
				"deviceType": "Plug Mini (JP)",
				"enableCloudService": true,
				"hubDeviceId": "000000000000"
			},
			{
				"deviceId": "6055F92188F2",
				"deviceName": "会議システム",
				"deviceType": "Plug Mini (JP)",
				"enableCloudService": true,
				"hubDeviceId": "000000000000"
			},
			{
				"deviceId": "6055F923826A",
				"deviceName": "照明1",
				"deviceType": "Plug Mini (JP)",
				"enableCloudService": true,
				"hubDeviceId": "000000000000"
			},
			{
				"deviceId": "84F703B4268E",
				"deviceName": "モニタースピーカー",
				"deviceType": "Plug Mini (JP)",
				"enableCloudService": true,
				"hubDeviceId": "000000000000"
			},
			{
				"deviceId": "FBD2F4204699",
				"deviceName": "工場のハブミニ",
				"deviceType": "Hub Mini",
				"hubDeviceId": "000000000000"
			},
			{
				"deviceId": "FE4A3CA272C9",
				"deviceName": "工場の温湿度計",
				"deviceType": "Meter",
				"enableCloudService": true,
				"hubDeviceId": "FBD2F4204699"
			}
		],
		"infraredRemoteList": [
			{
				"deviceId": "02-202112061436-27743848",
				"deviceName": "工場のモニター",
				"remoteType": "TV",
				"hubDeviceId": "FBD2F4204699"
			},
			{
				"deviceId": "02-202112061440-67816937",
				"deviceName": "工場のエアコン",
				"remoteType": "Air Conditioner",
				"hubDeviceId": "FBD2F4204699"
			},
			{
				"deviceId": "02-202112061500-22277780",
				"deviceName": "工場のタイマー",
				"remoteType": "Others",
				"hubDeviceId": "FBD2F4204699"
			},
			{
				"deviceId": "02-202112061707-47436045",
				"deviceName": "工場のスピーカー",
				"remoteType": "Others",
				"hubDeviceId": "FBD2F4204699"
			},
			{
				"deviceId": "02-202205120851-98869948",
				"deviceName": "BALMUDA",
				"remoteType": "Others",
				"hubDeviceId": "FBD2F4204699"
			}
		]
	},
	"message": "success"
}

5.STREAM DECK

APIを呼び出すためのアドオンアプリをインストールしておく。

  • API Request

右のメニュー一覧から「API Request」(最下部)をクリック

下記の項目を入力する

・Request Type

POST

・Data

{"command": "turnOn","parameter": "default","commandType": "command"}

※スイッチをONにする場合のJSONデータ

curl --request POST 'https://api.switch-bot.com/v1.0/devices/ccc/commands' \
  --header 'Authorization: c1a7ec9840d74dabe8ae000a27565174c544b7f0a1ef818169fe62a43207a2aafb44ffc912fd9be40b8819f7a9c35c40' \
  --header 'Content-Type: application/json; charset=utf8' \
  --data-raw '{
    "commandType": "command",
    "command": "turnOn",
    "parameter": "default"
  }'