-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathecobee_config.py
43 lines (35 loc) · 1.43 KB
/
ecobee_config.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
import json
import logging
DEFAULT_CONFIG_FILENAME = "config.json"
class EcobeeConfig:
def __init__(self, config_location=DEFAULT_CONFIG_FILENAME):
self.config_location = config_location
self.loaded = False
def load(self):
try:
with open(self.config_location) as config_file:
data = json.load(config_file)
self.pin = data.get("pin", "")
self.code = data.get("code", "")
self.access_token = data.get("access_token", "")
self.refresh_token = data.get("refresh_token", "")
self.api_key = data.get("api_key", "")
self.thermostat_ids = data.get("thermostat_ids", [])
self.csv_location = data.get("csv_location", "ecobee.csv")
self.loaded = True
except:
logging.error(
"Failed to load config file, make sure to run setup (`--setup`) first!"
)
exit(1)
def save(self):
with open(self.config_location, "w") as config_file:
output = dict(self.__dict__)
output.pop("loaded")
output.pop("config_location")
json_str = json.dumps(output, indent=4)
config_file.write(json_str)
def auth_header(self):
if not self.loaded:
raise Exception("Config not loaded!")
return {"Authorization": "Bearer " + self.access_token}