-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
76 lines (66 loc) · 2.37 KB
/
__main__.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
import os
import time
import g_price
import g_coms
import g_temp
# =======================
# Configuration
# =======================
load_zone = os.environ["LOAD_ZONE"]
ifttt_key = os.environ["IFTTT_KEY"]
notification_url = f"https://maker.ifttt.com/trigger/g_notification/with/key/{ifttt_key}"
heat_url = f"https://maker.ifttt.com/trigger/g_heat/with/key/{ifttt_key}"
cool_url = f"https://maker.ifttt.com/trigger/g_cool/with/key/{ifttt_key}"
# Read the favorite temperature settings
with open("settings/fav_heat.txt") as file:
fav_heat = int(file.read())
with open("settings/fav_cool.txt") as file:
fav_cool = int(file.read())
try:
with open("settings/.last_heat.txt") as file:
last_heat = int(file.read())
with open("settings/.last_cool.txt") as file:
last_cool = int(file.read())
except Exception as e:
last_heat = -1
last_cool = -1
g_coms.send_data(notification_url, ["Welcome to Griddle! If everything is set up right, I'll start saving "
"you money by shifting your AC and Heat usage away from random spikes! "
"Make sure that you're starting the container every minute for best results."])
print(f"{last_heat} {last_cool}")
# =======================
# Get the price and temp
# =======================
# Get the current price
current_price, updated_at = g_price.get_current(load_zone)
print(f"Currently costs {round(current_price, 2)}¢ ({updated_at})")
# Calculate the new temperature settings
temp_offset = g_temp.calculate_offset(current_price)
new_cool = fav_cool + temp_offset
new_heat = fav_heat - temp_offset
# =======================
# Update, notify, & save
# =======================
if last_cool != new_cool or last_heat != new_heat:
# Update the heat
g_coms.send_data(
heat_url,
[new_heat]
)
# Update the cool
time.sleep(2)
g_coms.send_data(
cool_url,
[new_cool]
)
# Send a notification about the changes
time.sleep(2)
g_coms.send_data(
notification_url,
[f"Griddle – The price of energy is at {current_price}¢ so I'm setting your temp to {new_heat}º – {new_cool}º."]
)
# Update 'last' values
with open("settings/.last_heat.txt", "w") as file:
file.write(str(new_heat))
with open("settings/.last_cool.txt", "w") as file:
file.write(str(new_cool))