refactor: centralize ADB host/key resolution in helpers.py; drop the three duplicated resolvers
This commit is contained in:
1 parent
d3afda0a25
commit
6b4227cb40
4 files changed
+80
-74
No files matched your search
@@ -0,0 +1,60 @@
|
||||
"""Shared config helpers for the NVIDIA Shield integration."""
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from .const import (
|
||||
CONF_ADB_HOST,
|
||||
CONF_ADB_PORT,
|
||||
CONF_ANDROIDTV_ENTITY,
|
||||
DEFAULT_ADB_PORT,
|
||||
DOMAIN,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class AdbConfig:
|
||||
"""Resolved ADB connection settings for a config entry."""
|
||||
|
||||
host: str
|
||||
port: int
|
||||
key_path: str
|
||||
|
||||
|
||||
def adb_key_path(hass: HomeAssistant) -> str:
|
||||
"""Path of the ADB private key for this integration (may not exist yet)."""
|
||||
return os.path.join(hass.config.config_dir, ".storage", DOMAIN, "adbkey")
|
||||
|
||||
|
||||
def discover_adb_host(
|
||||
hass: HomeAssistant, androidtv_entity_id: str | None
|
||||
) -> str | None:
|
||||
"""Best-effort host for an AndroidTV entity, from its owning config entry."""
|
||||
if not androidtv_entity_id:
|
||||
return None
|
||||
registry = er.async_get(hass)
|
||||
entity_entry = registry.async_get(androidtv_entity_id)
|
||||
if not entity_entry or not entity_entry.config_entry_id:
|
||||
return None
|
||||
config_entry = hass.config_entries.async_get_entry(entity_entry.config_entry_id)
|
||||
if config_entry:
|
||||
return config_entry.data.get("host")
|
||||
return None
|
||||
|
||||
|
||||
def resolve_adb_config(hass: HomeAssistant, entry) -> AdbConfig | None:
|
||||
"""Resolve host/port/key_path for the entry's ADB connection, or None.
|
||||
|
||||
Priority: explicit ADB host/port options > host from the AndroidTV
|
||||
entity's own config entry.
|
||||
"""
|
||||
host = entry.options.get(CONF_ADB_HOST) or discover_adb_host(
|
||||
hass, entry.data.get(CONF_ANDROIDTV_ENTITY)
|
||||
)
|
||||
if not host:
|
||||
return None
|
||||
port = int(entry.options.get(CONF_ADB_PORT, DEFAULT_ADB_PORT))
|
||||
return AdbConfig(host=host, port=port, key_path=adb_key_path(hass))
|
||||
Reference in new issue
Block a user