-
Notifications
You must be signed in to change notification settings - Fork 20
/
sensibo_client.py
55 lines (43 loc) · 1.9 KB
/
sensibo_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import requests
import json
_SERVER = 'https://home.sensibo.com/api/v2'
class SensiboClientAPI(object):
def __init__(self, api_key):
self._api_key = api_key
def _get(self, path, ** params):
params['apiKey'] = self._api_key
response = requests.get(_SERVER + path, params = params)
response.raise_for_status()
return response.json()
def _patch(self, path, data, ** params):
params['apiKey'] = self._api_key
response = requests.patch(_SERVER + path, params = params, data = data)
response.raise_for_status()
return response.json()
def devices(self):
result = self._get("/users/me/pods", fields="id,room")
return {x['room']['name']: x['id'] for x in result['result']}
def pod_measurement(self, podUid):
result = self._get("/pods/%s/measurements" % podUid)
return result['result']
def pod_ac_state(self, podUid):
result = self._get("/pods/%s/acStates" % podUid, limit = 1, fields="acState")
return result['result'][0]['acState']
def pod_change_ac_state(self, podUid, currentAcState, propertyToChange, newValue):
self._patch("/pods/%s/acStates/%s" % (podUid, propertyToChange),
json.dumps({'currentAcState': currentAcState, 'newValue': newValue}))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Sensibo client example parser')
parser.add_argument('apikey', type = str)
parser.add_argument('deviceName', type = str)
args = parser.parse_args()
client = SensiboClientAPI(args.apikey)
devices = client.devices()
print "-" * 10, "devices", "-" * 10
print devices
uid = devices[args.deviceName]
ac_state = client.pod_ac_state(uid)
print "-" * 10, "AC State of %s" % args.deviceName, "_" * 10
print ac_state
client.pod_change_ac_state(uid, ac_state, "on", not ac_state['on'])