feat: faster, correct preset editor; default presets use their own pattern

- Editing a preset can change its pattern (the dropdown was disabled).
- on, off and rainbow are built-in patterns and are always listed, so the
  editor no longer falls back to the first pattern (aurora) for them.
- The editor loads the pattern names once and only the selected pattern's
  definition (new GET /patterns/names and GET /patterns/<name>).
- A shared GET cache (fetch_cache.js) reuses profile, palette, zone, preset,
  group and pattern lookups; any write clears it and entries expire at 30 s.
- The seeded rainbow preset uses the rainbow pattern.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Sonnet 5.5 committed 2026-11-10 01:46:11 +13:00
1 parent d4118072f6
commit 38162d4b89
9 files changed
+189 -81

No files matched your search

+1 -1
View File
@@ -1 +1 @@
{"1": {"name": "on", "pattern": "on", "colors": ["#FFFFFF"], "brightness": 255, "delay": 100, "n1": 0, "n2": 0, "n3": 0, "n4": 0, "n5": 0, "n6": 0, "n7": 0, "n8": 0, "profile_id": "1", "auto": true, "id": "1"}, "2": {"name": "off", "pattern": "off", "colors": [], "brightness": 0, "delay": 100, "n1": 0, "n2": 0, "n3": 0, "n4": 0, "n5": 0, "n6": 0, "n7": 0, "n8": 0, "profile_id": "1", "auto": true, "id": "2"}, "3": {"name": "rainbow", "pattern": "colour_cycle", "colors": [], "brightness": 255, "delay": 100, "n1": 2, "n2": 0, "n3": 0, "n4": 0, "n5": 0, "n6": 0, "n7": 0, "n8": 0, "profile_id": "1", "auto": true, "id": "3"}, "4": {"name": "Colour Cycle", "pattern": "colour_cycle", "colors": ["#FF0000", "#00FF00", "#0000FF"], "brightness": 255, "delay": 100, "n1": 1, "n2": 0, "n3": 0, "n4": 0, "n5": 0, "n6": 0, "n7": 0, "n8": 0, "profile_id": "1", "auto": true, "id": "4"}, "5": {"name": "transition", "pattern": "transition", "colors": ["#FF0000", "#00FF00", "#0000FF"], "brightness": 255, "delay": 500, "n1": 0, "n2": 0, "n3": 0, "n4": 0, "n5": 0, "n6": 0, "n7": 0, "n8": 0, "profile_id": "1", "auto": true, "id": "5"}, "6": {"name": "flicker", "pattern": "flicker", "colors": ["#FFB84D"], "brightness": 255, "delay": 80, "n1": 30, "n2": 0, "n3": 0, "n4": 0, "n5": 0, "n6": 0, "n7": 0, "n8": 0, "profile_id": "1", "auto": true, "id": "6"}, "7": {"name": "flame", "pattern": "flame", "colors": [], "brightness": 255, "delay": 50, "n1": 35, "n2": 2600, "n3": 0, "n4": 0, "n5": 0, "n6": 0, "n7": 0, "n8": 0, "profile_id": "1", "auto": true, "id": "7"}, "8": {"name": "twinkle", "pattern": "twinkle", "colors": ["#78C8FF", "#508CFF", "#B478FF", "#64DCE8", "#A0C8FF"], "brightness": 255, "delay": 55, "n1": 72, "n2": 140, "n3": 2, "n4": 6, "n5": 0, "n6": 0, "n7": 0, "n8": 0, "profile_id": "1", "auto": true, "id": "8"}}
{"1":{"name":"on","pattern":"on","colors":["#FFFFFF"],"brightness":255,"delay":100,"n1":0,"n2":0,"n3":0,"n4":0,"n5":0,"n6":0,"n7":0,"n8":0,"profile_id":"1","auto":true,"id":"1"},"2":{"name":"off","pattern":"off","colors":[],"brightness":0,"delay":100,"n1":0,"n2":0,"n3":0,"n4":0,"n5":0,"n6":0,"n7":0,"n8":0,"profile_id":"1","auto":true,"id":"2"},"3":{"name":"rainbow","pattern":"rainbow","colors":[],"brightness":255,"delay":100,"n1":2,"n2":0,"n3":0,"n4":0,"n5":0,"n6":0,"n7":0,"n8":0,"profile_id":"1","auto":true,"id":"3"},"4":{"name":"Colour Cycle","pattern":"colour_cycle","colors":["#FF0000","#00FF00","#0000FF"],"brightness":255,"delay":100,"n1":1,"n2":0,"n3":0,"n4":0,"n5":0,"n6":0,"n7":0,"n8":0,"profile_id":"1","auto":true,"id":"4"},"5":{"name":"transition","pattern":"transition","colors":["#FF0000","#00FF00","#0000FF"],"brightness":255,"delay":500,"n1":0,"n2":0,"n3":0,"n4":0,"n5":0,"n6":0,"n7":0,"n8":0,"profile_id":"1","auto":true,"id":"5"},"6":{"name":"flicker","pattern":"flicker","colors":["#FFB84D"],"brightness":255,"delay":80,"n1":30,"n2":0,"n3":0,"n4":0,"n5":0,"n6":0,"n7":0,"n8":0,"profile_id":"1","auto":true,"id":"6"},"7":{"name":"flame","pattern":"flame","colors":[],"brightness":255,"delay":50,"n1":35,"n2":2600,"n3":0,"n4":0,"n5":0,"n6":0,"n7":0,"n8":0,"profile_id":"1","auto":true,"id":"7"},"8":{"name":"twinkle","pattern":"twinkle","colors":["#78C8FF","#508CFF","#B478FF","#64DCE8","#A0C8FF"],"brightness":255,"delay":55,"n1":72,"n2":140,"n3":2,"n4":6,"n5":0,"n6":0,"n7":0,"n8":0,"profile_id":"1","auto":true,"id":"8"}}
+1 -1
View File
@@ -93,7 +93,7 @@ A profile is `{name, type: "zones", zones: [zone ids], scenes, palette_id}`.
| GET | `/profiles/<id>/export` | Profile bundle (see Import and export) |
| POST | `/profiles/import` | Body: `{"bundle": {...}, "name"?, "apply"? (default true)}` or the bundle itself. Reply `{"<id>": profile, "id": "<id>"}`, 201 |
A store with no profile gets one at boot, `default`, with the same seed, a starter palette and an empty `default` group that its zone plays to. A new profile gets an empty palette, eight presets (`on`, `off`, `rainbow` (`colour_cycle`), `Colour Cycle`, `transition`, `flicker`, `flame`, `twinkle`) and a zone `default` that lists them and names driver `1`. With `seed_dj_zone` it also gets three `DJ …` presets in a zone `dj` naming driver `dj`.
A store with no profile gets one at boot, `default`, with the same seed, a starter palette and an empty `default` group that its zone plays to. A new profile gets an empty palette, eight presets (`on`, `off`, `rainbow`, `Colour Cycle`, `transition`, `flicker`, `flame`, `twinkle`) and a zone `default` that lists them and names driver `1`. With `seed_dj_zone` it also gets three `DJ …` presets in a zone `dj` naming driver `dj`.
## Presets — `/presets`
+56 -3
View File
@@ -139,10 +139,24 @@ static esp_err_t write_file(const char *base, const char *code, size_t len)
return ok ? ESP_OK : ESP_FAIL;
}
// GET /patterns and /patterns/definitions: stored metadata plus {} for every other pattern file
static esp_err_t list_patterns(httpd_req_t *req)
// Patterns the driver has built in (no .py file); the seed presets use them
static const struct {
const char *name, *json;
} BUILTIN[] = {
{ "on", "{\"min_delay\":10,\"max_delay\":10000,\"max_colors\":1}" },
{ "off", "{\"min_delay\":10,\"max_delay\":10000,\"max_colors\":0}" },
{ "rainbow", "{\"n1\":\"Step Rate\",\"min_delay\":10,\"max_delay\":10000,\"max_colors\":0}" },
};
// Stored metadata plus the built-ins and {} for every other pattern file, as {name: {...}}
static cJSON *all_patterns(void)
{
cJSON *o = cJSON_Duplicate(store_coll("pattern"), true);
for (size_t i = 0; i < sizeof(BUILTIN) / sizeof(BUILTIN[0]); i++) {
if (!cJSON_GetObjectItem(o, BUILTIN[i].name)) {
cJSON_AddItemToObject(o, BUILTIN[i].name, cJSON_Parse(BUILTIN[i].json));
}
}
cJSON *files = file_names(), *it;
cJSON_ArrayForEach(it, files)
{
@@ -151,7 +165,38 @@ static esp_err_t list_patterns(httpd_req_t *req)
}
}
cJSON_Delete(files);
return api_json(req, "200 OK", o, true);
return o;
}
// GET /patterns and /patterns/definitions: every pattern with its definition
static esp_err_t list_patterns(httpd_req_t *req)
{
return api_json(req, "200 OK", all_patterns(), true);
}
// GET /patterns/names: just the names, for a pattern picker that loads definitions one at a time
static esp_err_t list_pattern_names(httpd_req_t *req)
{
cJSON *all = all_patterns(), *names = cJSON_CreateArray(), *it;
cJSON_ArrayForEach(it, all)
{
cJSON_AddItemToArray(names, cJSON_CreateString(it->string));
}
cJSON_Delete(all);
return api_json(req, "200 OK", names, true);
}
// GET /patterns/<name>: one pattern's definition ({} when it has none)
static esp_err_t get_pattern(httpd_req_t *req, const char *name)
{
const cJSON *rec = cJSON_GetObjectItem(store_coll("pattern"), name);
for (size_t i = 0; !rec && i < sizeof(BUILTIN) / sizeof(BUILTIN[0]); i++) {
if (!strcmp(BUILTIN[i].name, name)) {
cJSON *o = cJSON_Parse(BUILTIN[i].json);
return api_json(req, "200 OK", o, true);
}
}
return api_json(req, "200 OK", rec ? (cJSON *)rec : cJSON_CreateObject(), !rec);
}
// POST /patterns {name, data: {...}, ...}: data (plus any other keys) becomes the record under name
@@ -452,6 +497,14 @@ bool patterns_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
*ret = list_patterns(req);
return true;
}
if (method == HTTP_GET && n == 2 && !strcmp(seg[1], "names")) {
*ret = list_pattern_names(req);
return true;
}
if (method == HTTP_GET && n == 2 && strcmp(seg[1], "ota") && strcmp(seg[1], "upload") && strcmp(seg[1], "driver")) {
*ret = get_pattern(req, seg[1]);
return true;
}
if (method != HTTP_POST) {
return false;
}
+1 -1
View File
@@ -164,7 +164,7 @@ typedef struct {
static const seed_t SEED[] = {
{ "on", "on", "[\"#FFFFFF\"]", 255, 100, { 0 } },
{ "off", "off", "[]", 0, 100, { 0 } },
{ "rainbow", "colour_cycle", "[]", 255, 100, { 2 } },
{ "rainbow", "rainbow", "[]", 255, 100, { 2 } },
{ "Colour Cycle", "colour_cycle", "[\"#FF0000\",\"#00FF00\",\"#0000FF\"]", 255, 100, { 1 } },
{ "transition", "transition", "[\"#FF0000\",\"#00FF00\",\"#0000FF\"]", 255, 500, { 0 } },
{ "flicker", "flicker", "[\"#FFB84D\"]", 255, 80, { 30 } },
+2 -2
View File
@@ -69,9 +69,9 @@ def test_clone(c, profile):
assert c.delete(f"/profiles/{clone}").status_code == 200
def test_seeded_rainbow_uses_colour_cycle(c, profile):
def test_seeded_rainbow_uses_rainbow(c, profile):
presets = c.get("/presets").json().values()
assert next(p for p in presets if p["name"] == "rainbow")["pattern"] == "colour_cycle"
assert next(p for p in presets if p["name"] == "rainbow")["pattern"] == "rainbow"
def test_delete(c):
+45
View File
@@ -0,0 +1,45 @@
// Page-wide cache for the GET requests the editors repeat (profile, palette, zones, presets,
// groups, patterns). Any write (POST/PUT/PATCH/DELETE) empties it, entries expire after TTL_MS,
// and a request with cache: 'no-store' always goes to the controller. Live data (/devices,
// /status, /settings, ...) is never cached.
(function () {
const CACHEABLE = /^\/(profiles\/current|palettes|patterns|presets|zones|groups)(\/|\?|$)/;
const TTL_MS = 30000;
const store = new Map(); // url -> { at, promise }
const nativeFetch = window.fetch.bind(window);
const pathOf = (input) => {
try {
const u = new URL(typeof input === 'string' ? input : input.url, location.origin);
return u.origin === location.origin ? u.pathname + u.search : null;
} catch (_) {
return null;
}
};
window.fetch = function (input, init) {
const method = String((init && init.method) || (input && input.method) || 'GET').toUpperCase();
if (method !== 'GET') {
store.clear();
return nativeFetch(input, init).finally(() => store.clear());
}
const path = pathOf(input);
const noStore = (init && init.cache === 'no-store') || (input && input.cache === 'no-store');
if (!path || noStore || !CACHEABLE.test(path)) {
return nativeFetch(input, init);
}
const hit = store.get(path);
if (hit && Date.now() - hit.at < TTL_MS) {
return hit.promise.then((r) => r.clone());
}
const promise = nativeFetch(input, init).then((r) => {
if (!r.ok) store.delete(path); // never keep an error
return r;
}, (e) => {
store.delete(path);
throw e;
});
store.set(path, { at: Date.now(), promise });
return promise.then((r) => r.clone());
};
})();
+1
View File
@@ -452,6 +452,7 @@ document.addEventListener('DOMContentLoaded', () => {
}
const patterns = await response.json();
renderPatterns(patterns);
document.dispatchEvent(new Event('patternsChanged')); // presets.js caches the definitions
} catch (error) {
console.error('Load patterns failed:', error);
patternsList.innerHTML = '';
+81 -73
View File
@@ -62,7 +62,24 @@ const getCurrentProfileData = async () => {
}
};
const getCurrentProfilePaletteColors = async () => {
// Share one request among callers in a burst (opening the editor asks for the same data several
// times); the result is reused for ttlMs so a later change is still picked up.
const dedupeFetch = (fn, ttlMs) => {
let pending = null;
let doneAt = 0;
return () => {
if (pending && (doneAt === 0 || Date.now() - doneAt < ttlMs)) return pending;
doneAt = 0;
pending = fn();
pending.then(
() => { doneAt = Date.now(); },
() => { pending = null; },
);
return pending;
};
};
const getCurrentProfilePaletteColors = dedupeFetch(async () => {
const profileData = await getCurrentProfileData();
const profile = profileData && profileData.profile;
const paletteId = profile && (profile.palette_id || profile.paletteId);
@@ -75,7 +92,7 @@ const getCurrentProfilePaletteColors = async () => {
} catch (_) {
return [];
}
};
}, 2000);
const resolveColorsWithPaletteRefs = (colors, paletteRefs, paletteColors) => {
const baseColors = Array.isArray(colors) ? colors : [];
@@ -768,21 +785,19 @@ document.addEventListener('DOMContentLoaded', () => {
// Update color section visibility based on pattern
updateColorSectionVisibility();
// Make name and pattern read-only when editing (not when creating new)
// Name is read-only when editing (not when creating new); the pattern can be changed
const isEditing = currentEditId !== null;
presetNameInput.disabled = isEditing;
presetPatternInput.disabled = isEditing;
presetPatternInput.disabled = false;
if (isEditing) {
presetNameInput.style.backgroundColor = '#2a2a2a';
presetNameInput.style.cursor = 'not-allowed';
presetPatternInput.style.backgroundColor = '#2a2a2a';
presetPatternInput.style.cursor = 'not-allowed';
} else {
presetNameInput.style.backgroundColor = '';
presetNameInput.style.cursor = '';
presetPatternInput.style.backgroundColor = '';
presetPatternInput.style.cursor = '';
}
presetPatternInput.style.backgroundColor = '';
presetPatternInput.style.cursor = '';
// Get pattern config to map descriptive names back to n keys
const patternConfig = cachedPatterns && cachedPatterns[patternName];
@@ -1004,59 +1019,58 @@ document.addEventListener('DOMContentLoaded', () => {
return payload;
};
const normalizePatternMap = (raw) => {
if (!raw) return {};
if (typeof raw === 'object' && !Array.isArray(raw)) {
// Support wrapped payloads like { patterns: {...} }.
if (raw.patterns && typeof raw.patterns === 'object' && !Array.isArray(raw.patterns)) {
return raw.patterns;
}
return raw;
}
if (Array.isArray(raw)) {
// Support list payloads like [{name: "blink", ...}, ...].
return raw.reduce((acc, item, idx) => {
if (item && typeof item === 'object') {
const name = item.name || item.id || String(idx);
acc[String(name)] = item;
}
return acc;
}, {});
}
return {};
// The pattern names are fetched once per page and each pattern's definition only when it is
// selected (cachedPatterns[name]). patterns.js fires "patternsChanged" after it creates or edits
// a pattern, which drops both caches.
let patternNamesPromise = null;
const definitionRequests = new Map();
document.addEventListener('patternsChanged', () => {
patternNamesPromise = null;
cachedPatterns = {};
definitionRequests.clear();
});
const fetchPatternNames = () => {
if (patternNamesPromise) return patternNamesPromise;
const promise = (async () => {
const response = await fetch('/patterns/names', { headers: { Accept: 'application/json' } });
if (!response.ok) throw new Error('patterns unavailable');
const names = await response.json();
if (!Array.isArray(names) || !names.length) throw new Error('no patterns');
return names;
})();
patternNamesPromise = promise;
promise.catch(() => {
if (patternNamesPromise === promise) patternNamesPromise = null;
});
return promise;
};
const loadPatterns = async () => {
// Load one pattern's definition into cachedPatterns (once); resolves when it is available
const ensurePatternConfig = (patternName) => {
const name = String(patternName || '').trim();
if (!name || cachedPatterns[name]) return Promise.resolve();
if (!definitionRequests.has(name)) {
const request = fetch(`/patterns/${encodeURIComponent(name)}`, { headers: { Accept: 'application/json' } })
.then((response) => (response.ok ? response.json() : {}))
.then((config) => {
cachedPatterns[name] = config && typeof config === 'object' ? config : {};
})
.catch(() => {
definitionRequests.delete(name);
});
definitionRequests.set(name, request);
}
return definitionRequests.get(name);
};
// want: the pattern to select (a preset's own); defaults to the current selection
const loadPatterns = async (want) => {
if (!presetPatternInput) {
return;
}
try {
// Load pattern definitions from pattern.json
let patternsPayload = null;
let response = await fetch('/patterns/definitions', {
cache: 'no-store',
headers: { Accept: 'application/json' },
});
if (response.ok) {
patternsPayload = await response.json();
}
let normalized = normalizePatternMap(patternsPayload);
if (!Object.keys(normalized).length) {
// Fallback when definitions route is unavailable or returns an empty map.
response = await fetch('/patterns', {
cache: 'no-store',
headers: { Accept: 'application/json' },
});
if (!response.ok) {
return;
}
patternsPayload = await response.json();
normalized = normalizePatternMap(patternsPayload);
}
cachedPatterns = normalized;
const entries = Object.keys(cachedPatterns);
const desiredPattern = presetPatternInput.value;
const entries = await fetchPatternNames();
const desiredPattern = want || presetPatternInput.value;
presetPatternInput.innerHTML = '<option value="">Pattern</option>';
entries.forEach((patternName) => {
const option = document.createElement('option');
@@ -1064,22 +1078,15 @@ document.addEventListener('DOMContentLoaded', () => {
option.textContent = patternName;
presetPatternInput.appendChild(option);
});
if (desiredPattern && cachedPatterns[desiredPattern]) {
presetPatternInput.value = desiredPattern;
} else if (entries.length > 0) {
let defaultPattern = entries[0];
for (const patternName of entries) {
const config = cachedPatterns[patternName];
const hasMapping = config && Object.keys(config).some((key) => {
return typeof key === 'string' && key.startsWith('n');
});
if (hasMapping) {
defaultPattern = patternName;
break;
}
}
presetPatternInput.value = defaultPattern;
if (desiredPattern && !entries.includes(desiredPattern)) {
// A preset's pattern that has no definition must still show (and keep) its own value
const option = document.createElement('option');
option.value = desiredPattern;
option.textContent = desiredPattern;
presetPatternInput.appendChild(option);
}
presetPatternInput.value = desiredPattern || entries[0];
await ensurePatternConfig(presetPatternInput.value);
updatePresetNLabels(presetPatternInput.value);
} catch (error) {
console.warn('Failed to load patterns:', error);
@@ -1194,7 +1201,7 @@ document.addEventListener('DOMContentLoaded', () => {
editButton.addEventListener('click', async () => {
currentEditId = presetId;
currentEditTabId = null;
await loadPatterns();
await loadPatterns(preset && preset.pattern);
const paletteColors = await getCurrentProfilePaletteColors();
const presetForEditor = {
...(preset || {}),
@@ -1563,7 +1570,8 @@ document.addEventListener('DOMContentLoaded', () => {
presetEditorCloseButton.addEventListener('click', closeEditor);
}
if (presetPatternInput) {
presetPatternInput.addEventListener('change', () => {
presetPatternInput.addEventListener('change', async () => {
await ensurePatternConfig(presetPatternInput.value);
updatePresetNLabels(presetPatternInput.value);
// Update color section visibility
updateColorSectionVisibility();
@@ -1838,7 +1846,7 @@ document.addEventListener('DOMContentLoaded', () => {
const { presetId, preset, zoneId } = event.detail;
currentEditId = presetId;
currentEditTabId = zoneId || null;
await loadPatterns();
await loadPatterns(preset && preset.pattern);
const paletteColors = await getCurrentProfilePaletteColors();
setFormValues({
...(preset || {}),
+1
View File
@@ -1302,6 +1302,7 @@
</div>
</div>
<script src="/static/fetch_cache.js"></script>
<script src="/static/zone-devices-panel.js"></script>
<script src="/static/groups.js"></script>
<script src="/static/zones.js"></script>