Skip to content

Tell each user whether they may operate the panel, and disable the card's controls for those who may not - #376

Merged
cayossarian merged 15 commits into
mainfrom
feat/control-access
Nov 18, 2026
Merged

cayossarian merged 15 commits into
mainfrom
feat/control-access

Conversation

@cayossarian

Copy link
Copy Markdown
Member

The panel topology now tells each user whether they may operate the panel, and the bundled card uses that to disable the controls they may not operate.

Before, with "Who may operate the panel" set to Administrators only, a non-administrator could open a circuit's side panel and toggle its breaker. The control gate refused the change, and the switch stayed showing it.

The topology's control_access

span_panel/panel_topology answers control_access: {may_operate, reason} for the user the WebSocket connection is authenticated as. reason is the first of these that applies:

  • control_disabled: "Who may operate the panel" is Nobody.
  • admin_only: Administrators only, and the user is not an administrator.
  • read_only: the user's Home Assistant permissions allow control of none of the relay switches and priority selects the topology offers. Core refuses such a user before the gate is asked, so this comes from Core's own check, POLICY_CONTROL. Administrators are never checked, as in Core.

may_operate is true exactly when reason is null. The control lock and the relay debounce are runtime state, and are not part of the answer.

One rule for the gate and the topology

  • ControlPolicy.refusal_for in control_gate.py decides the two mode reasons. The gate's before_publish and the topology both call it on the same runtime_data.control_policy object.
  • The gate keeps its order, messages and translation keys, and all 672 input combinations give the same result before and after.
  • The mode's reasons are ControlModeRefusal and the permission reason is PermissionRefusal. Their union, ControlAccessReason, is the wire type, so the gate can never be handed read_only.

Tests

  • Expected answers: every mode for an administrator, an ordinary user and a read-only user, over a real WebSocket.
  • Gate agreement: the topology and a real gate give one answer for each user.
  • Core agreement: a real switch.turn_on and select.select_option are refused by Core exactly where the topology says read_only. A custom policy that allows one relay may operate.
  • End to end: through real setup, a non-admin under Administrators only is told admin_only and refused admin_only_control on a real relay. After switching to All users and reloading, both the topology and the gate let them through.
  • Gate order: a 36-row table covers every mode, lock state, contextless setting and kind of caller.
  • Mutation checks: reordering the gate, handing the topology a different policy, flipping precedence and dropping the admin skip each fail the tests.
  • Docs: a test checks that the two reason enums are disjoint and match the documented reason table.

Card

