fix(sensor): F2 — the Current App sensor shows the resolved app name (t40)

Reported from a live install: the sensor showed ``ar.tvplayer.tv`` while the
media player showed "TiviMate" — the same foreground app, two identities.

The sensor's ``native_value`` returned ``manager.foreground_app`` (the raw
package push) or ``running_apps[0]["package"]`` as-is, while the media player
resolved names through apps map → cached manifest label → androidtv entity tier.
Both now call ONE function, ``resolve_app_name`` (media_player.py), so the two
entities cannot disagree again; the sensor keeps the raw package as its
``app_id`` attribute and ``app_name`` carries the shown name.

Per the contract:
* apps map wins, then the cached manifest label, then the androidtv entity's
  ``app_name``, then the package text itself;
* a bare or unknown package falls back to the package text, NOT None (it used to
  be possible for the value to vanish while the app was plainly running);
* pinned by tests: map tier, label tier, entity tier, the ADB-recents source, the
  unknown-package fallback, and sensor == media player for the same inputs.

Verified: 302 passed / 2 skipped, ruff clean.
This commit is contained in:
James Mackay committed 2026-10-11 06:02:30 +01:00
1 parent be3abae3e7
commit dd23d01a88
3 files changed
+177 -34

No files matched your search

+40 -15
View File
@@ -153,6 +153,36 @@ async def _migrate_legacy_unique_id(hass, entry) -> None:
_LOGGER.info("P5-1: legacy unique_id %r migrated to %r", legacy, new_id)
def resolve_app_name(
package: str | None,
app_name_map,
app_label,
androidtv_app_name: str | None,
) -> str | None:
"""Resolve a package id to a friendly name (t40).
The single chain behind ``media_player.app_name`` and the Current App
sensor (which used to publish the raw package id):
1. the user's explicit apps map (``CONF_APP_NAME_MAP``);
2. the cached device-manifest label (``manager.app_label`` — a cache miss
enqueues the on-demand fetch and falls through);
3. the androidtv entity tier (that entity's ``app_name`` attribute);
4. the raw package text — a bare or unknown package falls back to itself,
never to None.
"""
if package:
mapped = (app_name_map or {}).get(package)
if mapped:
return mapped
label = app_label(package) if app_label is not None else None
if label:
return label
if androidtv_app_name:
return androidtv_app_name
return package or None
class NvidiaShieldMediaPlayer(MediaPlayerEntity):
_attr_device_class = MediaPlayerDeviceClass.TV
@@ -609,22 +639,17 @@ class NvidiaShieldMediaPlayer(MediaPlayerEntity):
3. androidtv entity tier (when configured)
4. raw package (protocol push / ADB foreground parse)
"""
package = self._manager.foreground_app if self._manager else None
if package:
mapped = self._app_name_map.get(package)
if mapped:
return mapped
label = self._manager.app_label(package)
if label:
return label
# t40: one implementation, shared with the Current App sensor
# (``resolve_app_name``) — the two can no longer disagree about what
# is on screen.
package = self._manager.foreground_app if self._manager is not None else None
atv = self._read(self._androidtv_entity) if self._androidtv_entity else None
if atv is not None:
app_name = atv.attributes.get("app_name")
if app_name:
return app_name
if self._manager is not None and package:
return package
return None
return resolve_app_name(
package,
self._app_name_map,
self._manager.app_label if self._manager is not None else None,
atv.attributes.get("app_name") if atv is not None else None,
)
@property
def media_content_type(self):