"""ADB session adapter for NVIDIA Shield. Since Phase 1 this module is a thin adapter on top of ha_adb_common: - ``ensure_adb_key`` (re-exported here for the config flow / Pair ADB button) does the one-time key-material setup — the pairing flow stays untouched. - ``AdbMediaSessionPoller`` remains the protocol-level ADB client used by the ShieldConnectionManager (connection.py) and one-shot pairing connects. The polling loop itself lives in the connection manager, not here. Re-exports MediaSessionInfo and ensure_adb_key for existing importers. """ import logging from .ha_adb_common import ( AdbClientBase, MediaSessionInfo, # noqa: F401 (re-export) parse_foreground, ) from .ha_adb_common import ensure_adb_key as _ensure_adb_key _LOGGER = logging.getLogger(__name__) DOMAIN = "nvidia_shield" def ensure_adb_key(hass_config_dir: str) -> str: """Ensure an ADB key pair exists for the nvidia_shield integration.""" return _ensure_adb_key(hass_config_dir, DOMAIN) class AdbMediaSessionPoller(AdbClientBase): """ADB client for NVIDIA Shield — extends base with wake-state tracking.""" def __init__(self, host: str, port: int, key_path: str): super().__init__(host, port, key_path) async def poll(self, foreground_package: str | None = None) -> MediaSessionInfo | None: """Query the current media session. Returns parsed info or None. Args: foreground_package: The package name of the foreground app, used to prefer its media session over stale sessions from other apps. """ return await self.poll_media_sessions(foreground_package) async def poll_foreground(self) -> str | None: """Foreground package via `dumpsys activity activities` (RESEARCH R4). The grep keeps the dump tiny; the parser handles the R4 line wrap. """ dump = await self.shell("dumpsys activity activities 2>/dev/null | grep -iE 'resumed'") if not dump: return None return parse_foreground(dump)