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).
109 lines
3.8 KiB
Python
109 lines
3.8 KiB
Python
"""Switch platform for Remote Media Player integration (display power)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
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 display power switch entities from a config entry."""
|
|
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 = [
|
|
DisplayPowerSwitch(
|
|
client=client,
|
|
entry=entry,
|
|
monitor=monitor,
|
|
)
|
|
for monitor in monitors
|
|
if monitor.get("power_supported", False)
|
|
]
|
|
|
|
if entities:
|
|
async_add_entities(entities)
|
|
_LOGGER.info("Added %d display power switch entities", len(entities))
|
|
|
|
|
|
class DisplayPowerSwitch(SwitchEntity):
|
|
"""Switch entity for controlling display power."""
|
|
|
|
_attr_has_entity_name = True
|
|
_attr_device_class = SwitchDeviceClass.SWITCH
|
|
_attr_name = "Power"
|
|
|
|
def __init__(
|
|
self,
|
|
client: MediaServerClient,
|
|
entry: ConfigEntry,
|
|
monitor: dict[str, Any],
|
|
) -> None:
|
|
"""Initialize the display power switch."""
|
|
self._client = client
|
|
self._entry = entry
|
|
self._monitor_id: int = monitor["id"]
|
|
self._attr_is_on = monitor.get("power_on", True)
|
|
self._attr_unique_id = f"{entry.entry_id}_display_power_{self._monitor_id}"
|
|
self._attr_device_info = display_device_info(entry, monitor)
|
|
|
|
@property
|
|
def icon(self) -> str:
|
|
"""Return icon based on power state."""
|
|
return "mdi:monitor" if self._attr_is_on else "mdi:monitor-off"
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
"""Turn the monitor on."""
|
|
try:
|
|
result = await self._client.set_display_power(self._monitor_id, True)
|
|
if result.get("success"):
|
|
self._attr_is_on = True
|
|
self.async_write_ha_state()
|
|
else:
|
|
_LOGGER.error("Failed to turn on monitor %d", self._monitor_id)
|
|
except MediaServerError as err:
|
|
_LOGGER.error("Failed to turn on monitor %d: %s", self._monitor_id, err)
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
"""Turn the monitor off."""
|
|
try:
|
|
result = await self._client.set_display_power(self._monitor_id, False)
|
|
if result.get("success"):
|
|
self._attr_is_on = False
|
|
self.async_write_ha_state()
|
|
else:
|
|
_LOGGER.error("Failed to turn off monitor %d", self._monitor_id)
|
|
except MediaServerError as err:
|
|
_LOGGER.error("Failed to turn off monitor %d: %s", self._monitor_id, err)
|
|
|
|
async def async_update(self) -> None:
|
|
"""Fetch updated power state from the server."""
|
|
try:
|
|
monitors = await self._client.get_display_monitors()
|
|
for monitor in monitors:
|
|
if monitor["id"] == self._monitor_id:
|
|
self._attr_is_on = monitor.get("power_on", True)
|
|
break
|
|
except MediaServerError as err:
|
|
_LOGGER.error("Failed to update power state for monitor %d: %s", self._monitor_id, err)
|