-
Notifications
You must be signed in to change notification settings - Fork 0
/
nest_thermostat.py
59 lines (48 loc) · 2.66 KB
/
nest_thermostat.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
import requests
import json
import os
class thermostat:
def __init__(self):
self.access_token = self.get_access_token()
self.request_headers = { 'Content-Type' : 'application/json',
'Authorization' : 'Bearer ' + self.access_token
}
project_id = os.environ['NEST_GCP_PROJECT_ID']
device_id = os.environ['NEST_GCP_DEVICE_ID']
self.endpoint = f'https://smartdevicemanagement.googleapis.com/v1/enterprises/{project_id}/devices/{device_id}'
def get_access_token(self):
NEST_GCP_CLIENT_ID = os.environ['NEST_GCP_CLIENT_ID']
NEST_GCP_CLIENT_SECRET = os.environ['NEST_GCP_CLIENT_SECRET']
NEST_GCP_REFRESH_TOKEN = os.environ['NEST_GCP_REFRESH_TOKEN']
refresh_url = f'https://www.googleapis.com/oauth2/v4/token?client_id={NEST_GCP_CLIENT_ID}&client_secret={NEST_GCP_CLIENT_SECRET}&refresh_token={NEST_GCP_REFRESH_TOKEN}&grant_type=refresh_token'
response = requests.post(refresh_url)
access_token = response.json()["access_token"]
return access_token
def execute_thermostat_command(self, command, params):
post_url = self.endpoint + ':executeCommand'
message = {'command': f'sdm.devices.commands.{command}',
'params': params
}
response = requests.post(post_url, headers=self.request_headers, json=message)
if response.status_code != 200:
print(f' * error: {command} failed with status code {response.status_code}')
exit(1)
def set_mode(self, mode):
params = {'mode' : mode}
self.execute_thermostat_command('ThermostatMode.SetMode', params)
def check_mode(self):
response = requests.get(self.endpoint, headers=self.request_headers)
return response.json()['traits']['sdm.devices.traits.ThermostatMode']['mode']
def set_fan_timer(self, duration_mins):
# System must be set to HEAT in order for fan to run - but HEAT mode doesn't it's always blowing warm air. You can just set the heat to a low temp.
params = {'timerMode' : 'ON',
'duration' : str(duration_mins * 60) + 's'
}
current_mode = self.check_mode()
# Heat must be on in order for fan to run.
# You can set the heat to turn on with self.set_mode('HEAT')...
# ...but leaving it like this creates a manual override, since anyone can disable the script by setting heat to OFF)
if current_mode == "HEAT":
self.execute_thermostat_command('Fan.SetTimer', params)
else:
print(f'Heat is set to {current_mode}, doing nothing')