4156dedf5e
Restructure how displays are exposed in Home Assistant:
Each physical monitor is now its own HA device linked to the media-server
hub via `via_device`. The hub keeps the media_player + script buttons; per-
display devices hold the power switch, brightness slider, and the new
capability entities. This lets users place displays in their own area/room
and keeps related entities grouped together in the UI.
New platforms:
- sensor: DisplayResolutionSensor (diagnostic, from EDID)
- binary_sensor: DisplayPrimaryBinarySensor + DisplayPowerControlBinarySensor
(both diagnostic; help users see why a power switch is or isn't created)
- select: DisplayInputSourceSelect (HDMI1/DP1/...), DisplayColorPresetSelect
(color temperature), DisplayPictureModeSelect (VCP 0xDC scene modes)
- number: added DisplayContrastNumber alongside brightness
Other changes:
- display_device helper centralises the per-display DeviceInfo; pulls real
manufacturer/model from EDID; device name no longer prepends the hub
title since via_device already shows the hierarchy.
- api_client gains set_display_{contrast,input_source,color_preset,picture_mode}
and stops forcing `?refresh=true` on every poll so HA can ride the
server's TTL cache instead of triggering full DDC/CI probes per entity.
- select / number entities now check the server's `success` flag and re-
sync from the actual monitor state when a write was silently rejected
(some monitors honor reads but ignore writes for certain DDC/CI codes).
Bumps manifest.json to 0.3.0 - the device topology change is user-visible
and existing brightness/power entities migrate to per-display devices on
first reload (unique_ids are preserved).
77 lines
2.5 KiB
Python
77 lines
2.5 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 .api_client import MediaServerClient, MediaServerError
|
|
from .const import DOMAIN
|
|
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."""
|
|
client: MediaServerClient = hass.data[DOMAIN][entry.entry_id]["client"]
|
|
|
|
try:
|
|
monitors = await client.get_display_monitors()
|
|
except MediaServerError as err:
|
|
_LOGGER.error("Failed to fetch display monitors: %s", err)
|
|
return
|
|
|
|
entities = [
|
|
DisplayResolutionSensor(client, entry, monitor)
|
|
for monitor in monitors
|
|
if monitor.get("resolution")
|
|
]
|
|
|
|
if entities:
|
|
async_add_entities(entities)
|
|
_LOGGER.info("Added %d display sensor entities", len(entities))
|
|
|
|
|
|
class DisplayResolutionSensor(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,
|
|
client: MediaServerClient,
|
|
entry: ConfigEntry,
|
|
monitor: dict[str, Any],
|
|
) -> None:
|
|
"""Initialize the display resolution sensor."""
|
|
self._client = client
|
|
self._monitor_id: int = monitor["id"]
|
|
self._attr_native_value = monitor.get("resolution")
|
|
self._attr_unique_id = f"{entry.entry_id}_display_resolution_{self._monitor_id}"
|
|
self._attr_device_info = display_device_info(entry, monitor)
|
|
|
|
async def async_update(self) -> None:
|
|
"""Refresh resolution from the server (rarely changes)."""
|
|
try:
|
|
monitors = await self._client.get_display_monitors()
|
|
for monitor in monitors:
|
|
if monitor["id"] == self._monitor_id:
|
|
self._attr_native_value = monitor.get("resolution")
|
|
break
|
|
except MediaServerError as err:
|
|
_LOGGER.error("Failed to refresh resolution for monitor %d: %s", self._monitor_id, err)
|