The bundled card is built from span-card main (SpanPanel/span-card#44). For a user who may not operate the panel, it disables these with the reason:

  • the side panel's relay switch and priority select;
  • every toggle in By Panel, By Activity, By Area and the Lovelace card;
  • Enable Switches.

A change refused anyway, for example while the lock is armed, returns the control to the panel's actual state and shows the gate's message.

Docs

  • websocket-api.md documents control_access and its reasons, and which controls each reason covers.
  • It also documents first_position/last_position and panel_entities.
  • The README says which users see the controls disabled.

Checks

pytest (4317 passed), mypy, ruff and prek run --all-files all pass.

ControlPolicy.refusal_for answers why the mode refuses a caller, judged by whether the caller is an administrator, or None for a caller with no logged-in user, whom only Nobody refuses. The gate asks it once per command and acts on the answer in its own order: Nobody first, then the lock, then contextless control, then Administrators only. Every refusal and its message is unchanged.
…he panel

span_panel/panel_topology answers control_access, {may_operate, reason}, for the user the connection is authenticated as. The reason is what ControlPolicy.refusal_for, the rule the gate refuses a command by, answers for that user under the entry's policy: null, admin_only or control_disabled. The card can then disable the relay and priority controls of a user the gate would refuse. The control lock is not part of it, and contextless control never applies to a WebSocket caller.

A test runs the topology and the gate for every mode and every kind of user and holds them to one answer. The capture topologies gain the key, and the test that an open command answers a non-admin as it answers an administrator now sets aside what the answer says about its asker.
Says what may_operate and reason answer, for whom, and what they leave out: the control lock, the relay debounce and contextless control. Prettier rewraps the paragraph on the package-ratings event, which was committed unwrapped.
…read_only

control_access.reason may also be read_only. Home Assistant refuses a user who is not an administrator control of any entity their permissions do not allow, before the gate is asked, so the card would otherwise offer a read-only user controls that fail.

The new control_access module asks the control mode first and the user's permissions second, over the relay switches and priority selects the topology offers, and reports read_only only where the user may control none of them. Administrators are not checked, as Core does not check them, and a panel offering no circuit control reports no permission refusal. The gate is unchanged.

Tests hold the topology to Core's own refusal of a switch.turn_on and to the gate's refusals, for every mode and every kind of user.
…ion range

control_access gains its third reason, with the order the reasons are reported in and what read_only is checked against. first_position and last_position, which the topology already sends, are named in the example and the top-level table, with what they cover.
…ate the panel

Beside the existing Nobody wording: under Administrators only for anyone but an administrator, and for an account whose Home Assistant permissions do not allow control.
The map is in the response and was named nowhere. It is now in the example and the top-level table, with a table of its roles, when a role is present, and which one stays available while the panel is offline.
…the reason both rules give ControlAccessReason

ControlAccessRefusal held only the control mode's two reasons, while read_only, Core's, is PermissionRefusal. The mode's enum is renamed for what it holds, and ControlAccessReason names the union that control_access.reason carries.

A docs test holds the two enums' values disjoint and, together, equal to the reason table in websocket-api.md.
A table of every mode, lock state, contextless option and kind of caller, with the refusal the gate gives a priority command or none. Written out, so moving one check past another changes a row: admin_only ahead of the lock, or disabled behind the lock and contextless checks, each passed the suite before this.
…ves them

Every other topology test hands the topology and the gate one policy object by construction, so a setup that gave runtime data all_users while the gate enforced admin_only passed the whole suite. That divergence is the bug control_access exists to prevent.

The new test runs the entry's real setup under admin_only, asks the topology as a non-admin and turns a real relay switch on: the topology says admin_only and the gate refuses with admin_only_control. After switching to all_users through the options and the reload it causes, both let the user through.

The lock tests' real-setup harness moves to switch_platform_setup and both use it. Its stand-in client now puts relay commands to the interceptor setup installed, its coordinator holds the real hass and a connected panel, and its rating manager answers as one holding no copy.
…ect too, and of a custom policy

The Core-agreement test asked only switch.turn_on of the relay switch. It now asks select.select_option of the priority select as well. A group whose policy allows control of the relay alone may operate the panel, and Core refuses that group the priority select.
…d where Core refuses

refusal_for no longer claims the two cannot disagree; it says they are decided by one rule and which tests hold them to one answer. The gate's ordering comment places admin_only before the relay debounce rather than last, and control_access says Core raises Unauthorized at the first entity a call names that the user may not control.
… permission case precisely

An empty space is a position in the range that no circuit's tabs include. The panel roles are read from the entity registry, and the battery example says a panel set up without one, which is what was checked. The README's account is one whose permissions allow control of none of the relay and priority controls.
…vers

control_disabled and admin_only hold for every control the integration sends the panel through the gate: circuit relays and priorities, the GFE override, EVSE charge limits and locks, and adopted controls. read_only is judged over the relay switches and priority selects alone, which a consumer other than the card needs to know.

Also renames the gate test for a caller with no user to say plainly what it shows: such a caller is refused only when control is disabled.
Built from span-card main 8c95a56 (SpanPanel/span-card#44), which reads the topology's control_access.
@cayossarian
cayossarian merged commit 37622ca into main Nov 18, 2026
8 checks passed
@cayossarian
cayossarian deleted the feat/control-access branch November 18, 2026 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant