-
Notifications
You must be signed in to change notification settings - Fork 0
/
historybuilder.py
82 lines (61 loc) · 2.81 KB
/
historybuilder.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 _jget(self, path, ** params):
params['apiKey'] = self._api_key
response = requests.get(_SERVER + path, params = params)
return response
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_timer(self, podUid):
result = self._get("/pods/%s/timer" % podUid)
return result['result']
def pod_ac_state(self, podUid):
response = self._jget("/pods/%s/acStates" % podUid, limit = 1, fields="acState,device")
response.raise_for_status()
return response.json()
def pod_historical(self, podUid, ndays):
response = self._jget("/pods/%s/historicalMeasurements" % podUid, days="%d" % ndays)
response.raise_for_status()
return response.json()
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
import time
timestamp = time.asctime( time.localtime(time.time()) )
parser = argparse.ArgumentParser(description='Sensibo client parser')
parser.add_argument('apikey', type = str, help='Request an API Key from home.sensibo.com')
parser.add_argument('deviceName', type = str, help='Your sensibo device name from home.sensibo.com')
parser.add_argument('days', type = int, help='Number of days of History to grab')
args = parser.parse_args()
days=args.days
g = open("/tmp/sensibo.data.log","a+")
g.write("datetime,power,sensibotemp,sensibomode,fanlevel,targettemp,outsideTemp\n")
g.write(timestamp + ",")
client = SensiboClientAPI(args.apikey)
devices = client.devices()
print "-" * 10, "devices", "-" * 10
print devices
uid = devices[args.deviceName]
history = client.pod_historical(uid,days)
print "-" * 10, "AC State of %s" % args.deviceName, "_" * 10
print history