diff --git a/tv_test.py b/tv_test.py index 213cff8..0c7d501 100644 --- a/tv_test.py +++ b/tv_test.py @@ -20,13 +20,24 @@ PERMISSIONS = [ "CONTROL_POWER", "CONTROL_INPUT_TEXT", "CONTROL_INPUT_MEDIA_PLAYBACK", + "CONTROL_INPUT_JOYSTICK", + "CONTROL_INPUT_TV", + "CONTROL_TV_MEDIA", + "CONTROL_MOUSE_AND_KEYBOARD", "READ_INSTALLED_APPS", + "READ_RUNNING_APPS", + "READ_INPUT_DEVICE_LIST", + "READ_TV_CHANNEL_LIST", + "READ_POWER_STATE", "WRITE_NOTIFICATION_TOAST", ] COMMANDS: dict[str, tuple[str, dict[str, Any]]] = { "info": ("ssap://system/getSystemInfo", {}), "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-down": ("ssap://audio/volumeDown", {}), "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") -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.""" payload: dict[str, Any] = { - "forcePairing": False, + "forcePairing": force_pairing, "pairingType": "PROMPT", "manifest": { "manifestVersion": 1, @@ -69,7 +80,7 @@ def build_register_payload(client_key: str | None) -> dict[str, Any]: "localizedAppNames": {"": "webOS Linux Test"}, }, } - if client_key: + if client_key and not force_pairing: payload["client-key"] = client_key return {"type": "register", "id": "register_0", "payload": payload} @@ -80,15 +91,16 @@ async def pair_and_send( uri: str, payload: dict[str, Any], key_file: Path, + re_pair: bool = False, ) -> dict[str, Any]: """Connect to the TV, complete registration/pairing, and send one SSAP request.""" 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" ssl_ctx = build_ssl_context() if scheme == "wss" else None 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: msg = json.loads(await ws.recv()) @@ -123,11 +135,20 @@ def parse_args() -> argparse.Namespace: default=DEFAULT_KEY_FILE, 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) toast_p = sub.add_parser("toast", help="Show a toast notification on the TV.") 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: sub.add_parser(name, help=f"Send {COMMANDS[name][0]}") @@ -143,13 +164,18 @@ def main() -> None: if args.command == "toast": uri = "ssap://system.notifications/createToast" 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": uri = args.uri payload = json.loads(args.payload) else: 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))