ab0585278c
- Introduce DisplayCoordinator polling /api/display/monitors once per cycle and fan out to all per-display entities via CoordinatorEntity. Removes ~9x redundant requests per polling cycle that came from each binary_sensor/number/select/sensor/switch entity calling get_display_monitors() in its own async_update. - Optimistic write-through via coordinator.apply_optimistic(...) keeps sibling entities in sync after slider/select writes without an extra network round-trip. - Make CONF_TOKEN optional. The media server already supports running without auth (auth_enabled() returns False when api_tokens is empty), so the integration omits the Authorization header and ?token= query from REST/WS/album-art URLs when no token is configured. Server-side auth-enabled rejections still surface as invalid_auth in the UI. - Bump manifest version to 0.3.2. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
"""Diagnostic sensors exposed per display (resolution, etc.)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from homeassistant.components.sensor import SensorEntity
|
|
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
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Set up per-display sensor entities."""
|
|
coordinator: DisplayCoordinator = hass.data[DOMAIN][entry.entry_id][
|
|
"display_coordinator"
|
|
]
|
|
|
|
if not coordinator.data:
|
|
return
|
|
|
|
entities = [
|
|
DisplayResolutionSensor(coordinator, entry, monitor)
|
|
for monitor in coordinator.data.values()
|
|
if monitor.get("resolution")
|
|
]
|
|
|
|
if entities:
|
|
async_add_entities(entities)
|
|
_LOGGER.info("Added %d display sensor entities", len(entities))
|
|
|
|
|
|
class DisplayResolutionSensor(CoordinatorEntity[DisplayCoordinator], SensorEntity):
|
|
"""Diagnostic sensor reporting the EDID-derived display resolution."""
|
|
|
|
_attr_has_entity_name = True
|
|
_attr_name = "Resolution"
|
|
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
|
_attr_icon = "mdi:monitor-screenshot"
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: DisplayCoordinator,
|
|
entry: ConfigEntry,
|
|
monitor: dict[str, Any],
|
|
) -> None:
|
|
super().__init__(coordinator)
|
|
self._monitor_id: int = monitor["id"]
|
|
self._attr_unique_id = f"{entry.entry_id}_display_resolution_{self._monitor_id}"
|
|
self._attr_device_info = display_device_info(entry, monitor)
|
|
|
|
@property
|
|
def native_value(self) -> str | None:
|
|
if self.coordinator.data is None:
|
|
return None
|
|
return self.coordinator.data.get(self._monitor_id, {}).get("resolution")
|