Implement basic schedule functionality

This commit is contained in:
2022-12-11 15:26:37 +01:00
parent 01ec2af4c7
commit 451160face
3 changed files with 49 additions and 5 deletions
+3 -3
View File
@@ -28,7 +28,7 @@ class Schedule(db.Model):
self.end_time = endtime
def __repr__(self) -> str:
return str(self.name) + ': ' + str(self.type)
return str(self.name) + ': ' + self.startday_str() + ' ' + str(self.start_time) + ' - ' + self.endday_str() + ' ' + str(self.end_time)
def startday_str(self):
return day_to_string(self.start_day)
@@ -37,7 +37,7 @@ class Schedule(db.Model):
return day_to_string(self.end_day)
@classmethod
def get_schedule(cls, id:int) -> str:
def get_schedule(cls, id:int) -> any:
result = cls.query.filter_by(id=id).first()
if result is not None:
return result
@@ -45,7 +45,7 @@ class Schedule(db.Model):
raise KeyError("ID {} does not exist".format(id))
@classmethod
def get_schedules(cls, uuid:str) -> str:
def get_schedules(cls, uuid:str) -> any:
result = cls.query.filter_by(callback_id=uuid)
if result is not None:
return result
-2
View File
@@ -53,8 +53,6 @@ def schedule(uuid:str):
callback = Callback.get_callback(uuid)
schedules = Schedule.get_schedules(uuid)
print(dir(schedules))
return render_template("schedule.html",callback=callback, schedules=schedules)
+46
View File
@@ -1,8 +1,12 @@
from datetime import datetime, time
from flask import render_template
from homeassistant_api import HomeassistantAPIError
from app import webapp
from app.models.callback import Callback
from app.models.settings import Setting
from app.models.schedule import Schedule
from app.homeassistant import HomeAssistant
@webapp.route('/hook/<uuid>')
@@ -14,6 +18,48 @@ def hook(uuid:str):
except:
return render_template("error/404.html"), 404
if(not callback.active):
return render_template("error/404.html"), 404
feedback = Setting.get_value(Setting.Keys.FEEDBACK);
schedules = Schedule.get_schedules(uuid=callback.uuid);
cweekday = datetime.now().weekday()
ctime = datetime.now().time()
anymatch = False;
if(schedules.count() != 0):
for schedule in schedules:
print(schedule)
possiblematch = True
if(cweekday == schedule.start_day):
if(ctime < schedule.start_time):
possiblematch = False
if(cweekday == schedule.end_day):
if(ctime > schedule.end_time):
possiblematch = False
if(cweekday < schedule.start_day or cweekday > schedule.end_day):
possiblematch = False
if(possiblematch):
anymatch = True
print(anymatch)
if not anymatch:
if(feedback == "none"):
return render_template("error/404.html"), 404
if(feedback == "some"):
return render_template("hook.html", name="Action not permitted", note="This action cannot be performed at this time.")
if(feedback == "all"):
return render_template("hook.html", name=callback.name, note="This action is not scheduled to be active at this time.")
try:
HomeAssistant.getInstance().trigger_service(callback.domain, callback.service, callback.entity, callback.data)
except HomeassistantAPIError as e: