-
Notifications
You must be signed in to change notification settings - Fork 0
/
NestThermostat.py
99 lines (84 loc) · 3.33 KB
/
NestThermostat.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
from nest_helpers import *
from NestDataPoint import NestDataPoint
import time
class NestThermostat:
UPDATE_INTERVAL = 120
def get_inside_temp(self, use_c=False):
key = 'ambient_temperature_c' if use_c else 'ambient_temperature_f'
return self.get_thermo_value(key)
def get_target_temp(self, use_c=False):
key = 'target_temperature_c' if use_c else 'target_temperature_f'
return self.get_thermo_value(key)
def get_humidity(self):
return self.get_thermo_value('humidity')
def get_state(self):
raw_value = self.get_thermo_value('hvac_state')
if raw_value == 'off':
return NestDataPoint.STATE_OFF
elif raw_value == 'heating':
return NestDataPoint.STATE_HEAT
elif raw_value == 'cooling':
return NestDataPoint.STATE_COOL
def get_mode(self):
raw_value = self.get_thermo_value('hvac_mode')
if raw_value == 'off':
return NestDataPoint.MODE_OFF
elif raw_value == 'heat':
return NestDataPoint.MODE_HEAT
elif raw_value == 'cool':
return NestDataPoint.MODE_COOL
elif raw_value == 'eco':
return NestDataPoint.MODE_ECO
def get_fan_status(self):
raw_value = self.get_thermo_value('fan_timer_active')
if raw_value:
return NestDataPoint.FAN_ON
else:
return NestDataPoint.FAN_OFF
def get_away_status(self):
structure_id = self.get_thermo_value('structure_id')
raw_value = self.data['structures'][structure_id]['away']
if raw_value == 'away':
return NestDataPoint.AWAY
elif raw_value == 'home':
return NestDataPoint.HOME
def get_thermo_value(self, key):
thermo_data = self.get_thermostat_data()
if key in thermo_data:
return thermo_data[key]
else:
raise KeyError("'{}' not found in thermostat data".format(key))
def get_thermostat_data(self, id=None):
try:
if id is None:
id = list(self.data['devices']['thermostats'].keys())[0]
return self.data['devices']['thermostats'][id]
except:
return None
def update_data(self):
self.data = read_data(self.token)
self.last_ret_time = round(time.time())
def get_outside_temp(self):
weather_data = get_weather_data(self.zipcode)
temp = weather_data['main']['temp']
return round(temp) # Rounding because data from Nest is less accurate
def get_datapoint(self):
self.update_data()
dp = NestDataPoint(self.last_ret_time, self.get_inside_temp(), self.get_target_temp(), self.get_humidity(),
self.get_away_status(), self.get_fan_status(), self.get_mode(), self.get_state(),
self.get_outside_temp())
return dp
def __init__(self, zipcode, lambda_id=None, token=None):
if token is None:
if lambda_id is None:
self.token = get_token()
else:
self.token = get_token_from_dynamo(lambda_id)
else:
self.token = token
if self.token is None:
raise Exception("Token is not defined")
self.last_ret_time = 0
self.data = {}
self.update_data()
self.zipcode = zipcode