def create_trigger_for(device_id): device_info = data_interface.get_device_info(device_id) device_type = device_info["device_type"] if device_type == "thermostat": form = CreateTriggerFormThermostat( possible_affected_devices=data_interface. get_possible_affected_devices(device_id)) elif device_type == "motion_sensor": form = CreateTriggerFormMotionSensor( possible_affected_devices=data_interface. get_possible_affected_devices(device_id)) else: raise Exception() if form.validate_on_submit(): actor_info = data_interface.get_device_info(form.affected_device.data) actor_type = actor_info["device_type"] if actor_type == "thermostat": action_form = CreateTriggerActionFormThermostat() action_form.event.data = form.event.data action_form.event_parameters.data = form.event_parameters.data else: raise Exception() return render_template("internal/triggers/create_trigger.html", device=device_info, actor=actor_info, action_form=action_form) return show_device(device_id, create_trigger_form=form)
def show_device(device_id, thermostat_settings_form=None, create_trigger_form=None): device = data_interface.get_device_info(device_id) device_type = device['device_type'] device_settings = None if device_type == "thermostat": device_settings = {"has_settings": True} if thermostat_settings_form is not None: device_settings["form"] = thermostat_settings_form else: device_settings["form"] = SetThermostatTargetForm() elif device_type == "light_switch": device_settings = {"has_settings": True} if create_trigger_form is not None: pass elif device_type == "thermostat": create_trigger_form = CreateTriggerFormThermostat( possible_affected_devices=data_interface.get_possible_affected_devices(device_id)) elif device_type == "motion_sensor": create_trigger_form = CreateTriggerFormMotionSensor( possible_affected_devices=data_interface.get_possible_affected_devices(device_id)) return render_template("internal/device_details.html", device=device, device_settings=device_settings, affecting_triggers=data_interface.get_affecting_triggers(device_id), affected_triggers=data_interface.get_affected_triggers(device_id), create_trigger_form=create_trigger_form)
def create_trigger_action_for(device_id, actor_id): device_info = data_interface.get_device_info(device_id) device_type = device_info["device_type"] actor_info = data_interface.get_device_info(actor_id) actor_type = actor_info["device_type"] if actor_type == "thermostat": action_form = CreateTriggerActionFormThermostat() else: raise Exception() if not action_form.validate_on_submit(): return create_trigger_for(device_id) result = data_interface.add_new_trigger(sensor_id=device_id, actor_id=actor_id, event=action_form.event.data, event_params=action_form.event_parameters.data, action=action_form.action.data, action_params=str(action_form.action_parameters.data), user_id=get_active_user()["user_id"]) return redirect(url_for('.show_device', device_id=device_id))
def show_device(user_id, device_id, form=None): user = data_interface.get_user_info(user_id) triggers = None device = data_interface.get_device_info(device_id) if device['device_type'] == "thermostat": triggers = shared.triggers.thermostat_triggers if form is None: form = SetThermostatTargetForm() elif device['device_type'] == "motion_sensor": triggers = shared.triggers.motion_triggers elif device['device_type'] == "light_switch": triggers = shared.triggers.light_triggers elif device['device_type'] == "door_sensor": triggers = shared.triggers.door_triggers all_user_devices = data_interface.get_user_devices(user_id) actors = [{ "id": device['device_id'], "name": device['name'], "type": "device", "device": device, "action": shared.actions.actions[device['device_type']] } for device in all_user_devices] + [{ "id": "webhook_url", "type": "webhook", "url": "#", "name": "Send email" }] thermostats = filter(lambda x: x['device_id'] == "thermostat", all_user_devices) door_sensors = filter(lambda x: x['device_id'] == "door_sensor", all_user_devices) motion_sensors = filter(lambda x: x['device_id'] == "motion_sensor", all_user_devices) light_switches = filter(lambda x: x['device_id'] == "light_switch", all_user_devices) return render_template("admin/admin_deviceactions.html", user=user, device=device, triggers=triggers, actors=actors, thermostats=thermostats, light_switches=light_switches, door_sensors=door_sensors, motion_sensors=motion_sensors, change_settings_form=form)