feat(foreground): foreground process sensors + translation key migration

Adds Home Assistant entities for the foreground-process feature shipped
in the media server, plus migrates existing display entities to use HA
translation keys (strings.json / translations/*) so per-language UI text
flows through the standard locale mechanism.

Foreground entities (all share one HA "Foreground" device linked to the
hub via via_device):
- sensor.foreground_process — process name as state + full payload
  (pid, exec path, window title, fullscreen flag, monitor, geometry,
  is_browser, browser_page_title, browser_url, error) as attributes
- sensor.window_title, sensor.pid, sensor.foreground_monitor,
  sensor.process_started (TIMESTAMP device class)
- binary_sensor.fullscreen, binary_sensor.minimized

Data flow:
- ForegroundCoordinator polls GET /api/foreground every 5s (HTTP fallback)
- media_player's WebSocket receiver forwards `foreground` /
  `foreground_update` push frames into the coordinator via
  apply_websocket_snapshot, so sensors update in near-real-time when WS
  is connected and fall back to polling otherwise

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-05-18 03:13:23 +03:00
parent ab0585278c
commit 9d277276b8
14 changed files with 463 additions and 32 deletions
@@ -31,6 +31,7 @@ from .const import (
API_BROWSER_BROWSE,
API_BROWSER_PLAY,
API_DISPLAY_MONITORS,
API_FOREGROUND,
API_DISPLAY_BRIGHTNESS,
API_DISPLAY_POWER,
API_DISPLAY_CONTRAST,
@@ -420,6 +421,15 @@ class MediaServerClient:
"POST", f"{API_DISPLAY_PICTURE_MODE}/{monitor_id}", {"code": code}
)
async def get_foreground(self) -> dict[str, Any]:
"""Get the foreground window/process snapshot.
Returns the structured payload described in the media server's
``ForegroundInfo`` dataclass: process name, window title, fullscreen
flag, owning monitor, geometry, and process start time.
"""
return await self._request("GET", API_FOREGROUND)
class MediaServerWebSocket:
"""WebSocket client for real-time media status updates."""
@@ -432,6 +442,7 @@ class MediaServerWebSocket:
on_status_update: Callable[[dict[str, Any]], None],
on_disconnect: Callable[[], None] | None = None,
on_scripts_changed: Callable[[], None] | None = None,
on_foreground_update: Callable[[dict[str, Any]], None] | None = None,
) -> None:
"""Initialize the WebSocket client.
@@ -442,6 +453,7 @@ class MediaServerWebSocket:
on_status_update: Callback when status update received
on_disconnect: Callback when connection lost
on_scripts_changed: Callback when scripts have changed
on_foreground_update: Callback when foreground process changes
"""
self._host = host
self._port = int(port)
@@ -449,6 +461,7 @@ class MediaServerWebSocket:
self._on_status_update = on_status_update
self._on_disconnect = on_disconnect
self._on_scripts_changed = on_scripts_changed
self._on_foreground_update = on_foreground_update
# The server's WS endpoint accepts an unauthenticated connection when
# api_tokens is empty (see media.py:websocket_endpoint), so we only
# append ?token=... when one was configured.
@@ -537,6 +550,9 @@ class MediaServerWebSocket:
_LOGGER.info("Scripts changed notification received")
if self._on_scripts_changed:
self._on_scripts_changed()
elif msg_type in ("foreground", "foreground_update"):
if self._on_foreground_update:
self._on_foreground_update(data.get("data", {}))
elif msg_type == "pong":
_LOGGER.debug("Received pong")