-
Notifications
You must be signed in to change notification settings - Fork 4
/
polynest_types.py
executable file
·348 lines (321 loc) · 18.2 KB
/
polynest_types.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from polyglot.nodeserver_api import Node
import nest
import requests
from nest import utils as nest_utils
import sys
from login import USERNAME, PASSWORD
# Globally disable SSL warnings from requests package.
requests.packages.urllib3.disable_warnings()
NEST_STATES = {0: "off", 1: "heat", 2: "cool", 3: "range", 13: "away"}
def myfloat(value, prec=2):
""" round and return float """
return round(float(value), prec)
class NestControl(Node):
def __init__(self, *args, **kwargs):
super(NestControl, self).__init__(*args, **kwargs)
def _discover(self, **kwargs):
try:
manifest = self.parent.config.get('manifest', {})
self.parent.poly.logger.info("Discovering Nest Products...")
self.parent.poly.logger.info("User: %s", USERNAME)
self.napi = nest.Nest(USERNAME,PASSWORD, cache_ttl=30, local_time=True)
for structure in self.napi.structures:
try:
self.parent.poly.logger.info('Structure : %s' % structure.name)
"""
self.parent.poly.logger.info(' Away : %s' % structure.away)
self.parent.poly.logger.info(' Postal Code : %s' % structure.postal_code)
self.parent.poly.logger.info(' Country : %s' % structure.country_code)
self.parent.poly.logger.info(' dr_reminder_enabled : %s' % structure.dr_reminder_enabled)
self.parent.poly.logger.info(' enhanced_auto_away_enabled : %s' % structure.enhanced_auto_away_enabled)
self.parent.poly.logger.info(' eta_preconditioning_active : %s' % structure.eta_preconditioning_active)
self.parent.poly.logger.info(' house_type : %s' % structure.house_type)
self.parent.poly.logger.info(' hvac_safety_shutoff_enabled : %s' % structure.hvac_safety_shutoff_enabled)
self.parent.poly.logger.info(' num_thermostats : %s' % structure.num_thermostats)
self.parent.poly.logger.info(' measurement_scale : %s' % structure.measurement_scale)
self.parent.poly.logger.info(' renovation_date : %s' % structure.renovation_date)
self.parent.poly.logger.info(' structure_area : %s' % structure.structure_area)
"""
except TypeError as e:
self.parent.poly.logger.info('Nestcontrol _discover Caught exception: %s', e)
for device in self.napi.devices:
try:
self.parent.poly.logger.info('Device: %s' % device.serial[-14:])
"""
self.parent.poly.logger.info(' Where: %s' % device.where)
self.parent.poly.logger.info(' Mode : %s' % device.mode)
self.parent.poly.logger.info(' Fan : %s' % device.fan)
self.parent.poly.logger.info(' Temp : %0.1fF' % nest_utils.c_to_f(device.temperature))
self.parent.poly.logger.info(' Humidity : %0.1f%%' % device.humidity)
self.parent.poly.logger.info(' Away Heat: %0.1fF' % nest_utils.c_to_f(device.away_temperature[0]))
self.parent.poly.logger.info(' Away Cool: %0.1fF' % nest_utils.c_to_f(device.away_temperature[1]))
self.parent.poly.logger.info(' hvac_ac_state : %s' % device.hvac_ac_state)
self.parent.poly.logger.info(' hvac_cool_x2_state : %s' % device.hvac_cool_x2_state)
self.parent.poly.logger.info(' hvac_heater_state : %s' % device.hvac_heater_state)
self.parent.poly.logger.info(' hvac_aux_heater_state : %s' % device.hvac_aux_heater_state)
self.parent.poly.logger.info(' hvac_heat_x2_state : %s' % device.hvac_heat_x2_state)
self.parent.poly.logger.info(' hvac_heat_x3_state : %s' % device.hvac_heat_x3_state)
self.parent.poly.logger.info(' hvac_alt_heat_state : %s' % device.hvac_alt_heat_state)
self.parent.poly.logger.info(' hvac_alt_heat_x2_state: %s' % device.hvac_alt_heat_x2_state)
self.parent.poly.logger.info(' hvac_emer_heat_state : %s' % device.hvac_emer_heat_state)
self.parent.poly.logger.info(' online : %s' % device.online)
self.parent.poly.logger.info(' last_ip : %s' % device.last_ip)
self.parent.poly.logger.info(' local_ip : %s' % device.local_ip)
self.parent.poly.logger.info(' last_connection : %s' % device.last_connection)
self.parent.poly.logger.info(' error_code : %s' % device.error_code)
self.parent.poly.logger.info(' battery_level : %s' % device.battery_level)
"""
except TypeError as e:
self.parent.poly.logger.info('Nestcontrol _discover Caught exception: %s', e)
# ISY only allows 14 character limit on nodes, have to strip the serial number down to the last 14 chars.
address = device.serial[-14:].lower()
lnode = self.parent.get_node(address)
if not lnode:
self.parent.poly.logger.info("New Thermostat Found.")
self.parent.thermostats.append(NestThermostat(self.parent, self.parent.get_node('nestcontrol'),
address, device.temperature, structure.name, device.where, manifest))
self.parent.update_config()
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError, TypeError) as e:
self.logger.error('Nestcontrol _discover Caught exception: %s', e)
return True
def query(self, **kwargs):
self.parent.report_drivers()
return True
_drivers = {}
_commands = {'DISCOVER': _discover}
node_def_id = 'nestcontrol'
class NestThermostat(Node):
def __init__(self, parent, primary, address, temperature, structurename, location, manifest=None):
self.parent = parent
self.logger = self.parent.poly.logger
self.structurename = structurename
self.location = location
try:
self.logger.info('Initializing New Thermostat')
self.napi = nest.Nest(USERNAME,PASSWORD, local_time=True)
except requests.exceptions.HTTPError as e:
self.logger.error('NestThermostat __init__ Caught exception: %s', e)
self.away = False
self.online = False
self.insidetemp = nest_utils.c_to_f(temperature)
try:
self.name = 'Nest ' + self.structurename + " " + self.location
except TypeError as e:
self.logger.error('Caught TypeError on structurename or location, which means they don\'t exist. Using Generic name.')
self.name = 'Nest Thermostat'
self.address = address
self.logger.info("Adding new Nest Device: %s Current Temp: %i F", self.name, self.insidetemp)
super(NestThermostat, self).__init__(parent, address, self.name, primary, manifest)
self.update_info()
def update_info(self):
self.away = False
try:
self._checkconnect()
self.logger.info("First structure update: %s", self.napi.structures[0].away)
for structure in self.napi.structures:
if self.structurename == structure.name:
if structure.away:
self.away = True
for device in self.napi.devices:
if self.address == device.serial[-14:].lower():
self.mode = device.mode
if device.fan:
self.set_driver('CLIFS', '1')
else:
self.set_driver('CLIFS', '0')
self.online = device.online
self.humidity = device.humidity
if device.hvac_ac_state:
self.state = '2'
elif device.hvac_heater_state:
self.state = '1'
else:
self.state = '0'
self.insidetemp = int(round(nest_utils.c_to_f(device.temperature)))
try:
self.outsidetemp = int(round(nest_utils.c_to_f(self.napi.structures[0].weather.current.temperature)))
except (TypeError) as e:
self.logger.error('NestThermostat update_info Caught an exception: %s', e)
self.outsidetemp = 0
if self.mode == 'range':
self.targetlow = int(round(nest_utils.c_to_f(device.target[0])))
self.targethigh = int(round(nest_utils.c_to_f(device.target[1])))
self.logger.info("Target Temp is a range between %i F and %i F",
self.targetlow, self.targethigh)
else:
self.targetlow = int(round(nest_utils.c_to_f(device.target)))
self.logger.info('Target Temp is %i F', self.targetlow)
self.targethigh = self.targetlow
# TODO, clean this up into a dictionary or something clever.
self.logger.info("Away %s: Mode: %s InsideTemp: %i F OutsideTemp: %i F TargetLow: %i F TargetHigh: %i F",
self.away, self.mode, self.insidetemp, self.outsidetemp, self.targetlow, self.targethigh)
if self.away:
self.set_driver('CLIMD', '13')
elif self.mode == 'range':
self.set_driver('CLIMD', '3')
elif self.mode == 'heat':
self.set_driver('CLIMD', '1')
elif self.mode == 'cool':
self.set_driver('CLIMD', '2')
elif self.mode == 'fan':
self.set_driver('CLIMD', '6')
else:
self.set_driver('CLIMD', '0')
self.set_driver('ST', int(self.insidetemp))
self.set_driver('CLISPC', self.targethigh)
self.set_driver('CLISPH', self.targetlow)
self.set_driver('CLIHUM', self.humidity)
self.set_driver('CLIHCS', self.state)
self.set_driver('GV2', self.outsidetemp)
self.set_driver('GV4', self.online)
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError) as e:
self.logger.error('NestThermostat update_info Caught exception: %s', e)
return
def _setoff(self, **kwargs):
try:
for device in self.napi.devices:
if self.address == device.serial[-14:].lower():
device.mode = 'off'
except requests.exceptions.HTTPError as e:
self.logger.error('NestThermostat _setoff Caught exception: %s', e)
return True
def _setauto(self, **kwargs):
try:
for device in self.napi.devices:
if self.address == device.serial[-14:].lower():
device.mode = 'range'
except requests.exceptions.HTTPError as e:
self.logger.error('NestThermostat _setauto Caught exception: %s', e)
return True
def _checkconnect(self):
try:
connected = self.napi.devices[0].online
self.logger.info('Connected: %s', connected)
if not connected:
self.napi = nest.Nest(USERNAME,PASSWORD, local_time=True)
return True
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError, TypeError, requests.exceptions.ReadTimeout) as e:
self.logger.error('CheckConnect: %s', e)
return False
def _setmode(self, **kwargs):
try:
val = kwargs.get('value')
if self._checkconnect():
newstate = NEST_STATES[int(val)]
self.logger.info('Got mode change request from ISY. Setting Nest to: %s', newstate)
if newstate == 'away':
for structure in self.napi.structures:
if self.structurename == structure.name:
structure.away = True
else:
for structure in self.napi.structures:
if self.structurename == structure.name:
structure.away = False
self.away = False
for device in self.napi.devices:
if self.address == device.serial[-14:].lower():
device.mode = newstate
self.set_driver('CLIMD', int(val))
#self.update_info()
except requests.exceptions.HTTPError as e:
self.logger.error('NestThermostat _setauto Caught exception: %s', e)
return True
def _setfan(self, **kwargs):
try:
val = int(kwargs.get('value'))
if self._checkconnect():
for device in self.napi.devices:
if self.address == device.serial[-14:].lower():
if val == 1:
device.fan = True
self.logger.info('Got Set Fan command. Setting fan to \'On\'')
else:
device.fan = False
self.logger.info('Got Set Fan command. Setting fan to \'Auto\'')
self.set_driver('CLIFS', val)
except requests.exceptions.HTTPError as e:
self.logger.error('NestThermostat _settemp Caught exception: %s', e)
return True
def _sethigh(self, **kwargs):
inc = False
try:
try:
val = int(kwargs.get('value'))
except TypeError:
inc = True
self._checkconnect()
for device in self.napi.devices:
if self.address == device.serial[-14:].lower():
if device.mode == 'range':
if not inc:
device.temperature = (device.target[0], nest_utils.f_to_c(val))
self.logger.info("Mode is ranged, Setting upper bound to %i F", val)
else:
val = int(nest_utils.c_to_f(device.target[1]) + 1)
self.logger.info("Mode is ranged, incrementing upper bound to %i F", val)
device.temperature = (device.target[0], nest_utils.f_to_c(val))
else:
if not inc:
device.temperature = int(nest_utils.f_to_c(val))
else:
val = int(nest_utils.c_to_f(device.target) + 1)
device.temperature = nest_utils.f_to_c(val)
self.logger.info("Setting temperature to %i F.", val)
self.set_driver('CLISPC', val)
except requests.exceptions.HTTPError as e:
self.logger.error('NestThermostat _settemp Caught exception: %s', e)
return True
def _setlow(self, **kwargs):
inc = False
val = None
try:
try:
val = int(kwargs.get('value'))
except TypeError:
inc = True
self._checkconnect()
for device in self.napi.devices:
if self.address == device.serial[-14:].lower():
if device.mode == 'range':
if not inc:
device.temperature = (nest_utils.f_to_c(val), device.target[1])
self.logger.info("Mode is ranged, Setting lower bound to %i F", val)
else:
val = int(round(nest_utils.c_to_f(device.target[0]) - 1))
self.logger.info("Mode is ranged, decrementing lower bound to %i F", val)
device.temperature = (nest_utils.f_to_c(val), device.target[1])
else:
if not inc:
device.temperature = nest_utils.f_to_c(val)
else:
val = int(round(nest_utils.c_to_f(device.target) - 1))
device.temperature = nest_utils.f_to_c(val)
self.logger.info("Setting temperature to %i F.", val)
self.set_driver('CLISPH', val)
except requests.exceptions.HTTPError as e:
self.logger.error('NestThermostat _settemp Caught exception: %s', e)
return True
def _beep(self, **kwargs):
return True
def query(self, **kwargs):
self.update_info()
self.report_driver()
return True
_drivers = {
'CLIMD': [0, 67, int], 'CLISPC': [0, 14, int],
'CLISPH': [0, 14, int], 'CLIFS':[0, 99, int],
'CLIHUM':[0, 51, int], 'CLIHCS':[0, 66, int],
'GV1': [0, 14, int], 'GV2': [0, 14, int],
'GV3': [0, 14, int], 'GV4': [0, 2, int],
'ST': [0, 14, int]}
_commands = {'DON': _setauto,
'DOF': _setoff,
'CLIMD': _setmode,
'CLIFS': _setfan,
'BRT': _sethigh,
'DIM': _setlow,
'BEEP': _beep,
'CLISPH': _setlow,
'CLISPC': _sethigh,
'QUERY': query}
node_def_id = 'nestthermostat'