Wake only for watched entities, registry events, light poll, 2 min keepalive (0.2.20)

The sync loop now wakes only for state changes of entities on a layout or
belonging to a screen. Registries refresh on HA registry events with a
10 minute fallback instead of every 30 s. The page polls a light inventory
(screens + update status) every 10 s and the full catalogues every 5 min or
when the tab becomes visible. The full keepalive interval is 120 s.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Max Gramser
2026-09-13 11:24:17 +02:00
parent b67cd33fb3
commit 8dc5e616f5
6 changed files with 85 additions and 16 deletions
+11 -4
View File
@@ -851,9 +851,11 @@ function renderResults() {
),
);
}
async function refresh() {
async function refresh(full = true) {
try {
inventory = await (await api("inventory")).json();
const data = await (await api(full ? "inventory" : "inventory?light=1")).json();
// A light poll carries only screens and update status; keep the catalogues we have.
inventory = full ? data : { ...inventory, ...data };
$("#connection").textContent = inventory.connected
? "● Home Assistant verbonden"
: "Verbinding met Home Assistant herstellen…";
@@ -1015,17 +1017,22 @@ window.addEventListener("beforeunload", (e) => {
});
refresh();
// Poll only while the tab is visible; a hidden tab would otherwise keep the add-on busy.
let pollTimer;
let pollTimer, lastFull = Date.now();
function poll() {
clearTimeout(pollTimer);
pollTimer = setTimeout(async () => {
if (!document.hidden) await refresh();
if (!document.hidden) {
const full = Date.now() - lastFull >= 300000;
if (full) lastFull = Date.now();
await refresh(full);
}
poll();
}, inventory.updates?.busy ? 3000 : 10000);
}
poll();
document.addEventListener("visibilitychange", async () => {
if (document.hidden) return;
lastFull = Date.now();
await refresh();
poll();
});