-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnest.py
More file actions
305 lines (272 loc) · 13 KB
/
Copy pathnest.py
File metadata and controls
305 lines (272 loc) · 13 KB
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
"""
nest.py
The Nest module for porter, the Prometheus exporter.
Uses the (new) Google Smart Device Management API.
See https://developers.google.com/nest/device-access/api/thermostat
"""
import json, prometheus_client, requests, time, threading
#from dateutil.parser import isoparse
from prometheus_client.core import GaugeMetricFamily
REQUEST_TIME = prometheus_client.Summary('nest_processing_seconds',
'time of nest requests')
class NestError(Exception):
pass
class NestClient:
OAUTH_PREFIX = 'https://www.googleapis.com/oauth2/v4/token?grant_type=refresh_token'
API_PREFIX = 'https://smartdevicemanagement.googleapis.com/v1/enterprises/'
def __init__(self, config):
self.config = config
self.cv = threading.Condition()
self.accesstokens = {}
nconfig = config.get('nest')
if not nconfig:
raise Exception('no nest configuration')
if not nconfig.get('projectid'):
raise Exception('no nest projectid')
if not nconfig.get('clientid'):
raise Exception('no nest clientid')
if not nconfig.get('clientsecret'):
raise Exception('no nest clientsecret')
if not nconfig.get('credentials'):
raise Exception('no nest credentials')
if not nconfig.get('timeout'):
nconfig['timeout'] = 10
def _fetch_token_json(self, user):
# holding self.cv
nconfig = self.config['nest']
timeout = nconfig['timeout']
refresh_token = nconfig['credentials'].get(user)
if refresh_token is None:
raise NestError(f'no credentials for {user}')
resp = requests.post(f'{self.OAUTH_PREFIX}&client_id={nconfig["clientid"]}&client_secret={nconfig["clientsecret"]}&refresh_token={refresh_token}', timeout=timeout)
resp.raise_for_status()
return resp.json()
def get_access_token(self, user):
with self.cv:
d = self.accesstokens.get(user, {})
refetch_at = d.get('expires_in', 0)/2 + d.get('fetched_at', 0)
now = time.time()
if refetch_at <= now:
newd = self._fetch_token_json(user)
if newd:
newd['fetched_at'] = now
self.accesstokens[user] = newd
d = newd
if not d:
raise NestError(f'could not refresh {user}')
return d['access_token']
def bearer_json_request(self, access_token, command, path, data=None):
# e.g. path is /devices
endpoint = f'{self.API_PREFIX}{self.config["nest"]["projectid"]}{path}'
headers = { 'Content-Type': 'application/json',
'Authorization': f'Bearer {access_token}' }
timeout = self.config['nest']['timeout']
if data: # depending on command, data may not be allowed as an argument
resp = command(endpoint, headers=headers, timeout=timeout, data=data)
else:
resp = command(endpoint, headers=headers, timeout=timeout)
resp.raise_for_status()
if resp.status_code == 204:
return None
return resp.json()
@REQUEST_TIME.time()
def collect(self, target):
"""get the status of all devices"""
metric_to_gauge = {}
def makegauge(metric, desc, morelabels=[]):
already = metric_to_gauge.get(metric)
if already:
return already
labels = ['deviceId', 'nameLabel', 'location', 'room'] + morelabels
gmf = GaugeMetricFamily(metric, desc, labels=labels)
metric_to_gauge[metric] = gmf
return gmf
token = self.get_access_token(target)
resp = self.bearer_json_request(token, requests.get, '/devices')
for device in resp.get('devices', []):
deviceid = device['name'].split('/')[-1] # last path component is the deviceid
basetype = device['type'].split('.')[-1].lower() # e.g. 'thermostat'
customname = device.get('traits', {}).get('sdm.devices.traits.Info', {}).get('customName', '')
location, roomname = '', ''
for d in device.get('parentRelations', []):
p = d.get('parent')
if p and not roomname:
# e.g. enterprises/88f863/structures/W6hYBQCS/rooms/PPrzWmR99RRz
path = p.split('/')
location = '/'.join(path[2:4])
roomname = d.get('displayName')
labelvalues = [deviceid, customname, location, roomname]
for (n, d) in device.get('traits', {}).items():
for (nn, vv) in d.items():
if nn == 'ambientHumidityPercent':
g = makegauge('humidity_pct', 'percent ambient humidity')
g.add_metric(labelvalues, vv)
elif nn == 'ambientTemperatureCelsius':
g = makegauge('temp_c', 'ambient temperature (degrees Celsius)')
g.add_metric(labelvalues, vv)
elif n == 'sdm.devices.traits.Connectivity' and nn == 'status':
online = (vv.lower() == 'online')
g = makegauge('is_online', 'true if is online',
morelabels=['state'])
g.add_metric(labelvalues + [vv], online)
elif n == 'sdm.devices.traits.Fan' and nn == 'timerMode':
on = (vv.lower() != 'off')
g = makegauge('fan_manual_on', 'true if fan is manually on',
morelabels=['state'])
g.add_metric(labelvalues + [vv], on)
elif n == 'sdm.devices.traits.ThermostatMode' and nn == 'mode':
def makeit(mode):
g = makegauge(f'thermostat_mode_is_{mode.lower()}',
f'true if thermostat mode is {mode.upper()}',
morelabels=['state'])
g.add_metric(labelvalues + [vv], vv.lower() == mode.lower())
makeit('heat')
makeit('cool')
makeit('heatcool')
makeit('off')
elif n == 'sdm.devices.traits.ThermostatEco' and nn == 'mode':
on = (vv.lower() != 'off')
g = makegauge('thermostat_eco_on', 'true if in manual eco mode',
morelabels=['state'])
g.add_metric(labelvalues + [vv], on)
elif n == 'sdm.devices.traits.ThermostatHvac' and nn == 'status':
def makeit(state):
g = makegauge(f'thermostat_state_is_{state.lower()}',
f'true if thermostat state is {state.upper()}',
morelabels=['state'])
g.add_metric(labelvalues + [vv], vv.lower() == state.lower())
makeit('heating')
makeit('cooling')
elif n == 'sdm.devices.traits.ThemostatTemperatureSetpoint':
if nn == 'heatCelsius':
gheat = makegauge('thermostat_heat_setpoint_c',
'thermostat heat setpoint (deg Celsius)')
gheat.add_metric(labelvalues, vv)
elif nn == 'coolCelsius':
gcool = makegauge('thermostat_cool_setpoint_c',
'thermostat cool setpoint (deg Celsius)')
gcool.add_metric(labelvalues, vv)
return metric_to_gauge.values()
def prompt_for_project():
print('''
To create Google developer credentials, follow these steps:
STEP 1. Create a new project in the Google Cloud Platform Console
In your browser, visit https://console.cloud.google.com/projectcreate
''')
input('Press ENTER when complete: ')
print('''
STEP 2. Create OAuth credentials in the GCP Console
In your browser, visit https://console.cloud.google.com/apis/credentials
Create an OAuth 2.0 client ID **for desktop** and download the credentials.json
''')
while True:
credfile = input('Enter the path to the credentials.json file: ')
creds = {}
try:
with open(credfile, 'rt') as c:
creds = json.load(c)
break
except IOError as e:
print(e)
continue
except OSError as e:
print(e)
continue
clientid = creds.get('installed', {}).get('client_id')
clientsecret = creds.get('installed', {}).get('client_secret')
if clientid and clientsecret:
pass
else:
print('could not load credentials -- exiting')
return
##### may need to set up OAuth consent screen in GCP Console
##### may need to add yourself as a test user
print('''
STEP 3. Enable the Smart Device Management API in the GCP Console
In your browser, visit https://console.cloud.google.com/apis/library/smartdevicemanagement.googleapis.com
''')
input('Press ENTER when complete: ')
print(f'''
STEP 4. Create a project in the Google Nest Device Access Console
This requires you to connect a credit card and pay $5.
In your browser, visit https://console.nest.google.com/device-access/project-list
- Enter a name such as "Porter for Prometheus"
- When it asks for the OAuth Client ID, paste this:
{clientid}
- Disable events (Pub/Sub) for now.
''')
projectid = input('When complete, copy the project ID and enter it here: ')
uri = f'https://nestservices.google.com/partnerconnections/{projectid}/auth?redirect_uri=https://localhost&access_type=offline&prompt=consent&client_id={clientid}&response_type=code&scope=https://www.googleapis.com/auth/sdm.service'
print(f'''
STEP 5. Obtain an OAuth code
In your browser, visit {uri}
- Agree to the consent screens.
- The final page will fail to load and that is okay.
''')
(resp, refreshtoken) = get_refresh(clientid, clientsecret, projectid)
print(json.dumps(resp.json(), indent=2))
print(f'''
Add these lines to your porter.yml file:
nest:
projectid: {projectid}
clientid: {clientid}
clientsecret: {clientsecret}
credentials:
'YOUR_TARGETNAME_GOES_HERE': '{refreshtoken}'
''')
def get_refresh(clientid, clientsecret, projectid):
while True:
finaluri = input('Paste the URL from the browser bar after you have agreed to the consent screens: ')
(junk, mid, end) = finaluri.partition('code=')
if end:
(code, amp, end) = end.partition('&')
break
print('The URL you entered does not contain a code. Please try again.')
resp = requests.post(f'https://www.googleapis.com/oauth2/v4/token?client_id={clientid}&client_secret={clientsecret}&code={code}&grant_type=authorization_code&redirect_uri=https://localhost')
resp.raise_for_status()
c = resp.json()
accesstoken = c.get('access_token')
refreshtoken = c.get('refresh_token')
endpoint = f'https://smartdevicemanagement.googleapis.com/v1/enterprises/{projectid}/devices'
headers = { 'Content-Type': 'application/json',
'Authorization': f'Bearer {accesstoken}' }
resp = requests.get(endpoint, headers=headers, timeout=20)
resp.raise_for_status()
return (resp, refreshtoken)
def prompt_for_refresh(config):
clientid = config['nest']['clientid']
clientsecret = config['nest']['clientsecret']
projectid = config['nest']['projectid']
uri = f'https://nestservices.google.com/partnerconnections/{projectid}/auth?redirect_uri=https://localhost&access_type=offline&prompt=consent&client_id={clientid}&response_type=code&scope=https://www.googleapis.com/auth/sdm.service'
print(f'''
In your browser, visit {uri}
- Agree to the consent screens.
- The final page will fail to load and that is okay.
''')
(resp, refreshtoken) = get_refresh(clientid, clientsecret, projectid)
print(f'''
credentials:
'YOUR_TARGETNAME_GOES_HERE': '{refreshtoken}'
''')
if __name__ == '__main__':
import sys, yaml
assert len(sys.argv) == 2, sys.argv
config = yaml.safe_load(open(sys.argv[1]))
try:
creds = config.get('nest', {}).get('credentials', {})
if creds:
client = NestClient(config)
for (target, refresh) in creds.items():
token = client.get_access_token(target)
resp = client.bearer_json_request(token, requests.get, '/devices')
print(f'***************** {target}')
print(json.dumps(resp, indent=2))
else:
print('no nest credentials in config')
prompt_for_project()
except NestError as e:
print(f'could not instantiate client: {e}')
prompt_for_project()
except requests.exceptions.HTTPError as e:
print(f'error instantiating client: {e}')
prompt_for_refresh(config)