diff --git a/app/models/schedule.py b/app/models/schedule.py index 9314654..eafad0e 100644 --- a/app/models/schedule.py +++ b/app/models/schedule.py @@ -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 diff --git a/app/views/callbacks.py b/app/views/callbacks.py index c25133a..d9e077b 100644 --- a/app/views/callbacks.py +++ b/app/views/callbacks.py @@ -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) diff --git a/app/views/hook.py b/app/views/hook.py index 51a216b..3b18cb1 100644 --- a/app/views/hook.py +++ b/app/views/hook.py @@ -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/') @@ -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: