1 from __future__
import print_function
10 For Using SwitchBot via official API. 11 Please see https://github.com/OpenWonderLabs/SwitchBotAPI for details. 25 def request(self, method='GET', devices_or_scenes='devices', service_id='', service='', json_body=None):
29 if devices_or_scenes
not in [
'devices',
'scenes']:
30 raise ValueError(
'Please set devices_or_scenes variable devices or scenes')
32 url = os.path.join(self.
_host_domain, devices_or_scenes, service_id, service)
35 response = requests.get(
37 headers={
'Authorization': self.
token}
39 elif method ==
'POST':
40 response = requests.post(
44 'Content-Type':
'application/json; charset=utf8',
45 'Authorization': self.
token 48 raise ValueError(
'Got unexpected http request method. Please use GET or POST.')
50 response_json = response.json()
54 response.raise_for_status()
55 except requests.exceptions.RequestException
as e:
56 if response.status_code == 422:
58 elif response.status_code == 429:
63 if response_json[
'statusCode'] == 100:
65 elif response_json[
'statusCode'] == 151:
67 elif response_json[
'statusCode'] == 152:
69 elif response_json[
'statusCode'] == 160:
71 elif response_json[
'statusCode'] == 161:
73 elif response_json[
'statusCode'] == 171:
75 elif response_json[
'statusCode'] == 190:
78 raise ValueError(
"Got unknown status code : " + str(response_json[
'statusCode']))
83 Update the information of deviceIds, deviceNames, deviceBots. 98 Update the information of sceneIDs, sceneNames. 109 Get the device status. 116 except KeyError
as e:
117 raise KeyError(
"Device name:{} is not registered at switchbot server. Please check the setting.".format(device_name))
119 raise ValueError(
"Please set device_id or device_name.")
121 return self.
request(service_id=device_id, service=
'status')[
'body']
124 def control_device(self, command, parameter='default', command_type='command', device_id=None, device_name=None):
126 Send Command to the device. Please see https://github.com/OpenWonderLabs/SwitchBotAPI#send-device-control-commands for command options. 128 json_body = json.dumps({
130 "parameter": parameter,
131 "commandType": command_type
138 except KeyError
as e:
139 raise KeyError(
"Device name:{} is not registered at switchbot server. Please check the setting.".format(device_name))
141 raise ValueError(
"Please set device_id or device_name.")
143 return self.
request(method=
'POST', service_id=device_id, service=
'commands', json_body=json_body)[
'message']
148 Execute your registered scene. 155 except KeyError
as e:
156 raise KeyError(
"Scene name:{} is not registered at switchbot server. Please check the setting.".format(scene_name))
158 raise ValueError(
"Please set scene_id or scene_name.")
160 return self.
request(method=
'POST', devices_or_scenes=
'scenes', service_id=scene_id, service=
'execute')[
'message']
168 class DeviceTypeError(DeviceError):
170 return(
"Device type is not correct")
174 return(
"The device is not found")
178 return(
"Your command is not supported")
182 return(
"The device is offline now")
186 return(
"The hub device is offline now")
190 return(
"Device internal error due to device states not synchronized with server. Or command format is invalid")
197 class InvalidRequestError(SwitchBotAPIError):
199 return(
"The client has issued an invalid request. This is commonly used to specify validation errors in a request payload.")
203 return(
"The client has exceeded the number of requests allowed for a given time window.")
def control_device(self, command, parameter='default', command_type='command', device_id=None, device_name=None)
def device_status(self, device_id=None, device_name=None)
def execute_scene(self, scene_id=None, scene_name=None)
def update_device_list(self)
def request(self, method='GET', devices_or_scenes='devices', service_id='', service='', json_body=None)
def update_scene_list(self)
def __init__(self, token)