-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermo.py
executable file
·179 lines (149 loc) · 5.61 KB
/
thermo.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
#!/usr/bin/env python
import requests
import json
from pprint import pprint
from collections import namedtuple
import os
base_nest_url = "https://smartdevicemanagement.googleapis.com/v1/"
Thermostat = namedtuple('Thermostat', ['name', 'temp', 'target_temp', 'mode', 'id'])
#def fetch_token():
# payload = {
# "client_id": client_id,
# "client_secret": client_secret,
# "code": code,
# "grant_type": "authorization_code",
# "redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
# }
#
# rval = requests.post("https://ioauth2.googleapis.com/token", data=payload)
# print(rval)
class TempUtils:
@staticmethod
def f_to_c(tin):
return (tin - 32) * .55
@staticmethod
def c_to_f(tin):
return (tin * 1.8) + 32
TARGET_COOL = TempUtils.c_to_f(22.78) # 73
LIMIT_COOL = TempUtils.c_to_f(20.55) # 69
TARGET_HEAT = TempUtils.c_to_f(20) # 68
LIMIT_HEAT = TempUtils.c_to_f(22.22) # 72
class NestCredentialsException(Exception):
pass
class NestCredentials():
'''
class to read credentials
'''
def __init__(self, path="~/.nest/config"):
self.path = path
self.project_id = None
self.client_id = None
self.client_secret = None
self.refresh_token = None
self.load_credentials()
def load_credentials(self):
path = os.path.expanduser(self.path)
if not os.path.exists(path):
print("The credentials file is missing.")
raise NestCredentialsException("CredentialFileMissing")
data = None
with open(path) as fd:
data = json.loads(fd.read())
if not data:
raise NestCredentialsException("Error loading credentials file.")
self.refresh_token = data['refresh_token']
self.client_secret = data['client_secret']
self.project_id = data['project_id']
self.client_id = data['client_id']
class NestThermostat():
'''
simple class to control a nest thermostat using google's APIs
'''
def __init__(self, creds, target_cool=TARGET_COOL, target_heat=TARGET_HEAT):
self.target_cool = target_cool
self.target_heat = target_heat
self.creds = creds
self.access_token = self._refresh_access_token()
def _refresh_access_token(self):
payload = {
"client_id": self.creds.client_id,
"client_secret": self.creds.client_secret,
"refresh_token": self.creds.refresh_token,
"grant_type": "refresh_token",
}
headers = {"Content-Type": "application/x-www-form-urlencoded"}
rval = requests.post("https://oauth2.googleapis.com/token", data=payload, headers=headers)
if rval.status_code != 200:
raise f"Error refreshing token: {rval.status_code} {rval.reason}"
return rval.json()['access_token']
def set_temp(self, dev):
'''
set target temperature of thermostat
POST /enterprises/project-id/devices/device-id:executeCommand
{
"command" : "sdm.devices.commands.ThermostatTemperatureSetpoint.SetHeat",
"params" : {
"heatCelsius" : 22.0
}
}
'''
if dev.mode == 'COOL':
mode = "coolCelsius"
command = "SetCool"
temp = TempUtils.f_to_c(self.target_cool)
else:
mode = "heatCelsius"
command = "SetHeat"
temp = TempUtils.f_to_c(self.target_heat)
data = {
"params": {
mode: temp,
},
"command": f"sdm.devices.commands.ThermostatTemperatureSetpoint.{command}",
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.access_token}",
}
url = base_nest_url + dev.id
url += ":executeCommand"
rval = requests.post(url, data=json.dumps(data), headers=headers)
if rval.status == 200:
print(f"Success: Temparature set to {TempUtils.c_to_f(temp)}")
if rval.status_code != 200:
raise f"Error refreshing token: {rval.status_code} {rval.reason}"
return rval.json()
def get(self, nest_type="structures"):
'''
get method to fetch structures or devices
'''
url = base_nest_url + f"enterprises/{self.creds.project_id}/{nest_type}"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.access_token}",
}
rval = requests.get(url, headers=headers)
import pdb; pdb.set_trace()
return rval.json()
def create_device(dev):
'''
create an easier device object
'''
dname = dev['parentRelations'][0]['displayName']
temp = TempUtils.c_to_f(dev['traits']['sdm.devices.traits.Temperature']['ambientTemperatureCelsius'])
goal_temp = TempUtils.c_to_f(dev['traits']['sdm.devices.traits.ThermostatTemperatureSetpoint']['coolCelsius'])
mode = dev['traits']['sdm.devices.traits.ThermostatMode']['mode']
did = dev['name']
return Thermostat(dname, temp=temp, target_temp=goal_temp, mode=mode, id=did)
def main():
creds = NestCredentials()
nt = NestThermostat(creds)
results = nt.get(nest_type="devices")
for dev in results['devices']:
dev = create_device(dev)
print("{name}\t [MODE: {mode}] is set to {target_temp:.2f}. Currently: {temp:.2f}".format(**dev._asdict()))
if dev.mode == 'COOL' and dev.target_temp < LIMIT_COOL and False:
print(f"Setting temperature to {self.target_cool}")
set_temp(dev)
if __name__ == '__main__':
main()