9d277276b8
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>
123 lines
3.8 KiB
Python
123 lines
3.8 KiB
Python
"""Diagnostic binary sensors per display (primary, DDC/CI power-control support)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity import EntityCategory
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
from .const import DOMAIN
|
|
from .display_coordinator import DisplayCoordinator
|
|
from .display_device import display_device_info
|
|
from .foreground import FOREGROUND_BINARY_SENSORS
|
|
from .foreground_coordinator import ForegroundCoordinator
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Set up display + foreground binary sensor entities."""
|
|
store = hass.data[DOMAIN][entry.entry_id]
|
|
display_coordinator: DisplayCoordinator = store["display_coordinator"]
|
|
foreground_coordinator: ForegroundCoordinator | None = store.get(
|
|
"foreground_coordinator"
|
|
)
|
|
|
|
entities: list[Any] = []
|
|
if display_coordinator.data:
|
|
for monitor in display_coordinator.data.values():
|
|
entities.append(
|
|
DisplayPrimaryBinarySensor(display_coordinator, entry, monitor)
|
|
)
|
|
entities.append(
|
|
DisplayPowerControlBinarySensor(display_coordinator, entry, monitor)
|
|
)
|
|
|
|
if foreground_coordinator is not None:
|
|
entities.extend(
|
|
cls(foreground_coordinator, entry) for cls in FOREGROUND_BINARY_SENSORS
|
|
)
|
|
|
|
if entities:
|
|
async_add_entities(entities)
|
|
_LOGGER.info(
|
|
"Added %d binary sensor entities (display + foreground)", len(entities)
|
|
)
|
|
|
|
|
|
class _DisplayBinarySensorBase(
|
|
CoordinatorEntity[DisplayCoordinator], BinarySensorEntity
|
|
):
|
|
"""Common boilerplate for per-display diagnostic binary sensors."""
|
|
|
|
_attr_has_entity_name = True
|
|
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: DisplayCoordinator,
|
|
entry: ConfigEntry,
|
|
monitor: dict[str, Any],
|
|
) -> None:
|
|
super().__init__(coordinator)
|
|
self._monitor_id: int = monitor["id"]
|
|
self._attr_device_info = display_device_info(entry, monitor)
|
|
|
|
@property
|
|
def _monitor(self) -> dict[str, Any]:
|
|
if self.coordinator.data is None:
|
|
return {}
|
|
return self.coordinator.data.get(self._monitor_id, {})
|
|
|
|
|
|
class DisplayPrimaryBinarySensor(_DisplayBinarySensorBase):
|
|
"""Indicates whether the display is the OS primary monitor."""
|
|
|
|
_attr_translation_key = "primary_display"
|
|
_attr_icon = "mdi:monitor-star"
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: DisplayCoordinator,
|
|
entry: ConfigEntry,
|
|
monitor: dict[str, Any],
|
|
) -> None:
|
|
super().__init__(coordinator, entry, monitor)
|
|
self._attr_unique_id = f"{entry.entry_id}_display_primary_{self._monitor_id}"
|
|
|
|
@property
|
|
def is_on(self) -> bool:
|
|
return bool(self._monitor.get("is_primary"))
|
|
|
|
|
|
class DisplayPowerControlBinarySensor(_DisplayBinarySensorBase):
|
|
"""Indicates whether DDC/CI power control is available for this display."""
|
|
|
|
_attr_translation_key = "power_control_supported"
|
|
_attr_icon = "mdi:power-plug"
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: DisplayCoordinator,
|
|
entry: ConfigEntry,
|
|
monitor: dict[str, Any],
|
|
) -> None:
|
|
super().__init__(coordinator, entry, monitor)
|
|
self._attr_unique_id = (
|
|
f"{entry.entry_id}_display_power_supported_{self._monitor_id}"
|
|
)
|
|
|
|
@property
|
|
def is_on(self) -> bool:
|
|
return bool(self._monitor.get("power_supported"))
|