-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensibo.py
249 lines (213 loc) · 11.2 KB
/
sensibo.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import requests
import json
import math
from meteocalc import Temp, dew_point, heat_index, wind_chill, feels_like
_SERVER = 'https://home.sensibo.com/api/v2'
def to_fahrenheit(refTemp):
return (refTemp * 9/5) + 32
def to_celcius(refTemp):
return (refTemp - 32) * 5 / 9
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_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(
'apikey2', type=str, help='Request an API Key from http://api.openweathermap.org/')
parser.add_argument('deviceName', type=str,
help='Your sensibo device name from home.sensibo.com')
parser.add_argument('cityLat', type=str,
help='Lattitude of the city you live in')
parser.add_argument('cityLon', type=str,
help='Longitude of the city you live in')
parser.add_argument(
'offset', type=float, help='number of degrees C offset from ambient to use', default=0)
parser.add_argument('ReactHot', type=float,
help='Temp in C offset for trigger AC turn On', default=27)
parser.add_argument('ReactOff', type=float,
help='Temp in C offset for trigger Climate Off ', default=22)
parser.add_argument('ReactCold', type=float,
help='Temp in C offset for trigger Heat turn On', default=18)
args = parser.parse_args()
offset = args.offset
cityLat = args.cityLat
cityLon = args.cityLon
ReactHot = args.ReactHot
ReactOff = args.ReactOff
ReactCold = args.ReactCold
f = open("/tmp/sensibo.log", "a+")
f.write(timestamp + "\n")
g = open("/tmp/sensibo.data.log", "a+")
g.write(timestamp + ",")
client = SensiboClientAPI(args.apikey)
weathAPIkey = args.apikey2
devices = client.devices()
print("--------Devices---------")
print(devices)
f.write("--------Devices---------\n")
f.write("Devices {} \n".format(devices))
uid = devices[args.deviceName]
ac_state = client.pod_ac_state(uid)
print("-------------AC State of %s -------------" % args.deviceName)
#targettemp = ac_state['result'][0]['device']['acState']['nativeTargetTemperature']
targettemp = ac_state['result'][0]['device']['acState']['targetTemperature']
targettempUnit = ac_state['result'][0]['device']['acState']['temperatureUnit']
sensibotemp = ac_state['result'][0]['device']['measurements']['temperature']
sensibohumidity = ac_state['result'][0]['device']['measurements']['humidity']
sensibomode = ac_state['result'][0]['acState']['mode']
fanlevel = ac_state['result'][0]['acState']['fanLevel']
power = ac_state['result'][0]['acState']['on']
if (targettempUnit == "F"):
targettemp = ac_state['result'][0]['device']['acState']['nativeTargetTemperature']
# print ac_state
# print(json.dumps(ac_state, indent=4, sort_keys=True))
# client.pod_change_ac_state(uid, ac_state, "on", not ac_state['on'])
# Sensibo moved nativeTargetTemperature out of the acstate structure into device/acstate
# print ac_state['result'][0]['device']['acState']['nativeTargetTemperature']
url = 'https://api.openweathermap.org/data/2.5/onecall?lat=' + cityLat + \
'&lon=' + cityLon + '&units=metric&exclude=hourly,daily&appid=' + weathAPIkey
response = requests.get(url)
response.raise_for_status()
weather = response.json()
outsideTemp = weather['current']['temp']
fahrenheit = (outsideTemp * 9/5) + 32
# create input temperature in different units
t = Temp(sensibotemp, 'c') # c - celsius, f - fahrenheit, k - kelvin
# calculate Heat Index
tRealFeel = heat_index(temperature=t, humidity=sensibohumidity)
RealFeel = tRealFeel.c
# r = requests.get('http://wttr.in/' + cityName + '?format=%t')
# s = r.text[1:-2]
# outsideTemp = float(s)
# fahrenheit = (outsideTemp * 9/5) + 32
g.write("{},{},{},{},{},{:.2f},{:.2f},{:.2f}\n".format(power, sensibotemp,
sensibomode, fanlevel, targettemp, outsideTemp, RealFeel, sensibohumidity))
f.write("--------Temps---------\n")
f.write("Target Temp: {} {}\n".format(
targettemp, to_fahrenheit(targettemp)))
f.write("Sensibo Power On: {} Temp: {} Humidity: {:.2f} % RealFeel: {:.2f} State: {} Fan: {}\n".format(
power, sensibotemp, sensibohumidity, RealFeel, sensibomode, fanlevel))
f.write("Outside Temp: {}C /{}F\n".format(outsideTemp,
to_fahrenheit(outsideTemp)))
print("--------Temps---------\n")
print("Target Temp: {} {}\n".format(targettemp, to_fahrenheit(targettemp)))
print("Sensibo Power On: {} Temp: {} Humidity: {:.2f} % RealFeel: {:.2f} State: {} Fan: {}\n".format(
power, sensibotemp, sensibohumidity, RealFeel, sensibomode, fanlevel))
print("Outside Temp: {}C /{}F\n".format(outsideTemp, to_fahrenheit(outsideTemp)))
f.write("--------Analysis---------\n")
if (False == power): # climate react Onlogic
if (outsideTemp > targettemp) and (RealFeel > ReactHot) and ("cool" == sensibomode):
print("Climate react [AC ON] Outside air {} Warmer than target {} temp".format(
outsideTemp, targettemp))
print("Climate react [AC ON] Inside air {} Warmer than ClimateReact {} temp".format(
RealFeel, ReactHot))
f.write("Climate react [AC ON] Outside air {} Warmer than target {} temp \n".format(
outsideTemp, targettemp))
f.write("Climate react [AC ON] Inside air {} Warmer than ClimateReact {} temp \n".format(
RealFeel, ReactHot))
client.pod_change_ac_state(uid, ac_state, "on", True)
power = True
if (outsideTemp < targettemp) and (sensibotemp < ReactCold) and ("heat" == sensibomode):
print("Climate react [Heat ON] Outside air {} Colder than target {} temp".format(
outsideTemp, targettemp))
print("Climate react [Heat ON] Inside air {} Colder than ClimateReact {} temp".format(
RealFeel, ReactCold))
f.write("Climate react [Heat ON] Outside air {} Colder than target {} temp \n".format(
outsideTemp, targettemp))
f.write("Climate react [Heat ON] Inside air {} Colder than ClimateReact {} temp\n".format(
RealFeel, ReactCold))
client.pod_change_ac_state(uid, ac_state, "on", True)
power = True
else: # Power On Climate React Off
if (sensibotemp < ReactOff) and ("cool" == sensibomode):
print("Climate react [AC ON] Inside air {} Colder than target {} temp".format(
RealFeel, ReactOff))
f.write("Climate react [AC ON] Inside air {} Colder than target {} temp {}\n".format(
RealFeel, ReactOff))
client.pod_change_ac_state(uid, ac_state, "on", False)
power = False
if (sensibotemp > targettemp) and ("heat" == sensibomode):
print("Climate react [Heat ON] Inside air {} Warmer than target {} temp".format(
sensibotemp, targettemp))
f.write("Climate react [Heat ON] Inside air {} Warmer than target {} temp\n".format(
sensibotemp, targettemp))
client.pod_change_ac_state(uid, ac_state, "on", False)
power = False
# regular logic for fan control
if (power == True):
if ("cool" == sensibomode):
if (outsideTemp + offset < targettemp):
print("[AC Off] Outside air {} plus offset {} lower than target {} temp".format(
outsideTemp, offset, targettemp))
f.write("[AC Off] Outside air {} plus offset {} lower than target {} temp".format(
outsideTemp, offset, targettemp))
client.pod_change_ac_state(uid, ac_state, "on", False)
if (RealFeel > targettemp):
if ("auto" != fanlevel):
print(
"Fan is not Auto, Interior temp {:.2f} too high raising fan".format(RealFeel))
f.write(
"Fan is not Auto, Interior temp {:.2f} too high raising fan\n".format(RealFeel))
client.pod_change_ac_state(
uid, ac_state, "fanLevel", "auto")
else: # Heating Mode
if (outsideTemp > targettemp):
print("[Heat Off] Outside air {} higher than target {} temp".format(
outsideTemp, targettemp))
f.write("[Heat Off] Outside air {} higher than target {} temp\n".format(
outsideTemp, targettemp))
client.pod_change_ac_state(uid, ac_state, "on", False)
if (RealFeel < targettemp):
if ("auto" != fanlevel):
print(
"Fan is not auto, Interior temp {:.2f} too low raising fan".format(RealFeel))
f.write(
"Fan is not auto, Interior temp {:.2f} too low raising fan\n".format(RealFeel))
client.pod_change_ac_state(
uid, ac_state, "fanLevel", "auto")
if (outsideTemp < RealFeel):
print("Inside {:.2f} hotter than Outside {}".format(
RealFeel, outsideTemp))
else:
print("Outside {} hotter than Inside {:.2f}".format(outsideTemp, RealFeel))
# Below has never happened
if (RealFeel == targettemp) and (power == True):
print("Target Temp {} reached. Reducing Fan".format(targettemp))
client.pod_change_ac_state(uid, ac_state, "fanLevel", "low")
f.write("Target Temp reached reducing fan\n")