Closed4

SwitchBot API を使う作業ログ

mamixmamix

やりたいこと

  • API で電気のオンオフをする

環境

  • ラズパイ4
mamixmamix

アクセストークンを取得する

iPhoneのアプリから、プロフィール > 設定 を開く。
「アプリバージョン」を連打すると、「開発者向けオプション」が表示される。
「開発者向けオプション」を選択し、アクセストークンを取得する。

mamixmamix
import json, requests
header = {"Authorization": "xxxxxxxxxxxxx"}

response = requests.get("https://api.switch-bot.com/v1.0/devices", headers=header)
devices = json.loads(response.text)

devices

{'statusCode': 100,
'body': {'deviceList': [{'deviceId': 'xxxxxxxx',
'deviceName': 'Hub Mini D0',
'deviceType': 'Hub Mini',
'hubDeviceId': '000000000000'},
{'deviceId': 'xxxxxxxx',
'deviceName': 'ランプのスイッチ',
'deviceType': 'Bot',
'enableCloudService': False,
'hubDeviceId': '000000000000'}],
'infraredRemoteList': [{'deviceId': '02-xxxxxxxx-xxxxxxxx',
'deviceName': '電気',
'remoteType': 'Light',
'hubDeviceId': 'xxxxxxxx'},
{'deviceId': '02-xxxxxxxx-xxxxxxxx',
'deviceName': 'ライト',
'remoteType': 'Light',
'hubDeviceId': 'xxxxxxxx'}]},
'message': 'success'}

deviceid_light = "02-xxxxxxxx-xxxxxxxx"
command_on =  json.dumps({
    "command": "turnOn",
    "commandType": "command"
})
device_on = requests.post("https://api.switch-bot.com/v1.0/devices/"+deviceid_light+"/commands", command_on, headers=header)

json.loads(device_on.text)

{'statusCode': 100, 'body': {}, 'message': 'success'}

deviceid_lamp = "xxxxxxxx"
command_on = json.dumps({
    "command": "turnOn",
    "commandType": "command"
})
command_off = json.dumps({
    "command": "turnOff",
    "commandType": "command"
})

# ON
device_on = requests.post("https://api.switch-bot.com/v1.0/devices/"+deviceid_lamp+"/commands", command_on, headers=header)
json.loads(device_on.text)

# OFF
device_off = requests.post("https://api.switch-bot.com/v1.0/devices/"+deviceid_lamp+"/commands", command_off, headers=header)
json.loads(device_off.text)

{'statusCode': 100, 'body': {}, 'message': 'success'}

mamixmamix

aibo API と組み合わせる

import requests
import random
import sys
import time
import datetime
import json
import config

# aibo config
deviceId = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
#headers = config.headers
headers = {'Authorization' : 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
}
TIME_OUT_LIMIT =10

# switchbot config
swb_headers = {"Authorization": "xxxxxxxxxxxxxxxxxxxxxxxxx"}
swb_url = "https://api.switch-bot.com/v1.0/devices/"

# swb lamp on/off
deviceid_lamp = "xxxxxxxxxxxx" #device_id
lamp_on = json.dumps({
    "command": "turnOn",
    "commandType": "command"
})
lamp_off = json.dumps({
    "command": "turnOff",
    "commandType": "command"
})

def aibo_lamp_control(lamp_command):
    post_url = 'https://public.api.aibo.com/v1/devices/' + deviceId + '/capabilities/' + 'play_motion' +'/execute'
    data = '{"arguments":{"Category":"beckon","Mode":"BODY_LEFT"}}'
    
    #POST API
    response = requests.post(post_url, headers=headers, data=data)
    post_result = json.loads(response.text)
    executionID = post_result["executionId"]
    print(post_result)
    time.sleep(8)
    # SWB API
    device_command = requests.post(swb_url+deviceid_lamp+"/commands", lamp_command, headers=swb_headers)
    json.loads(device_command.text)
    
    
    #GET API
    get_result_url = 'https://public.api.aibo.com/v1/executions/' + executionID
    TimeOut = 0
    while True:
        response = requests.get(get_result_url, headers=headers)
        get_result = json.loads(response.text)
        get_status = get_result["status"]

        if get_status == "SUCCEEDED":
            print(get_result)
            break
        elif get_status == "FAILED":
            print(get_result)
            break

        TimeOut += 1
        if TimeOut > TIME_OUT_LIMIT:
            print("Time out")
            break

        time.sleep(1)
    
aibo_lamp_control(lamp_on)

{'executionId': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'status': 'ACCEPTED', 'result': {}}
{'executionId': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'status': 'SUCCEEDED', 'result': None}

このスクラップは2021/05/09にクローズされました