feat(media_player): per-command script overrides (t44)
The user's remaining universal-media-player dependency: some commands need hardware this integration deliberately does not drive — a speaker switch that owns the volume, an AVR input that has to move with the Shield, a mute that has to hit two devices. One script per command is the escape hatch, and the rule is absolute: a configured command calls its script INSTEAD of the native protocol/ADB path, and with nothing configured every command behaves exactly as it did (the whole existing suite passes untouched, which is what "byte-identical" means here). * const.py owns the option: CONF_COMMAND_SCRIPTS (one dict, keyed by command), SCRIPT_COMMANDS (turn_on, turn_off, volume_up, volume_down, volume_set, volume_mute, select_source) and command_scripts(), the ONE normalizer of the stored shape — options round-trip through HA storage and can be hand-edited, so a value that is not a non-empty string for a known command is dropped instead of reaching the service layer. * media_player.py: async_run_command_script() calls script.turn_on with the script entity and its variables, blocking=True so a failure surfaces HERE — a mapped command that cannot run raises HomeAssistantError naming the command and script rather than falling back silently or doing nothing. Every command method checks its script first; volume_set passes `level` and select_source passes `app_name`. * volume_mute also passes `is_volume_muted`: without it a mute script cannot tell muting from unmuting, which is the case the user asked for. * volume_set and select_source have NO native path (C-VOL; app launching goes through play_media), so they are advertised only when configured — the feature flag is what makes HA offer the command at all. Unset leaves the feature set exactly as it was. * remote.py shares async_run_command_script for POWER: turn_on/turn_off is one configuration and the remote entity is the other route to it, so a script that owns the power sequence owns it on both. * config_flow.py: one entity selector per command (domain: script), stored as the single dict option, prefilled from the entry's options and cleared by saving without a value — the editor is the truth, as with the app map editor. Two things the harness caught, worth recording: `MediaPlayerEntityFeature` has no SET_VOLUME (the real member is VOLUME_SET — the stub enum and the drift engine disagree with a wrong name), and the stub `homeassistant.exceptions` had no HomeAssistantError, so the stub now mirrors the real inheritance (ServiceValidationError derives from it). tests/test_command_scripts.py covers each command mapped and unmapped, the exact variable payloads, the native path untouched when unmapped (protocol key, then the ADB fallthrough with the protocol down), the rejecting script's error, hand-edited option shapes, and the feature set. test_config_flow_ux.py covers the selectors, the stored shape, prefill and clearing. New tests leaked the harness into the next file at first (a missing per-file scope); that is fixed, and tests/test_harness_scoping.py now asserts that every file loading the harness scopes it, so the next such mistake fails where it is made. Verified: 401 passed / 2 skipped, 402 / 1 with androidtvremote2, reverse file order 401 / 2, ruff clean, and the drift engine exits 0 on a real-HA interpreter (23 modules, 59 imports, 157 relative imports, 36 call arguments, 6 awaited HA calls).
This commit is contained in:
1 parent
2e1d162ef4
commit
89c05f1dcd
11 files changed
+627
-21
No files matched your search
@@ -21,6 +21,7 @@ from .const import (
|
||||
CONF_APP_NAME_MAP,
|
||||
CONF_CHANNEL_DIGIT_DELAY,
|
||||
CONF_CHANNEL_SEND_ENTER,
|
||||
CONF_COMMAND_SCRIPTS,
|
||||
CONF_HOST,
|
||||
CONF_JELLYFIN_ENTITY,
|
||||
CONF_LOGCAT_EVENTS,
|
||||
@@ -34,6 +35,9 @@ from .const import (
|
||||
DEFAULT_NAME,
|
||||
DOMAIN,
|
||||
ENTRY_VERSION,
|
||||
SCRIPT_COMMANDS,
|
||||
SCRIPT_FIELD_PREFIX,
|
||||
command_scripts,
|
||||
)
|
||||
from .helpers import entry_host, protocol_cert_paths, seed_adb_enabled, seed_host
|
||||
|
||||
@@ -187,6 +191,10 @@ def _build_selectors():
|
||||
"power": selector.EntitySelector(
|
||||
selector.EntitySelectorConfig(domain=["switch", "binary_sensor"])
|
||||
),
|
||||
# t44: a script entity per command override
|
||||
"script": selector.EntitySelector(
|
||||
selector.EntitySelectorConfig(domain="script")
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
@@ -848,6 +856,18 @@ class NvidiaShieldOptionsFlow(config_entries.OptionsFlowWithReload):
|
||||
delay = None
|
||||
if delay is not None and not (0.05 <= delay <= 1.0):
|
||||
errors["base"] = "invalid_digit_delay"
|
||||
# t44: one selector per command, stored as a single dict option so
|
||||
# the service layer reads one shape. Popped here because _options_dict
|
||||
# is updated with the rest of user_input verbatim below.
|
||||
scripts = {
|
||||
command: script
|
||||
for command in SCRIPT_COMMANDS
|
||||
if (
|
||||
script := str(
|
||||
user_input.pop(f"{SCRIPT_FIELD_PREFIX}{command}", "") or ""
|
||||
).strip()
|
||||
)
|
||||
}
|
||||
app_map_text = user_input.pop(CONF_APP_NAME_MAP, None)
|
||||
icons: dict[str, str] = {}
|
||||
if app_map_text is None:
|
||||
@@ -867,6 +887,8 @@ class NvidiaShieldOptionsFlow(config_entries.OptionsFlowWithReload):
|
||||
# truth, so a save without icons clears them (and a
|
||||
# single-column map stays valid).
|
||||
self._options_dict[CONF_APP_ICON_MAP] = icons
|
||||
# an empty dict clears every override (the editor is the truth)
|
||||
self._options_dict[CONF_COMMAND_SCRIPTS] = scripts
|
||||
return self.async_create_entry(title="", data=self._options_dict)
|
||||
|
||||
sel = _build_selectors()
|
||||
@@ -891,6 +913,16 @@ class NvidiaShieldOptionsFlow(config_entries.OptionsFlowWithReload):
|
||||
recv_key = vol.Optional(CONF_RECEIVER_ENTITY)
|
||||
schema_dict[recv_key] = sel["receiver"]
|
||||
|
||||
# t44: command script overrides — the escape hatch for hardware we do not
|
||||
# drive (a speaker switch owning the volume). Unset = native path as before.
|
||||
configured_scripts = command_scripts(self._options_dict)
|
||||
suggested_scripts = {}
|
||||
for command in SCRIPT_COMMANDS:
|
||||
field = f"{SCRIPT_FIELD_PREFIX}{command}"
|
||||
schema_dict[vol.Optional(field)] = sel["script"]
|
||||
if configured_scripts.get(command):
|
||||
suggested_scripts[field] = configured_scripts[command]
|
||||
|
||||
# P5-2: channel digit behavior (the user-validated §7 flow)
|
||||
schema_dict[vol.Optional(CONF_CHANNEL_DIGIT_DELAY)] = str
|
||||
schema_dict[vol.Optional(CONF_CHANNEL_SEND_ENTER)] = bool
|
||||
@@ -944,7 +976,8 @@ class NvidiaShieldOptionsFlow(config_entries.OptionsFlowWithReload):
|
||||
return self.async_show_form(
|
||||
step_id="init",
|
||||
data_schema=self.add_suggested_values_to_schema(
|
||||
vol.Schema(schema_dict), self._options_dict
|
||||
vol.Schema(schema_dict),
|
||||
{**self._options_dict, **suggested_scripts},
|
||||
),
|
||||
errors=errors,
|
||||
)
|
||||
Reference in new issue
Block a user