feat(config_flow,media_player): apps name/icon map editor in the options flow (package=name per line, invalid input error) + app_name resolves through CONF_APP_NAME_MAP after the entity/push tiers (P4-1b)
This commit is contained in:
1 parent
1dbb695cec
commit
61c7ac99a3
2 files changed
+63
-4
No files matched your search
@@ -18,6 +18,7 @@ from .const import (
|
||||
CONF_ADB_PORT,
|
||||
CONF_ADB_STORE_APP_INFO,
|
||||
CONF_ANDROIDTV_ENTITY,
|
||||
CONF_APP_NAME_MAP,
|
||||
CONF_JELLYFIN_ENTITY,
|
||||
CONF_PAIRING_CODE,
|
||||
CONF_POWER_ENTITY,
|
||||
@@ -75,6 +76,29 @@ def _find_jellyfin_entity(hass, device_name: str) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
def _app_map_to_text(app_map: dict) -> str:
|
||||
"""Serialize the apps map to one ``package=name`` per line."""
|
||||
return "\n".join(f"{pkg}={name}" for pkg, name in app_map.items())
|
||||
|
||||
|
||||
def _app_map_from_text(text: str) -> dict[str, str] | None:
|
||||
"""Parse one ``package=name`` per line; None on malformed input."""
|
||||
app_map: dict[str, str] = {}
|
||||
for line in text.split("\n"):
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
if "=" not in line:
|
||||
return None
|
||||
package, name = line.split("=", 1)
|
||||
package = package.strip()
|
||||
name = name.strip()
|
||||
if not package or not name or " " in package:
|
||||
return None
|
||||
app_map[package] = name
|
||||
return app_map
|
||||
|
||||
|
||||
def _user_schema_dict(sel, defaults: dict | None = None) -> dict:
|
||||
"""User-step schema — the androidtv entity is optional (standalone).
|
||||
|
||||
@@ -458,13 +482,34 @@ class NvidiaShieldOptionsFlow(config_entries.OptionsFlow):
|
||||
|
||||
async def async_step_init(self, user_input=None):
|
||||
"""Single-page options."""
|
||||
errors = {}
|
||||
if user_input is not None:
|
||||
self._options.update(user_input)
|
||||
return self.async_create_entry(title="", data=self._options)
|
||||
app_map_text = user_input.pop(CONF_APP_NAME_MAP, None)
|
||||
if app_map_text is None:
|
||||
app_map = self._options.get(CONF_APP_NAME_MAP, {})
|
||||
elif app_map_text.strip() == "":
|
||||
app_map = {}
|
||||
else:
|
||||
app_map = _app_map_from_text(app_map_text)
|
||||
if app_map is None:
|
||||
errors["base"] = "invalid_app_map"
|
||||
if not errors:
|
||||
self._options.update(user_input)
|
||||
self._options[CONF_APP_NAME_MAP] = app_map
|
||||
return self.async_create_entry(title="", data=self._options)
|
||||
|
||||
sel = _build_selectors()
|
||||
schema_dict = {}
|
||||
|
||||
# Apps name/icon map editor (P4-1, core androidtv_remote options
|
||||
# pattern, simplified): one package=name per line.
|
||||
current_map = self._options.get(CONF_APP_NAME_MAP, {})
|
||||
if current_map:
|
||||
app_map_default = _app_map_to_text(current_map)
|
||||
schema_dict[vol.Optional(CONF_APP_NAME_MAP, default=app_map_default)] = str
|
||||
else:
|
||||
schema_dict[vol.Optional(CONF_APP_NAME_MAP)] = str
|
||||
|
||||
# Jellyfin
|
||||
jf = self._options.get(CONF_JELLYFIN_ENTITY)
|
||||
jf_key = vol.Optional(CONF_JELLYFIN_ENTITY)
|
||||
@@ -532,4 +577,5 @@ class NvidiaShieldOptionsFlow(config_entries.OptionsFlow):
|
||||
return self.async_show_form(
|
||||
step_id="init",
|
||||
data_schema=vol.Schema(schema_dict),
|
||||
errors=errors,
|
||||
)
|
||||
Reference in new issue
Block a user