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:
1 parent
b2aab15ebf
commit
c3e4c7855c
8 files changed
+299
-28
No files matched your search
@@ -20,9 +20,14 @@ import entity_stublib
|
||||
import pytest
|
||||
from nsh.const import (
|
||||
CONF_COMMAND_SCRIPTS,
|
||||
CONF_REMOTE_COMMAND_SCRIPT,
|
||||
SCRIPT_VAR_APP_NAME,
|
||||
SCRIPT_VAR_COMMAND,
|
||||
SCRIPT_VAR_DELAY_SECS,
|
||||
SCRIPT_VAR_HOLD_SECS,
|
||||
SCRIPT_VAR_LEVEL,
|
||||
SCRIPT_VAR_MUTED,
|
||||
SCRIPT_VAR_NUM_REPEATS,
|
||||
)
|
||||
from test_command_routing import _remote_entity, make_manager, make_player
|
||||
|
||||
@@ -200,7 +205,7 @@ class TestScriptFailures:
|
||||
"""A mapped command that cannot run must be visible, never a silent no-op."""
|
||||
|
||||
async def test_a_raising_script_surfaces_a_clean_error(self, modules):
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
|
||||
player, _served = _player(modules, {"volume_up": WATCH}, is_on=False)
|
||||
|
||||
@@ -210,13 +215,17 @@ class TestScriptFailures:
|
||||
|
||||
player.hass.services = _FailingServices()
|
||||
|
||||
with pytest.raises(HomeAssistantError) as raised:
|
||||
with pytest.raises(ServiceValidationError) as raised:
|
||||
await player.async_volume_up()
|
||||
|
||||
message = str(raised.value)
|
||||
assert "volume_up" in message
|
||||
assert WATCH in message
|
||||
assert "speaker switch is offline" in message
|
||||
# translated (t47): the command, the script and the cause are placeholders
|
||||
assert raised.value.translation_domain == "nvidia_shield"
|
||||
assert raised.value.translation_key == "command_script_failed"
|
||||
assert raised.value.translation_placeholders == {
|
||||
"action": "volume_up",
|
||||
"script": WATCH,
|
||||
"error": "speaker switch is offline",
|
||||
}
|
||||
# ...and it did NOT quietly fall back to the native path
|
||||
assert player._manager._protocol.key_calls == []
|
||||
assert player._manager._poller.media_calls == []
|
||||
@@ -309,3 +318,126 @@ def test_the_remote_entry_options_are_the_media_player_options(modules):
|
||||
== entity._entry.options[CONF_COMMAND_SCRIPTS]
|
||||
)
|
||||
assert make_manager().entry is None # the harness's manager starts unbound
|
||||
|
||||
|
||||
WATCH_SCRIPT = "script.shield_remote"
|
||||
|
||||
|
||||
class TestRemoteCommandScript:
|
||||
"""t47 B: one optional script for remote.send_command, replacing the key path.
|
||||
|
||||
A remote command is a SEQUENCE (repeats, long presses, delays), so the escape
|
||||
hatch is one script for the whole call rather than a per-key override: an IR
|
||||
bridge, a macro or a second device needs the call, not the keys.
|
||||
"""
|
||||
|
||||
def _entity(self, modules, script=None):
|
||||
entity = _remote_entity(modules)
|
||||
served = []
|
||||
entity.hass = SimpleNamespace(services=_services(served))
|
||||
if script is not None:
|
||||
entity._entry.options[CONF_REMOTE_COMMAND_SCRIPT] = script
|
||||
return entity, served
|
||||
|
||||
async def test_a_configured_script_receives_the_exact_parameters(self, modules):
|
||||
entity, served = self._entity(modules, WATCH_SCRIPT)
|
||||
|
||||
await entity.async_send_command(
|
||||
["HOME", "KEYCODE_DPAD_CENTER"],
|
||||
num_repeats=3,
|
||||
delay_secs=0.25,
|
||||
hold_secs=1.5,
|
||||
)
|
||||
|
||||
assert served == [
|
||||
(
|
||||
"script",
|
||||
"turn_on",
|
||||
{
|
||||
"entity_id": WATCH_SCRIPT,
|
||||
"variables": {
|
||||
SCRIPT_VAR_COMMAND: ["HOME", "KEYCODE_DPAD_CENTER"],
|
||||
SCRIPT_VAR_NUM_REPEATS: 3,
|
||||
SCRIPT_VAR_DELAY_SECS: 0.25,
|
||||
SCRIPT_VAR_HOLD_SECS: 1.5,
|
||||
},
|
||||
},
|
||||
)
|
||||
]
|
||||
assert entity._manager._protocol.key_calls == [] # key path untouched
|
||||
|
||||
async def test_the_service_defaults_are_named_too(self, modules):
|
||||
"""The variable set is fixed: a script can rely on all four being present."""
|
||||
entity, served = self._entity(modules, WATCH_SCRIPT)
|
||||
|
||||
await entity.async_send_command(["home"])
|
||||
|
||||
variables = served[0][2]["variables"]
|
||||
assert variables == {
|
||||
SCRIPT_VAR_COMMAND: ["home"],
|
||||
SCRIPT_VAR_NUM_REPEATS: 1,
|
||||
SCRIPT_VAR_DELAY_SECS: 0.0,
|
||||
SCRIPT_VAR_HOLD_SECS: 0.0,
|
||||
}
|
||||
|
||||
async def test_unset_keeps_the_native_key_path(self, modules):
|
||||
entity, served = self._entity(modules)
|
||||
|
||||
await entity.async_send_command(["HOME"], num_repeats=2, hold_secs=0.0)
|
||||
|
||||
assert served == []
|
||||
assert entity._manager._protocol.key_calls == [
|
||||
("KEYCODE_HOME", "SHORT"),
|
||||
("KEYCODE_HOME", "SHORT"),
|
||||
]
|
||||
|
||||
@pytest.mark.parametrize("stored", ["", " ", None, 7])
|
||||
async def test_a_blank_or_hand_edited_option_means_unset(self, modules, stored):
|
||||
entity, served = self._entity(modules, stored)
|
||||
|
||||
await entity.async_send_command(["HOME"])
|
||||
|
||||
assert served == []
|
||||
assert entity._manager._protocol.key_calls == [("KEYCODE_HOME", "SHORT")]
|
||||
|
||||
async def test_a_raising_script_is_a_translated_error(self, modules):
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
|
||||
entity, _served = self._entity(modules, WATCH_SCRIPT)
|
||||
|
||||
class _FailingServices:
|
||||
async def async_call(self, domain, service, data=None, **kwargs):
|
||||
raise RuntimeError("IR bridge is offline")
|
||||
|
||||
entity.hass.services = _FailingServices()
|
||||
|
||||
with pytest.raises(ServiceValidationError) as raised:
|
||||
await entity.async_send_command(["HOME"])
|
||||
|
||||
assert raised.value.translation_domain == "nvidia_shield"
|
||||
assert raised.value.translation_key == "command_script_failed"
|
||||
assert raised.value.translation_placeholders == {
|
||||
"action": "remote.send_command",
|
||||
"script": WATCH_SCRIPT,
|
||||
"error": "IR bridge is offline",
|
||||
}
|
||||
assert entity._manager._protocol.key_calls == [] # never a silent no-op
|
||||
|
||||
|
||||
def test_both_new_exception_keys_have_copy_in_both_files():
|
||||
"""A raised translation_key with no copy shows the user a bare key."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
root = Path(__file__).resolve().parent.parent / "custom_components" / "nvidia_shield"
|
||||
for name in ("strings.json", "translations/en.json"):
|
||||
exceptions = json.loads((root / name).read_text(encoding="utf-8"))["exceptions"]
|
||||
for key in ("unknown_source", "command_script_failed"):
|
||||
assert exceptions[key]["title"], (name, key)
|
||||
assert exceptions[key]["message"], (name, key)
|
||||
assert "{source}" in json.loads(
|
||||
(root / "strings.json").read_text(encoding="utf-8")
|
||||
)["exceptions"]["unknown_source"]["message"]
|
||||
assert "remote_command_script" in json.loads(
|
||||
(root / "translations/en.json").read_text(encoding="utf-8")
|
||||
)["options"]["step"]["init"]["data"]
|
||||
Reference in new issue
Block a user