Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add schedule #2

Merged
merged 6 commits into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,11 @@ ENV/
# Rope project settings
.ropeproject

# direnv
.envrc

# pipenv
Pipfile*

# output
*.out
50 changes: 50 additions & 0 deletions evohome-exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
import time
import datetime as dt
from evohomeclient2 import EvohomeClient
from keys import username, password
import prometheus_client as prom
Expand All @@ -23,6 +24,51 @@ def loginEvohome(myclient):
return True


def _get_set_point(zone_schedule, day_of_week, spot_time):
daily_schedules = {
s["DayOfWeek"]: s["Switchpoints"] for s in zone_schedule["DailySchedules"]
}
switch_points = {
dt.time.fromisoformat(s["TimeOfDay"]): s["heatSetpoint"]
for s in daily_schedules[day_of_week]
}
candidate_times = [k for k in switch_points.keys() if k <= spot_time]
if len(candidate_times) == 0:
# no time less than current time
return None

candidate_time = max(candidate_times)
return switch_points[candidate_time]


def calculate_planned_temperature(zone_schedule):
current_time = dt.datetime.now().time()
day_of_week = dt.datetime.today().weekday()
return _get_set_point(zone_schedule, day_of_week, current_time) or _get_set_point(
zone_schedule, day_of_week - 1 if day_of_week > 0 else 6, dt.time.max
)


schedules_updated = dt.datetime.min
schedules = {}


def get_schedules():
global schedules_updated
global schedules

# this takes time, update once per hour
if schedules_updated < dt.datetime.now() - dt.timedelta(hours=1):
for zone in client._get_single_heating_system()._zones:
schedules[zone.zoneId] = zone.schedule()

# schedules = {
# zone.zone_id: zone.schedule()
# for zone in client._get_single_heating_system()._zones
# }
schedules_updated = dt.datetime.now()


if __name__ == "__main__":
eht = prom.Gauge(
"evohome_temperature_celcius",
Expand Down Expand Up @@ -95,6 +141,7 @@ def loginEvohome(myclient):
newids = set()
try:
temps = list(client.temperatures())
get_schedules()
loggedin = True
updated = True
lastupdated = time.time()
Expand Down Expand Up @@ -142,6 +189,9 @@ def loginEvohome(myclient):
eht.labels(d["name"], d["thermostat"], d["id"], "setpoint").set(
d["setpoint"]
)
eht.labels(d["name"], d["thermostat"], d["id"], "planned").set(
calculate_planned_temperature(schedules[d["id"]])
)
zmode.labels(d["name"], d["thermostat"], d["id"]).state(
d.get("setpointmode", "FollowSchedule")
)
Expand Down