-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mariia Azbeleva <[email protected]>
- Loading branch information
Showing
10 changed files
with
2,584 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import time | ||
import hmac | ||
import hashlib | ||
import base64 | ||
import requests | ||
import uuid | ||
|
||
class SwitchbotLibrary: | ||
def __init__(self, token, secret): | ||
self.token = token | ||
self.secret = secret | ||
self.base_url = 'https://api.switch-bot.com/v1.1/devices' | ||
self.signature, self.timestamp, self.nonce = self.generate_signature() | ||
self.device_id = '' | ||
|
||
def generate_signature(self): | ||
nonce = str(uuid.uuid4()) | ||
timestamp = int(round(time.time() * 1000)) | ||
message = f'{self.token}{timestamp}{nonce}'.encode('utf-8') | ||
secret_enc = self.secret.encode('utf-8') | ||
signature = base64.b64encode(hmac.new(secret_enc, msg=message, digestmod=hashlib.sha256).digest()).decode('utf-8') | ||
return signature, timestamp, nonce | ||
|
||
def get_devices(self): | ||
headers = { | ||
'Authorization': self.token, | ||
'sign': self.signature, | ||
'nonce': self.nonce, | ||
't': str(self.timestamp), | ||
'Content-Type': 'application/json', | ||
} | ||
response = requests.get(self.base_url, headers=headers) | ||
print(response.json()) | ||
return response.json() | ||
|
||
def get_device_id(self, device_name): | ||
devices_data = self.get_devices() | ||
if 'body' in devices_data and 'deviceList' in devices_data['body']: | ||
for device in devices_data['body']['deviceList']: | ||
if device['deviceName'] == device_name: | ||
return device['deviceId'] | ||
return None | ||
|
||
def send_command(self, command): | ||
headers = { | ||
'Authorization': self.token, | ||
'sign': self.signature, | ||
'nonce': self.nonce, | ||
't': str(self.timestamp), | ||
'Content-Type': 'application/json', | ||
} | ||
url = f'{self.base_url}/{self.device_id}/commands' | ||
response = requests.post(url, headers=headers, json=command) | ||
print(response.json()) | ||
assert response.json()['message'] == 'success' | ||
|
||
def press_button(self, device_name): | ||
self.device_id = self.get_device_id(device_name) | ||
command = { | ||
"command": "press", | ||
"parameter": "default", | ||
"commandType": "command" | ||
} | ||
return self.send_command(command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.