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/') def hook(uuid:str): callback = None try: callback = Callback.get_callback(uuid) 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: return render_template("error/500.html", error=str(e)), 500 return render_template("hook.html", name=callback.name, note=callback.note)