Add a page schedule with rotation, Home Assistant trigger rules and a Stopwatch tab

- Schedule: daily blocks from a start time, each with up to 6 rotating pages
  or bitmaps (own duration), switching with the effect or instantly. A manual
  pick (Show tab, HA Mode) lasts until the next block; Back to schedule ends it.
- Rules: an HA state (=, is not, above, below) shows a page while true, adds it
  to the rotation while true, or shows it for N seconds when it becomes true.
  Each rule is polled at its own interval, with a 60 s back-off on errors.
- PageSelector (pure logic, tested) decides the shown page; schedule stored in
  NVS; HA sensor Shown page and button Back to schedule.
- Web UI: Schedule and Stopwatch tabs, bitmaps used by pages or rules in their
  own subsection, schedule in export/import. Version 2.6.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-10-15 21:36:56 +02:00
1 parent 47ef65ee7d
commit 93b9bec69a
25 files changed
+1217 -50

No files matched your search

+112
View File
@@ -0,0 +1,112 @@
#include "logic/page_schedule.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
static bool terminated(const char *text, size_t size)
{
return memchr(text, '\0', size) != nullptr;
}
static bool validAttribute(const char *attribute)
{
for (const char *c = attribute; *c; c++)
if (!isalnum((unsigned char)*c) && *c != '_')
return false;
return true;
}
const char *validateSchedule(const PageSchedule &s)
{
if (s.block_count > SCHEDULE_BLOCKS)
return "Too many schedule blocks (8 at most)";
if (s.rule_count > TRIGGER_RULES)
return "Too many rules (8 at most)";
for (int i = 0; i < s.block_count; i++)
{
const ScheduleBlock &b = s.blocks[i];
if (b.start >= 24 * 60)
return "Blocks must start before 24:00";
if (i > 0 && b.start <= s.blocks[i - 1].start)
return "Blocks must be in order, each at a different time";
if (b.item_count == 0 || b.item_count > BLOCK_ITEMS)
return "Each block needs 1 to 6 pages";
for (int j = 0; j < b.item_count; j++)
{
const ScheduleItem &item = b.items[j];
if (!terminated(item.page, sizeof(item.page)) || !validBitmapName(item.page))
return "Choose a page for each item of a block";
if (item.seconds < ITEM_SECONDS_MIN || item.seconds > ITEM_SECONDS_MAX)
return "Pages are shown 5 to 3600 seconds";
}
}
for (int i = 0; i < s.rule_count; i++)
{
const TriggerRule &r = s.rules[i];
if (!terminated(r.entity, sizeof(r.entity)) || !validEntityId(r.entity))
return "Rules need an entity id like sensor.printer_state";
if (!terminated(r.attribute, sizeof(r.attribute)) || !validAttribute(r.attribute))
return "Invalid attribute";
if (r.compare >= Compare::Count || r.mode >= TriggerMode::Count)
return "Unknown rule setting";
if (!terminated(r.value, sizeof(r.value)))
return "Rule value too long (32 characters)";
if (!terminated(r.page, sizeof(r.page)) || !validBitmapName(r.page))
return "Choose a page for each rule";
if (r.mode != TriggerMode::WhileTrue && (r.seconds < 1 || r.seconds > ITEM_SECONDS_MAX))
return "Rule times must be 1 to 3600 seconds";
if (r.interval < RULE_INTERVAL_MIN || r.interval > RULE_INTERVAL_MAX)
return "Rules are read every 2 to 600 seconds";
}
return nullptr;
}
static bool sameText(const char *a, const char *b)
{
for (; *a && *b; a++, b++)
if (tolower((unsigned char)*a) != tolower((unsigned char)*b))
return false;
return *a == *b;
}
static bool number(const char *text, double &value)
{
char *end;
value = strtod(text, &end);
return end != text && *end == '\0';
}
bool ruleMatches(const TriggerRule &rule, const char *value)
{
if (value == nullptr)
return false;
double a, b;
switch (rule.compare)
{
case Compare::Equal:
return sameText(value, rule.value);
case Compare::NotEqual:
return !sameText(value, rule.value);
case Compare::Above:
return number(value, a) && number(rule.value, b) && a > b;
case Compare::Below:
return number(value, a) && number(rule.value, b) && a < b;
default:
return false;
}
}
bool scheduleUses(const PageSchedule &s, const char *name)
{
for (int i = 0; i < s.block_count; i++)
for (int j = 0; j < s.blocks[i].item_count; j++)
if (sameName(s.blocks[i].items[j].page, name))
return true;
for (int i = 0; i < s.rule_count; i++)
if (sameName(s.rules[i].page, name))
return true;
return false;
}