feat(remote): optional command script for remote.send_command (t47 B)

One optional 'Remote command script' on the Configure page. Configured, it replaces
the whole key path for remote.send_command; unset, repeats, long presses and delays go
out as protocol keys exactly as before (the existing send_command tests pass
untouched).

A remote command is a SEQUENCE — repeats, a START_LONG/END_LONG pair, an inter-repeat
delay — so the escape hatch is one script for the call rather than per-key overrides:
an IR bridge, a macro, or a second device needs the call, not individual keys. The
script receives the service's own parameters as variables, named as they are on the
service and documented in the README and pinned in tests:

  command       the list of key commands as submitted (e.g. ["HOME", "KEYCODE_DPAD_CENTER"])
  num_repeats   the repeat count (default 1)
  delay_secs    the inter-repeat delay (default 0.0)
  hold_secs     the long-press duration, START_LONG/END_LONG handled by the script
                (default 0.0) — the script owns the key semantics, so it decides what
                "hold" means

All four are always present, including defaults, so a script can rely on the shape.
A raising script surfaces a translated error rather than a silent no-op. That made the
t44 script-failure mechanism consistent in the process: async_call_script() is now the
one caller for both the media player's per-command scripts and the remote's, raising
ServiceValidationError(translation_domain="nvidia_shield",
translation_key="command_script_failed") with {action, script, error} placeholders
instead of a bare HomeAssistantError — the t44 test was updated to assert the
translation contract (domain, key, placeholders) rather than a message string, which is
stronger evidence and gives the user real copy. The new copy lives in BOTH strings
files, as does the unknown_source key from UNIT A.

Option handling reuses the t47 C work: the field is an entity selector (so a cleared
value is tolerated by the schema), the stored value is trimmed, and a blank,
hand-edited or non-string value means "not configured" rather than a call to nowhere.

Verified: 454 passed / 2 skipped, ruff clean, drift engine exit 0 under real HA
(23 modules, 58 imports, 164 relative imports, 36 call arguments, 7 awaited HA calls).
This commit is contained in:
James Mackay committed 2026-10-11 07:07:06 +01:00
1 parent b2aab15ebf
commit c3e4c7855c
8 files changed
+299 -28

No files matched your search

@@ -30,6 +30,7 @@ from .const import (
CONF_PROTOCOL_ENABLED,
CONF_RECEIVER_ENTITY,
CONF_RECEIVER_INPUT,
CONF_REMOTE_COMMAND_SCRIPT,
CONF_SKIP_PAIRING,
DEFAULT_ADB_PORT,
DEFAULT_NAME,
@@ -895,6 +896,11 @@ class NvidiaShieldOptionsFlow(config_entries.OptionsFlowWithReload):
).strip()
)
}
# t47: one optional script for the remote entity — a plain string
# option, unlike the per-command dict above
remote_script = user_input.get(CONF_REMOTE_COMMAND_SCRIPT)
if remote_script is not None:
user_input[CONF_REMOTE_COMMAND_SCRIPT] = str(remote_script).strip()
app_map_text = user_input.pop(CONF_APP_NAME_MAP, None)
icons: dict[str, str] = {}
if app_map_text is None:
@@ -942,6 +948,8 @@ class NvidiaShieldOptionsFlow(config_entries.OptionsFlowWithReload):
# t44: command script overrides — the escape hatch for hardware we do not
# drive (a speaker switch owning the volume). Unset = native path as before.
schema_dict[vol.Optional(CONF_REMOTE_COMMAND_SCRIPT)] = sel["script"]
configured_scripts = command_scripts(self._options_dict)
suggested_scripts = {}
for command in SCRIPT_COMMANDS: