-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecobee_thermostat.py
66 lines (53 loc) · 2.21 KB
/
ecobee_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
60
61
62
63
64
65
66
import requests
import json
import os
import math
class thermostat:
def __init__(self):
self.access_token = self.get_access_token()
self.request_headers = { 'Content-Type': 'application/json;charset=UTF-8',
'Authorization' : 'Bearer ' + self.access_token
}
self.endpoint = 'https://api.ecobee.com/1/thermostat'
def get_access_token(self):
api_key = os.environ['ECOBEE_API_KEY']
auth_code = os.environ['ECOBEE_AUTH_CODE']
refresh_token = os.environ['ECOBEE_REFRESH_TOKEN']
token_url = f'https://api.ecobee.com/token?grant_type=refresh_token&code={refresh_token}&client_id={api_key}'
response = requests.post(token_url)
access_token = response.json()["access_token"]
return access_token
def check_mode(self):
selection = {'selection': {
'selectionType': 'registered',
'selectionMatch': '',
'includeSettings':'true'
}}
mode = self.execute_thermostat_command(selection)["thermostatList"][0]["settings"]["hvacMode"]
print(f'mode: {mode}')
return mode
def execute_thermostat_command(self, selection):
params = {'json': json.dumps(selection)}
response = requests.get(self.endpoint, headers=self.request_headers, params=params)
return response.json()
# Runs fan for the provided time - rounding up to the nearest hour.
def set_fan_timer(self, duration_mins):
duration_hours = math.ceil(duration_mins/60)
selection = {
"selection": {
"selectionType":"registered",
"selectionMatch":""
},
"functions": [{
"type":"setHold",
"params":{
"holdType":"holdHours",
"holdHours":duration_hours,
# Need to figure out proper hold temps
"heatHoldTemp":690,
"coolHoldTemp":720,
"fan": "on"
}
}]
}
response = self.execute_thermostat_command(selection)