Open phillipkliewer opened 12 months ago
Here's a python script to turn on 1 device - you'll need to put in the token, secret and MAC address of the device you want to control:
import json
import requests
import time
import hashlib
import hmac
import base64
import uuid
# Declare empty header dictionary
apiHeader = {}
# open token
token = '??????' # copy and paste from the SwitchBot app V6.14 or later
# secret key
secret = '??????????' # copy and paste from the SwitchBot app V6.14 or later
nonce = uuid.uuid4()
t = int(round(time.time() * 1000))
string_to_sign = '{}{}{}'.format(token, t, nonce)
string_to_sign = bytes(string_to_sign, 'utf-8')
secret = bytes(secret, 'utf-8')
sign = base64.b64encode(hmac.new(secret, msg=string_to_sign, digestmod=hashlib.sha256).digest())
print ('Authorization: {}'.format(token))
print ('t: {}'.format(t))
print ('sign: {}'.format(str(sign, 'utf-8')))
print ('nonce: {}'.format(nonce))
#Build api header JSON
apiHeader['Authorization']=token
apiHeader['Content-Type']='application/json'
apiHeader['charset']='utf8'
apiHeader['t']=str(t)
apiHeader['sign']=str(sign, 'utf-8')
apiHeader['nonce']=str(nonce)
url = "https://api.switch-bot.com/v1.1/devices/????????/commands" # Change out to your devices MAC address
data = {
"command": "turnOn",
"parameter": "default",
"commandType": "command"
}
response = requests.post(url, headers=apiHeader, json=data)
print ("Status Code", response.status_code)
print("JSON Response ", response.json())
The name of the device, hardware, or software you would like to control: SwitchBot
What you would like to be able to make it do from Companion: Control all buttons/remotes/etc
Direct links or attachments to the ethernet control protocol or API: https://github.com/OpenWonderLabs/SwitchBotAPI https://www.home-assistant.io/integrations/switchbot/ https://github.com/Danielhiversen/pySwitchbot