allow slots to be off in the daylight scheduler

This commit is contained in:
Jakob Ketterl 2021-05-17 23:23:25 +02:00
parent e206b83e74
commit 33a942707c
2 changed files with 27 additions and 16 deletions

View File

@ -42,9 +42,7 @@ class GainInput(Input):
{errors}
</div>
""".format(
id=self.id,
input=self.render_input(value, errors),
errors=self.render_errors(errors)
id=self.id, input=self.render_input(value, errors), errors=self.render_errors(errors)
)
def render_options(self, value):
@ -187,11 +185,28 @@ class SchedulerInput(Input):
self.profiles = config["profiles"]
return super().render(config, errors)
def render_profiles_select(self, value, errors, config_key, stage, extra_classes=""):
def render_profiles_select(self, value, errors, config_key, stage, extra_classes="", allow_empty=False):
stage_value = ""
if value and "schedule" in value and config_key in value["schedule"]:
stage_value = value["schedule"][config_key]
options = "".join(
"""
<option value="{id}" {selected}>{name}</option>
""".format(
id=p_id,
name=p["name"],
selected="selected" if stage_value == p_id else "",
)
for p_id, p in self.profiles.items()
)
if allow_empty:
# prepend a special "off" option to allow a schedule slot to go unused (daylight scheduler)
options = """<option value="None" {selected}>Off</option>""".format(
selected="selected" if value is None else ""
) + options
return """
<select class="{extra_classes} {classes}" id="{id}" name="{id}" {disabled}>
{options}
@ -201,16 +216,7 @@ class SchedulerInput(Input):
classes=self.input_classes(errors),
extra_classes=extra_classes,
disabled="disabled" if self.disabled else "",
options="".join(
"""
<option value={id} {selected}>{name}</option>
""".format(
id=p_id,
name=p["name"],
selected="selected" if stage_value == p_id else "",
)
for p_id, p in self.profiles.items()
),
options=options,
)
def render_static_entires(self, value, errors):
@ -271,7 +277,9 @@ class SchedulerInput(Input):
</div>
""".format(
name=name,
select=self.render_profiles_select(value, errors, stage, stage, extra_classes="col-9"),
select=self.render_profiles_select(
value, errors, stage, stage, extra_classes="col-9", allow_empty=True
),
)
for stage, name in [("day", "Day"), ("night", "Night"), ("greyline", "Greyline")]
)
@ -324,6 +332,9 @@ class SchedulerInput(Input):
def getStageValue(stage):
input_id = "{id}-{stage}".format(id=self.id, stage=stage)
if input_id in data:
# special treatment for the "off" option
if data[input_id][0] == "None":
return None
return data[input_id][0]
else:
return None

View File

@ -192,7 +192,7 @@ class DaylightSchedule(TimerangeSchedule):
for event in events:
# night profile _until_ sunrise, day profile _until_ sunset
stype = "night" if event["type"] == "sunrise" else "day"
if previousEvent is not None or event["time"] - delta > now:
if stype in self.schedule and (previousEvent is not None or event["time"] - delta > now):
start = now if previousEvent is None else previousEvent
entries.append(DatetimeScheduleEntry(start, event["time"] - delta, self.schedule[stype]))
if useGreyline: