-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnest.py
93 lines (78 loc) · 4.4 KB
/
nest.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
import requests
from config import nest_refresh_token_query_params, nest_device_project_id
access_token = None
def convert_to_f(c):
return c * (9 / 5) + 32
def get_access_token():
print(f'Getting new nest access token...')
# Get new oauth token using existing refresh token
r = requests.post('https://www.googleapis.com/oauth2/v4/token', params=nest_refresh_token_query_params)
response_body = r.json()
if 'access_token' in response_body:
token = response_body['access_token']
# print(f'New token: {token}')
else:
raise (Exception(f'NEST - No Access Token available in oauth response. Response body: {response_body}'))
return token
def get_nest_data():
global access_token
nest_data = []
try:
if not access_token:
access_token = get_access_token()
# print(f'get_nest_data: {access_token}')
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {access_token}'
}
# Get device(s)
r = requests.get(
f'https://smartdevicemanagement.googleapis.com/v1/enterprises/{nest_device_project_id}/devices',
headers=headers)
response_body = r.json()
# print(f'Devices response: {response_body}')
try:
devices = response_body['devices']
except Exception as ex:
# TODO: Check response to confirm token expiration message. Token appears to expire after 1hr
# response_body: {'error': {'code': 401, 'message': 'Request had invalid authentication credentials.
# Expected OAuth 2 access token, login cookie or other valid authentication credential. See
# https://developers.google.com/identity/sign-in/web/devconsole-project.', 'status': 'UNAUTHENTICATED'}}
print(f'Device Data was not returned - {type(ex).__name__} - {ex} - response_body: {response_body}')
access_token = get_access_token()
raise ex
for device in devices:
# print(json.dumps(device, indent=4, sort_keys=True))
traits = {
'display_name': device['parentRelations'][0]['displayName'],
'fan_on': 0 if device['traits']['sdm.devices.traits.Fan']['timerMode'] == 'OFF' else 1,
'humidity': device['traits']['sdm.devices.traits.Humidity']['ambientHumidityPercent'],
'temperature_ambient_c': device['traits']['sdm.devices.traits.Temperature'][
'ambientTemperatureCelsius'],
'temperature_ambient_f': convert_to_f(
device['traits']['sdm.devices.traits.Temperature']['ambientTemperatureCelsius']
),
'mode_heat': 1 if device['traits']['sdm.devices.traits.ThermostatMode']['mode'] == 'HEAT' else 0,
'mode_cool': 1 if device['traits']['sdm.devices.traits.ThermostatMode']['mode'] == 'COOL' else 0,
'mode_heatcool': 1 if device['traits']['sdm.devices.traits.ThermostatMode'][
'mode'] == 'HEATCOOL' else 0,
'mode_off': 1 if device['traits']['sdm.devices.traits.ThermostatMode']['mode'] == 'OFF' else 0,
'hvac_cooling': 1 if device['traits']['sdm.devices.traits.ThermostatHvac'][
'status'] == 'COOLING' else 0,
'hvac_heating': 1 if device['traits']['sdm.devices.traits.ThermostatHvac'][
'status'] == 'HEATING' else 0,
'hvac_off': 1 if device['traits']['sdm.devices.traits.ThermostatHvac']['status'] == 'OFF' else 0,
}
for set_point in device['traits']['sdm.devices.traits.ThermostatTemperatureSetpoint'].keys():
traits['temperature_set_point_c'] = \
device['traits']['sdm.devices.traits.ThermostatTemperatureSetpoint'][set_point]
traits['temperature_set_point_f'] = convert_to_f(
device['traits']['sdm.devices.traits.ThermostatTemperatureSetpoint'][set_point]
)
traits['set_point_cool'] = 1 if set_point == 'coolCelsius' else 0
traits['set_point_heat'] = 1 if set_point == 'heatCelsius' else 0
nest_data.append(traits)
# print(json.dumps(traits, indent=4, sort_keys=True))
except Exception as e:
print(f'{type(e).__name__} - {e}')
return nest_data