feat(intents): the registered-intent engine, the buttons and the send_intent action (t17, part 1)

Second half of the intents feature (docs/INTENTS_PLAN.md §3-§5), engine first: the
options screen that WRITES a registration follows in this same ticket.

WHAT SHIPPED.

* THE MODEL (const.py + media_player.py). A registered intent is ordinary options
  data — a list of dicts {id,name,package,activity,action,categories,data,fields}
  — so it survives a reload and is otherwise inert. `normalise_intent` accepts a
  row only if it names something to launch (a component with the slash Android
  requires, OR an action), `registered_intents` reads the options defensively
  (junk rows dropped, duplicate ids keep the first, the registration cap applied)
  and never touches a shell.
* THE COMMAND. `intent_am_start_command` assembles
  `am start -n <component> -a <action> -c <category>… -d <data> --es/--ei/--ez key value`.
  `{placeholder}` substitution is plain regex replacement over the DATA template
  only — no eval, no format(), no shell interpolation — and every value is
  shlex.quote'd, so a variable whose value is `"; rm -rf /"` reaches the device as
  literal data inside one quoted word. Address validation for adb_command is
  untouched; `adb_command` stays the one raw-shell door.
* HONEST FAILURES, each with a plain message AND a translation key so the button
  and the action both read well: an unknown intent name lists the registered ones,
  a missing REQUIRED field names the field, a variable the intent did not declare
  is REFUSED (nothing silently travels to the device and nothing is silently
  dropped), a value that cannot become its declared type names the field and the
  value, a data template with an unfilled placeholder names the placeholder, ADB
  off/down says the protocol cannot carry intents, a transport that never answered
  says so, and a device that refused the intent surfaces the DEVICE'S OWN message
  ("unable to resolve Intent", "does not exist", "Permission Denial") and raises.
* THE ACTION. `nvidia_shield.send_intent` takes `intent` (a select selector whose
  options are the loaded intents, `custom_value` on) plus `variables`, resolves
  exactly ONE Shield through t14's resolver (one participant, or no_target /
  unsupported_target / ambiguous_target naming the service), and returns the
  device's output under `intent_response`. Registered with
  `SupportsResponse.OPTIONAL` FIRST, because `async_set_service_schema` reads
  `hass.services.supports_response()` to publish the response flag — the t14
  mechanism, re-verified against the installed HA for t17. The description is
  republished on every entry setup, so an options change (which reloads) refreshes
  the dropdown.
* THE BUTTONS. One button per registered intent (name from the registration, id
  from the intent's stable id so a rename does not orphan the entity), pressing it
  sends the intent with the fields' declared defaults and raises on every failure
  instead of doing nothing. Its extra attributes expose what it will send.

TWO MEASURED REPAIRS IN THE SAME HOOK (media_player.py). Both were latent because
the test harness pre-sets `_manager`:
* the service registries were filled BEFORE the entry's manager was assigned, so a
  real entry registered `None` and `adb_command` would crash with AttributeError
  instead of answering. The manager is now consumed first, and both registries are
  filled after it;
* `async_will_remove_from_hass` never dropped the entity's participation (the
  comment said it did), so a removed or unloaded entry left a stale manager and the
  action answered `adb_unavailable` for an entity that no longer exists instead of
  reporting an unsupported target. Both registries are dropped now, keyed by the
  key computed at add time.

Verify: 794 passed / 4 skipped and 795 passed / 3 skipped (androidtvremote2) —
unchanged counts, so nothing regressed; ruff clean.
This commit is contained in:
James Mackay committed 2026-10-12 12:43:10 +01:00
1 parent 2819ee5a49
commit a8041e1428
4 files changed
+785 -32

No files matched your search

+74
View File
@@ -236,6 +236,80 @@ ADB_COMMAND_SERVICE_DESCRIPTION: dict = {
},
}
# ── t17: registered intents (the second half of the intents feature) ──
#
# A registered intent is ordinary OPTIONS data: a list of dicts, one per intent,
# stored under CONF_REGISTERED_INTENTS in JSON-safe form (no sets, no objects).
# It is read by three consumers that must agree about the shape: the options
# screen (which writes it), the button platform (one entity per row) and the
# nvidia_shield.send_intent action (which builds the command). The shape is the
# one docs/INTENTS_PLAN.md §3 fixes:
#
# {id, name, package, activity, action, categories, data, fields}
#
# ``activity`` and ``action`` are alternatives (the plan: a component OR an
# action, optionally with categories), and ``fields`` declares the variables the
# caller may (or must) supply. Only the DATA template carries {placeholders}.
CONF_REGISTERED_INTENTS = "registered_intents"
#: The action that fires a registered intent. Named by the feature, not by the
#: mechanism: the device's own vocabulary is "am start", which the user never
#: types.
SERVICE_SEND_INTENT = "send_intent"
#: t17: the key the device's `am start` output is returned under. Deliberately
#: NOT ``adb_response``: that key belongs to the raw-command action, and a
#: template reading it should not silently start seeing intent output.
INTENT_RESPONSE_KEY = "intent_response"
#: The four field types the plan fixes (v1): string/int/bool map to the device's
#: own flags (``--es``/``--ei``/``--ez``), and ``raw`` passes an extra through
#: verbatim — the documented escape hatch, NOT the default (the plan's §9
#: non-goal is that nothing beyond these four is expressible).
INTENT_FIELD_TYPES = ("string", "int", "bool", "raw")
#: The flag each declared type becomes on ``am start``.
INTENT_TYPE_FLAGS = {
"string": "--es",
"int": "--ei",
"bool": "--ez",
"raw": "--es",
}
#: The explicit "Add new" sentinel of the intents selector. A registered intent
#: id can never collide with it: ids must be slug-shaped (letters, digits,
#: underscore), and this one is not.
INTENT_NEW_SENTINEL = "__new__"
#: The options-flow fields of the per-intent form (one row at a time, the same
#: idiom as the apps editor). The declared fields are edited as TEXT, one
#: ``key:type[:required][=default]`` per line, because that is the shape the user
#: already knows from the apps table and it survives a paste.
INTENT_FIELD_CHOICE = "intent_choice"
INTENT_FIELD_NAME = "intent_name"
INTENT_FIELD_PACKAGE = "intent_package"
INTENT_FIELD_ACTIVITY = "intent_activity"
INTENT_FIELD_ACTION = "intent_action"
INTENT_FIELD_CATEGORIES = "intent_categories"
INTENT_FIELD_DATA = "intent_data"
INTENT_FIELD_DECLARED = "intent_fields"
INTENT_FIELD_CANDIDATE = "intent_candidate"
INTENT_FIELD_SCAN_PACKAGE = "intent_scan_package"
#: How many declared fields one intent may carry, and how many intents an entry
#: may register. Bounded so a hand-edited options blob (or a runaway paste)
#: cannot turn one button press into a command with thousands of flags, and so
#: the options page stays readable.
INTENT_FIELD_LIMIT = 20
INTENT_REGISTRATION_LIMIT = 50
#: The same target picker as adb_command (t14's measured mechanism), reused
#: verbatim: an intent is sent to exactly ONE Shield, selected as its media_player
#: entity, and the runtime description is what renders the picker.
INTENT_SERVICE_TARGET: dict = {
"entity": [{"domain": ADB_COMMAND_TARGET_DOMAIN}]
}
# P5-3 (C-STREAM): live events push — the streaming_shell logcat consumer
# (foreground AppState fast-path). Default OFF; the user flips it on and
# reports stability on their device.