From 44703213cff920434b59024e112c945e360d99ca Mon Sep 17 00:00:00 2001 From: kasunben Date: Sat, 22 Aug 2026 01:08:34 +0200 Subject: [PATCH] Add Console UI for RFC-0019's Home Assistant control allowlist Adds an "Allow control" toggle (independent of "Allow Home Assistant") and a per-entity "Control" checkbox next to each entity's existing "Read" checkbox, offered only for light/switch domains and cascaded off when Read is unchecked -- a client-side mirror of the subset invariant write_config() already enforces server-side. Manually verified end-to-end against a stub backend, including reload persistence. Co-Authored-By: Claude Sonnet 5 --- ROADMAP.md | 38 +++--- .../appliance/console/assets/console.js | 111 ++++++++++++++---- .../sovereign/appliance/console/index.html | 20 +++- tests/test_console.py | 63 ++++++++++ 4 files changed, 192 insertions(+), 40 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index ae6bfb5..43453b5 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -643,11 +643,10 @@ Milestone 01.2 will deliver: ### 5. Home Automation Integration -**Status:** 🟑 In progress β€” both slices implemented and unit-tested -(read-only slice also manually verified in Console); real-hardware -qualification of either slice blocked on an external dependency (see -below); Console UI for the second (allowlisted-action) slice's control -toggle/picker not yet built +**Status:** 🟑 In progress β€” both slices implemented, unit-tested, and +manually verified in Console (including the second slice's control +toggle/picker); real-hardware qualification of either slice blocked on +an external dependency (see below) **Depends on:** Qualified capability executor (satisfied β€” see Milestone 4's real-hardware qualification of the confirmation/policy/audit machinery via @@ -715,11 +714,22 @@ never contacts Home Assistant), and success verified against the real service-call response rather than trusting a 200 status alone. Extending the config endpoint's required fields would have silently broken Console's already-shipped read-only "Save connection" button β€” fixed by -having it load, retain, and resend the two new control fields unchanged -even though this pass builds no UI to edit them yet. Console's own -settings UI for the control toggle/picker remains unbuilt, and -real-hardware qualification depends on the same unresolved external -dependency (no Home Assistant instance yet) named above. +having it load, retain, and resend the two new control fields even +before any UI existed to edit them. + +**Console-wired**: an "Allow control" toggle independent of the +existing "Allow Home Assistant" toggle, and a per-entity "Control" +checkbox next to each allowlisted entity's existing "Read" checkbox β€” +offered only for `light`/`switch`-domain entities, disabled until Read +is checked for that entity, and cascaded off if Read is later +unchecked (client-side mirror of the same subset invariant +`write_config()` enforces server-side). Manually verified end-to-end in +a browser against a stub backend: sign in, save a connection, enable +both toggles, load entities, confirm Control is offered only for +light/switch domains, check Read+Control on two entities, save, reload, +and confirm both the toggle and per-entity grants persisted. Real-hardware +qualification depends on the same unresolved external dependency (no +Home Assistant instance yet) named above. ### 6. Full Base-OS Updates @@ -821,10 +831,10 @@ This milestone closes the remaining β€œflash once” gap. Accepted); a real signed-release qualification (artifact/import systemd paths, the real hardened sandbox) remains - 🟑 Home Assistant capability integration β€” read-only entity/history - mapping (RFC-0018) implemented and Console-wired; allowlisted-action - mapping (RFC-0019, `light`/`switch` only) implemented, Console UI for - it still unbuilt; real-hardware qualification of either blocked on no - Home Assistant instance existing on the household network yet + mapping (RFC-0018) and allowlisted-action mapping (RFC-0019, + `light`/`switch` only) both implemented and Console-wired; real-hardware + qualification of either blocked on no Home Assistant instance existing + on the household network yet - βœ… A/B full base-OS updates (RFC-0016): every Acceptance Criteria item hardware-qualified, including a second base-OS update with no reflash, the Console panel against a live transaction, and a genuine diff --git a/image-builder/sovereign/appliance/console/assets/console.js b/image-builder/sovereign/appliance/console/assets/console.js index d8ac479..6be992a 100644 --- a/image-builder/sovereign/appliance/console/assets/console.js +++ b/image-builder/sovereign/appliance/console/assets/console.js @@ -192,6 +192,8 @@ function showSignedIn() { chatWebSearchToggle.disabled = false; loadWebSearchPolicy(); haPolicyRow.hidden = false; + haControlPolicyRow.hidden = false; + haEntitiesHint.hidden = false; setHaFieldsEnabled(true); loadHomeAssistantConfig(); } @@ -214,6 +216,8 @@ function showSignedOut() { chatWebSearchToggle.checked = false; setChatPolicyMessage(""); haPolicyRow.hidden = true; + haControlPolicyRow.hidden = true; + haEntitiesHint.hidden = true; setHaFieldsEnabled(false); resetHomeAssistantSettingsUI(); } @@ -706,6 +710,9 @@ chatWebSearchToggle.addEventListener("change", async () => { // /home-assistant endpoints. const haPolicyRow = document.querySelector("#ha-policy-row"); const haEnabledToggle = document.querySelector("#ha-enabled-toggle"); +const haControlPolicyRow = document.querySelector("#ha-control-policy-row"); +const haControlEnabledToggle = document.querySelector("#ha-control-enabled-toggle"); +const haEntitiesHint = document.querySelector("#ha-entities-hint"); const haBaseUrlScheme = document.querySelector("#ha-base-url-scheme"); const haBaseUrlHost = document.querySelector("#ha-base-url-host"); const haAccessTokenInput = document.querySelector("#ha-access-token"); @@ -719,7 +726,16 @@ const haSaveAllowlistButton = document.querySelector("#ha-save-allowlist"); const haStatusPill = document.querySelector("#ha-status-pill"); const haStatusDetail = document.querySelector("#ha-status-detail"); const haEntityCount = document.querySelector("#ha-entity-count"); -const HA_ENTITIES_PLACEHOLDER = "Sign in, save a connection, then load entities to choose which ones the assistant may read."; +const HA_ENTITIES_PLACEHOLDER = "Sign in, save a connection, then load entities to choose which ones the assistant may read and control."; + +// RFC-0019: the same light/switch restriction the backend enforces +// (write_config(), _control_policy_check, and the implementation itself +// all independently check this) -- mirrored here only to decide whether +// to offer a Control checkbox at all. This is a UI convenience, not a +// security boundary: the real enforcement is server-side, so offering +// (or not) a checkbox here changes nothing about what a bypassed request +// could do. +const HA_CONTROLLABLE_DOMAINS = new Set(["light", "switch"]); // The live-editable allowlist, loaded from the real config and mutated by // the entity checklist below -- resent in full on every save, since @@ -727,14 +743,11 @@ const HA_ENTITIES_PLACEHOLDER = "Sign in, save a connection, then load entities // list (there is no partial-update endpoint). let haAllowlist = []; -// RFC-0019: control_enabled/controllable_entities -- loaded and resent -// unchanged on every save the same way haAllowlist is, even though this -// pass builds no UI to edit them yet (that's Console's own follow-up -// per RFC-0019's Non-Goals). Without this, "Save connection" would -// silently wipe out control settings a household configured through -// some other path (a future settings page, or a direct API call), -// since POST /home-assistant always replaces the whole config object. -let haControlEnabled = false; +// RFC-0019: the live-editable controllable-entities list, same pattern +// as haAllowlist above. control_enabled itself is read directly from +// haControlEnabledToggle.checked at save time, the same way enabled +// already reads from haEnabledToggle.checked -- no separate variable +// needed for a single checkbox's own state. let haControllableEntities = []; function setHaSettingsMessage(text, isError) { @@ -749,6 +762,7 @@ function setHaEntitiesMessage(text, isError) { function setHaFieldsEnabled(enabled) { haEnabledToggle.disabled = !enabled; + haControlEnabledToggle.disabled = !enabled; haBaseUrlScheme.disabled = !enabled; haBaseUrlHost.disabled = !enabled; haAccessTokenInput.disabled = !enabled; @@ -806,12 +820,12 @@ function applyHaBaseUrl(baseUrl) { function resetHomeAssistantSettingsUI() { haEnabledToggle.checked = false; + haControlEnabledToggle.checked = false; haBaseUrlScheme.value = "http"; haBaseUrlHost.value = ""; haAccessTokenInput.value = ""; renderHaTokenStatus(false); haAllowlist = []; - haControlEnabled = false; haControllableEntities = []; haEntitiesList.replaceChildren(); const placeholder = document.createElement("p"); @@ -846,7 +860,7 @@ async function loadHomeAssistantConfig() { haAccessTokenInput.value = ""; renderHaTokenStatus(Boolean(data.has_access_token)); haAllowlist = Array.isArray(data.allowlisted_entities) ? data.allowlisted_entities.slice() : []; - haControlEnabled = Boolean(data.control_enabled); + haControlEnabledToggle.checked = Boolean(data.control_enabled); haControllableEntities = Array.isArray(data.controllable_entities) ? data.controllable_entities.slice() : []; renderHaStatus(data); } catch (error) { @@ -859,7 +873,7 @@ async function saveHomeAssistantConfig(reportTo) { enabled: haEnabledToggle.checked, base_url: buildHaBaseUrl(), allowlisted_entities: haAllowlist, - control_enabled: haControlEnabled, + control_enabled: haControlEnabledToggle.checked, controllable_entities: haControllableEntities, }; // Omitted entirely (not even an empty string) means "leave the stored @@ -884,7 +898,7 @@ async function saveHomeAssistantConfig(reportTo) { haAccessTokenInput.value = ""; renderHaTokenStatus(Boolean(data.has_access_token)); haAllowlist = Array.isArray(data.allowlisted_entities) ? data.allowlisted_entities.slice() : []; - haControlEnabled = Boolean(data.control_enabled); + haControlEnabledToggle.checked = Boolean(data.control_enabled); haControllableEntities = Array.isArray(data.controllable_entities) ? data.controllable_entities.slice() : []; renderHaStatus(data); reportTo("Saved."); @@ -922,8 +936,9 @@ function renderHaEntities(entities) { return; } const allowedSet = new Set(haAllowlist); + const controllableSet = new Set(haControllableEntities); entities.forEach((entity) => { - const row = document.createElement("label"); + const row = document.createElement("div"); row.className = "check-row"; const info = document.createElement("div"); const name = document.createElement("div"); @@ -933,18 +948,74 @@ function renderHaEntities(entities) { detail.className = "detail"; detail.textContent = `${entity.entity_id} Β· ${entity.state || "unknown"}`; info.append(name, detail); - const checkbox = document.createElement("input"); - checkbox.type = "checkbox"; - checkbox.checked = allowedSet.has(entity.entity_id); - checkbox.addEventListener("change", () => { - if (checkbox.checked) { + + const toggles = document.createElement("div"); + toggles.style.display = "flex"; + toggles.style.gap = "16px"; + toggles.style.alignItems = "center"; + + const readLabel = document.createElement("label"); + readLabel.style.display = "inline-flex"; + readLabel.style.gap = "4px"; + readLabel.style.alignItems = "center"; + const readCheckbox = document.createElement("input"); + readCheckbox.type = "checkbox"; + readCheckbox.checked = allowedSet.has(entity.entity_id); + const readText = document.createElement("span"); + readText.className = "detail"; + readText.textContent = "Read"; + readLabel.append(readCheckbox, readText); + toggles.append(readLabel); + + // RFC-0019: control is only ever offered for light/switch entities, + // and only once Read is checked -- you cannot control something you + // haven't also chosen to let the assistant see (the same subset + // invariant write_config() enforces server-side). + const controllable = HA_CONTROLLABLE_DOMAINS.has(entity.domain); + let controlCheckbox = null; + if (controllable) { + const controlLabel = document.createElement("label"); + controlLabel.style.display = "inline-flex"; + controlLabel.style.gap = "4px"; + controlLabel.style.alignItems = "center"; + controlCheckbox = document.createElement("input"); + controlCheckbox.type = "checkbox"; + controlCheckbox.checked = controllableSet.has(entity.entity_id) && readCheckbox.checked; + controlCheckbox.disabled = !readCheckbox.checked; + const controlText = document.createElement("span"); + controlText.className = "detail"; + controlText.textContent = "Control"; + controlLabel.append(controlCheckbox, controlText); + toggles.append(controlLabel); + + controlCheckbox.addEventListener("change", () => { + if (controlCheckbox.checked) { + if (!haControllableEntities.includes(entity.entity_id)) haControllableEntities.push(entity.entity_id); + } else { + haControllableEntities = haControllableEntities.filter((id) => id !== entity.entity_id); + } + }); + } + + readCheckbox.addEventListener("change", () => { + if (readCheckbox.checked) { if (!haAllowlist.includes(entity.entity_id)) haAllowlist.push(entity.entity_id); } else { haAllowlist = haAllowlist.filter((id) => id !== entity.entity_id); + // Unchecking Read must also drop Control -- the backend rejects + // a controllable entity that isn't also allowlisted, so the UI + // enforces the same subset invariant rather than letting a save + // fail with an error the household didn't expect. + if (controlCheckbox) { + controlCheckbox.checked = false; + haControllableEntities = haControllableEntities.filter((id) => id !== entity.entity_id); + } } + if (controlCheckbox) controlCheckbox.disabled = !readCheckbox.checked; haEntityCount.textContent = String(haAllowlist.length); }); - row.append(info, checkbox); + + row.append(info, toggles); haEntitiesList.append(row); }); haSaveAllowlistButton.hidden = false; diff --git a/image-builder/sovereign/appliance/console/index.html b/image-builder/sovereign/appliance/console/index.html index 9273594..5cb98e7 100644 --- a/image-builder/sovereign/appliance/console/index.html +++ b/image-builder/sovereign/appliance/console/index.html @@ -204,13 +204,13 @@

Preview - Home Assistant control (turning things on or off) is part of a later milestone and isn't available on this device yet β€” the recent-operations and capability-scope sections below show that planned design. Reading allowlisted entities and their history is real today; connect it below. + Turning allowlisted lights and switches on or off is real today, below. Broader control (locks, climate, covers, cameras) and the automations/operations ledger are part of a later milestone and aren't available on this device yet β€” the recent-operations and capability-scope sections below show that planned design.
-

Read-only integration

+

Read and light/switch control

Home Assistant

-

What the assistant may read in your home, and the history it can answer questions about.

+

What the assistant may read in your home, the history it can answer questions about, and the lights and switches it may turn on or off.

Not connected @@ -232,6 +232,13 @@

Connection

Reads only the entities you allow below. Leaves your device only when you approve a specific request.
+
@@ -258,12 +265,13 @@

Connection

Allowlist

-

Entities the assistant may read

+

Entities the assistant may read and control

- +
+
-

Sign in, save a connection, then load entities to choose which ones the assistant may read.

+

Sign in, save a connection, then load entities to choose which ones the assistant may read and control.

diff --git a/tests/test_console.py b/tests/test_console.py index 7640b69..2dece00 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -532,21 +532,55 @@ def test_home_assistant_settings_start_hidden_and_disabled_in_markup(self): self.assertRegex(home_section, r'id="ha-save-allowlist"[^>]*hidden') self.assertEqual(home_section.count('type="password"'), 1) + def test_home_assistant_control_settings_start_hidden_and_disabled_in_markup(self): + html = HTML.read_text() + home_section = html[html.index('id="page-home"') : html.index('id="page-activity"')] + + self.assertRegex(home_section, r'id="ha-control-policy-row"[^>]*hidden') + self.assertRegex(home_section, r'id="ha-control-enabled-toggle"[^>]*disabled') + self.assertRegex(home_section, r'id="ha-entities-hint"[^>]*hidden') + def test_home_assistant_settings_shown_and_loaded_on_sign_in_hidden_and_reset_on_sign_out(self): javascript = JAVASCRIPT.read_text() signed_in_start = javascript.index("function showSignedIn") signed_in_block = javascript[signed_in_start : javascript.index("\n}", signed_in_start)] self.assertIn("haPolicyRow.hidden = false", signed_in_block) + self.assertIn("haControlPolicyRow.hidden = false", signed_in_block) + self.assertIn("haEntitiesHint.hidden = false", signed_in_block) self.assertIn("setHaFieldsEnabled(true)", signed_in_block) self.assertIn("loadHomeAssistantConfig()", signed_in_block) signed_out_start = javascript.index("function showSignedOut") signed_out_block = javascript[signed_out_start : javascript.index("\n}", signed_out_start)] self.assertIn("haPolicyRow.hidden = true", signed_out_block) + self.assertIn("haControlPolicyRow.hidden = true", signed_out_block) + self.assertIn("haEntitiesHint.hidden = true", signed_out_block) self.assertIn("setHaFieldsEnabled(false)", signed_out_block) self.assertIn("resetHomeAssistantSettingsUI()", signed_out_block) + def test_home_assistant_control_toggle_disabled_state_follows_read_toggle(self): + javascript = JAVASCRIPT.read_text() + + set_fields_start = javascript.index("function setHaFieldsEnabled") + set_fields_block = javascript[set_fields_start : javascript.index("\n}", set_fields_start)] + self.assertIn("haControlEnabledToggle.disabled = !enabled", set_fields_block) + + reset_start = javascript.index("function resetHomeAssistantSettingsUI") + reset_block = javascript[reset_start : javascript.index("\n}", reset_start)] + self.assertIn("haControlEnabledToggle.checked = false", reset_block) + + def test_home_assistant_control_config_is_loaded_and_saved(self): + javascript = JAVASCRIPT.read_text() + + load_start = javascript.index("async function loadHomeAssistantConfig") + load_block = javascript[load_start : javascript.index("\n}", load_start)] + self.assertIn("haControlEnabledToggle.checked = Boolean(data.control_enabled)", load_block) + + save_start = javascript.index("async function saveHomeAssistantConfig") + save_block = javascript[save_start : javascript.index("\nhaSaveConnectionButton", save_start)] + self.assertIn("control_enabled: haControlEnabledToggle.checked", save_block) + def test_home_assistant_config_endpoint_reads_and_writes_authenticated(self): javascript = JAVASCRIPT.read_text() @@ -610,6 +644,35 @@ def test_home_assistant_entities_proxy_is_authenticated_and_never_used_by_defaul self.assertIn('credentials: "same-origin"', load_entities_block) self.assertIn("X-CSRF-Token", load_entities_block) + def test_render_ha_entities_offers_control_only_for_light_and_switch_domains(self): + javascript = JAVASCRIPT.read_text() + + self.assertIn('const HA_CONTROLLABLE_DOMAINS = new Set(["light", "switch"]);', javascript) + + render_start = javascript.index("function renderHaEntities") + render_block = javascript[render_start : javascript.index("\nfunction ", render_start + 1)] + self.assertIn("const controllable = HA_CONTROLLABLE_DOMAINS.has(entity.domain);", render_block) + # Control must never be offered, let alone checked, for a + # non-controllable domain -- only built inside the `if (controllable)` branch. + self.assertIn("if (controllable) {", render_block) + + # Unchecking Read must cascade: uncheck Control, remove the + # entity from haControllableEntities, and re-disable the + # checkbox -- otherwise a stale control grant could survive + # a household member revoking read access to the same entity. + read_listener_start = render_block.index('readCheckbox.addEventListener("change"') + read_listener_block = render_block[read_listener_start : render_block.index("});", read_listener_start)] + self.assertIn("controlCheckbox.checked = false;", read_listener_block) + self.assertIn( + "haControllableEntities = haControllableEntities.filter((id) => id !== entity.entity_id);", + read_listener_block, + ) + self.assertIn("controlCheckbox.disabled = !readCheckbox.checked;", read_listener_block) + + # The Control checkbox itself starts disabled whenever Read + # isn't already checked for that entity. + self.assertIn("controlCheckbox.disabled = !readCheckbox.checked;", render_block) + def test_external_capability_names_include_home_assistant(self): javascript = JAVASCRIPT.read_text() external_start = javascript.index("const EXTERNAL_CAPABILITY_NAMES")