feat(tv_test): expand SSAP permissions, add --re-pair, foreground, inputs, audio, and launch commands

- Expand PERMISSIONS to include READ_RUNNING_APPS, READ_INPUT_DEVICE_LIST, CONTROL_INPUT_TV, READ_POWER_STATE, and input/media scopes
- Add --re-pair flag to force a fresh TV pairing prompt after permission changes
- Add foreground, inputs, audio, and launch subcommands to tv_test.py
This commit is contained in:
kiskaadee committed 2026-11-14 22:05:37 -05:00
1 parent 9cd722ed49
commit e8244dc084
1 file changed
+32 -6
+32 -6
View File
@@ -20,13 +20,24 @@ PERMISSIONS = [
"CONTROL_POWER", "CONTROL_POWER",
"CONTROL_INPUT_TEXT", "CONTROL_INPUT_TEXT",
"CONTROL_INPUT_MEDIA_PLAYBACK", "CONTROL_INPUT_MEDIA_PLAYBACK",
"CONTROL_INPUT_JOYSTICK",
"CONTROL_INPUT_TV",
"CONTROL_TV_MEDIA",
"CONTROL_MOUSE_AND_KEYBOARD",
"READ_INSTALLED_APPS", "READ_INSTALLED_APPS",
"READ_RUNNING_APPS",
"READ_INPUT_DEVICE_LIST",
"READ_TV_CHANNEL_LIST",
"READ_POWER_STATE",
"WRITE_NOTIFICATION_TOAST", "WRITE_NOTIFICATION_TOAST",
] ]
COMMANDS: dict[str, tuple[str, dict[str, Any]]] = { COMMANDS: dict[str, tuple[str, dict[str, Any]]] = {
"info": ("ssap://system/getSystemInfo", {}), "info": ("ssap://system/getSystemInfo", {}),
"apps": ("ssap://com.webos.applicationManager/listLaunchPoints", {}), "apps": ("ssap://com.webos.applicationManager/listLaunchPoints", {}),
"foreground": ("ssap://com.webos.applicationManager/getForegroundAppInfo", {}),
"inputs": ("ssap://tv/getExternalInputList", {}),
"audio": ("ssap://audio/getStatus", {}),
"vol-up": ("ssap://audio/volumeUp", {}), "vol-up": ("ssap://audio/volumeUp", {}),
"vol-down": ("ssap://audio/volumeDown", {}), "vol-down": ("ssap://audio/volumeDown", {}),
"mute": ("ssap://audio/setMute", {"mute": True}), "mute": ("ssap://audio/setMute", {"mute": True}),
@@ -57,10 +68,10 @@ def save_client_key(key_file: Path, ip: str, client_key: str) -> None:
key_file.write_text(json.dumps(keys, indent=2) + "\n", encoding="utf-8") key_file.write_text(json.dumps(keys, indent=2) + "\n", encoding="utf-8")
def build_register_payload(client_key: str | None) -> dict[str, Any]: def build_register_payload(client_key: str | None, force_pairing: bool = False) -> dict[str, Any]:
"""Build the SSAP register handshake payload.""" """Build the SSAP register handshake payload."""
payload: dict[str, Any] = { payload: dict[str, Any] = {
"forcePairing": False, "forcePairing": force_pairing,
"pairingType": "PROMPT", "pairingType": "PROMPT",
"manifest": { "manifest": {
"manifestVersion": 1, "manifestVersion": 1,
@@ -69,7 +80,7 @@ def build_register_payload(client_key: str | None) -> dict[str, Any]:
"localizedAppNames": {"": "webOS Linux Test"}, "localizedAppNames": {"": "webOS Linux Test"},
}, },
} }
if client_key: if client_key and not force_pairing:
payload["client-key"] = client_key payload["client-key"] = client_key
return {"type": "register", "id": "register_0", "payload": payload} return {"type": "register", "id": "register_0", "payload": payload}
@@ -80,15 +91,16 @@ async def pair_and_send(
uri: str, uri: str,
payload: dict[str, Any], payload: dict[str, Any],
key_file: Path, key_file: Path,
re_pair: bool = False,
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Connect to the TV, complete registration/pairing, and send one SSAP request.""" """Connect to the TV, complete registration/pairing, and send one SSAP request."""
keys = load_client_keys(key_file) keys = load_client_keys(key_file)
saved_key = keys.get(ip) saved_key = None if re_pair else keys.get(ip)
scheme = "wss" if port == 3001 else "ws" scheme = "wss" if port == 3001 else "ws"
ssl_ctx = build_ssl_context() if scheme == "wss" else None ssl_ctx = build_ssl_context() if scheme == "wss" else None
async with websockets.connect(f"{scheme}://{ip}:{port}", ssl=ssl_ctx) as ws: async with websockets.connect(f"{scheme}://{ip}:{port}", ssl=ssl_ctx) as ws:
await ws.send(json.dumps(build_register_payload(saved_key))) await ws.send(json.dumps(build_register_payload(saved_key, force_pairing=re_pair)))
while True: while True:
msg = json.loads(await ws.recv()) msg = json.loads(await ws.recv())
@@ -123,11 +135,20 @@ def parse_args() -> argparse.Namespace:
default=DEFAULT_KEY_FILE, default=DEFAULT_KEY_FILE,
help="Path to store client-key (default: .client_key.json)", help="Path to store client-key (default: .client_key.json)",
) )
parser.add_argument(
"--re-pair",
action="store_true",
help="Force a new pairing prompt on the TV (e.g. after expanding permissions).",
)
sub = parser.add_subparsers(dest="command", required=True) sub = parser.add_subparsers(dest="command", required=True)
toast_p = sub.add_parser("toast", help="Show a toast notification on the TV.") toast_p = sub.add_parser("toast", help="Show a toast notification on the TV.")
toast_p.add_argument("message", nargs="?", default="Hello from Linux!") toast_p.add_argument("message", nargs="?", default="Hello from Linux!")
launch_p = sub.add_parser("launch", help="Launch an app by ID (e.g. youtube.leanback.v4).")
launch_p.add_argument("app_id", help="App ID from `apps` output.")
launch_p.add_argument("--params", default="{}", help="Optional JSON params for the app.")
for name in COMMANDS: for name in COMMANDS:
sub.add_parser(name, help=f"Send {COMMANDS[name][0]}") sub.add_parser(name, help=f"Send {COMMANDS[name][0]}")
@@ -143,13 +164,18 @@ def main() -> None:
if args.command == "toast": if args.command == "toast":
uri = "ssap://system.notifications/createToast" uri = "ssap://system.notifications/createToast"
payload: dict[str, Any] = {"message": args.message} payload: dict[str, Any] = {"message": args.message}
elif args.command == "launch":
uri = "ssap://system.launcher/launch"
payload = {"id": args.app_id, "params": json.loads(args.params)}
elif args.command == "raw": elif args.command == "raw":
uri = args.uri uri = args.uri
payload = json.loads(args.payload) payload = json.loads(args.payload)
else: else:
uri, payload = COMMANDS[args.command] uri, payload = COMMANDS[args.command]
response = asyncio.run(pair_and_send(args.ip, args.port, uri, payload, args.key_file)) response = asyncio.run(
pair_and_send(args.ip, args.port, uri, payload, args.key_file, re_pair=args.re_pair)
)
print(json.dumps(response, indent=2)) print(json.dumps(response, indent=2))