Compare commits
2 Commits
02bdcc5d4b
...
a37eb46003
| Author | SHA1 | Date | |
|---|---|---|---|
| a37eb46003 | |||
| 83153dbddd |
@@ -27,7 +27,7 @@ from .const import (
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORMS: list[Platform] = [Platform.MEDIA_PLAYER, Platform.BUTTON]
|
PLATFORMS: list[Platform] = [Platform.MEDIA_PLAYER, Platform.BUTTON, Platform.NUMBER, Platform.SWITCH]
|
||||||
|
|
||||||
# Service schema for execute_script
|
# Service schema for execute_script
|
||||||
SERVICE_EXECUTE_SCRIPT_SCHEMA = vol.Schema(
|
SERVICE_EXECUTE_SCRIPT_SCHEMA = vol.Schema(
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ from .const import (
|
|||||||
API_BROWSER_FOLDERS,
|
API_BROWSER_FOLDERS,
|
||||||
API_BROWSER_BROWSE,
|
API_BROWSER_BROWSE,
|
||||||
API_BROWSER_PLAY,
|
API_BROWSER_PLAY,
|
||||||
|
API_DISPLAY_MONITORS,
|
||||||
|
API_DISPLAY_BRIGHTNESS,
|
||||||
|
API_DISPLAY_POWER,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@@ -342,6 +345,42 @@ class MediaServerClient:
|
|||||||
"""
|
"""
|
||||||
return await self._request("POST", API_BROWSER_PLAY, {"path": file_path})
|
return await self._request("POST", API_BROWSER_PLAY, {"path": file_path})
|
||||||
|
|
||||||
|
async def get_display_monitors(self) -> list[dict[str, Any]]:
|
||||||
|
"""Get list of connected monitors with brightness and power info.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List of monitor dicts with id, name, brightness, power_supported, power_on, resolution
|
||||||
|
"""
|
||||||
|
return await self._request("GET", f"{API_DISPLAY_MONITORS}?refresh=true")
|
||||||
|
|
||||||
|
async def set_display_brightness(self, monitor_id: int, brightness: int) -> dict[str, Any]:
|
||||||
|
"""Set brightness for a specific monitor.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
monitor_id: Monitor index
|
||||||
|
brightness: Brightness level (0-100)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Response data with success status
|
||||||
|
"""
|
||||||
|
return await self._request(
|
||||||
|
"POST", f"{API_DISPLAY_BRIGHTNESS}/{monitor_id}", {"brightness": brightness}
|
||||||
|
)
|
||||||
|
|
||||||
|
async def set_display_power(self, monitor_id: int, on: bool) -> dict[str, Any]:
|
||||||
|
"""Set power state for a specific monitor.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
monitor_id: Monitor index
|
||||||
|
on: True to turn on, False to turn off
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Response data with success status
|
||||||
|
"""
|
||||||
|
return await self._request(
|
||||||
|
"POST", f"{API_DISPLAY_POWER}/{monitor_id}", {"on": on}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class MediaServerWebSocket:
|
class MediaServerWebSocket:
|
||||||
"""WebSocket client for real-time media status updates."""
|
"""WebSocket client for real-time media status updates."""
|
||||||
|
|||||||
@@ -37,6 +37,9 @@ API_WEBSOCKET = "/api/media/ws"
|
|||||||
API_BROWSER_FOLDERS = "/api/browser/folders"
|
API_BROWSER_FOLDERS = "/api/browser/folders"
|
||||||
API_BROWSER_BROWSE = "/api/browser/browse"
|
API_BROWSER_BROWSE = "/api/browser/browse"
|
||||||
API_BROWSER_PLAY = "/api/browser/play"
|
API_BROWSER_PLAY = "/api/browser/play"
|
||||||
|
API_DISPLAY_MONITORS = "/api/display/monitors"
|
||||||
|
API_DISPLAY_BRIGHTNESS = "/api/display/brightness"
|
||||||
|
API_DISPLAY_POWER = "/api/display/power"
|
||||||
|
|
||||||
# Service names
|
# Service names
|
||||||
SERVICE_EXECUTE_SCRIPT = "execute_script"
|
SERVICE_EXECUTE_SCRIPT = "execute_script"
|
||||||
|
|||||||
@@ -385,7 +385,12 @@ class RemoteMediaPlayerEntity(CoordinatorEntity[MediaPlayerCoordinator], MediaPl
|
|||||||
if self.coordinator.data is None:
|
if self.coordinator.data is None:
|
||||||
return None
|
return None
|
||||||
duration = self.coordinator.data.get("duration")
|
duration = self.coordinator.data.get("duration")
|
||||||
return int(duration) if duration is not None else None
|
if duration is None:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return int(duration)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def media_position(self) -> int | None:
|
def media_position(self) -> int | None:
|
||||||
@@ -393,7 +398,12 @@ class RemoteMediaPlayerEntity(CoordinatorEntity[MediaPlayerCoordinator], MediaPl
|
|||||||
if self.coordinator.data is None:
|
if self.coordinator.data is None:
|
||||||
return None
|
return None
|
||||||
position = self.coordinator.data.get("position")
|
position = self.coordinator.data.get("position")
|
||||||
return int(position) if position is not None else None
|
if position is None:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return int(position)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def media_position_updated_at(self) -> datetime | None:
|
def media_position_updated_at(self) -> datetime | None:
|
||||||
@@ -575,7 +585,14 @@ class RemoteMediaPlayerEntity(CoordinatorEntity[MediaPlayerCoordinator], MediaPl
|
|||||||
raise ValueError("Invalid media_content_id format")
|
raise ValueError("Invalid media_content_id format")
|
||||||
|
|
||||||
# Get folder contents from API
|
# Get folder contents from API
|
||||||
browse_data = await self.coordinator.client.browse_folder(folder_id, path, offset=0, limit=1000)
|
browse_data = await self.coordinator.client.browse_folder(folder_id, path, offset=0, limit=5000)
|
||||||
|
|
||||||
|
# Fetch folder metadata once (not per-item) for building absolute paths
|
||||||
|
folders = await self.coordinator.client.get_media_folders()
|
||||||
|
base_path = folders.get(folder_id, {}).get("path", "")
|
||||||
|
# Detect path separator from server's base_path (Unix vs Windows)
|
||||||
|
separator = '\\' if '\\' in base_path else '/'
|
||||||
|
base_path_clean = base_path.rstrip('/\\')
|
||||||
|
|
||||||
children = []
|
children = []
|
||||||
for item in browse_data.get("items", []):
|
for item in browse_data.get("items", []):
|
||||||
@@ -593,15 +610,8 @@ class RemoteMediaPlayerEntity(CoordinatorEntity[MediaPlayerCoordinator], MediaPl
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif item.get("is_media", False):
|
elif item.get("is_media", False):
|
||||||
# Media file
|
# Media file - build absolute path for playback
|
||||||
# Build absolute path for playback
|
|
||||||
folders = await self.coordinator.client.get_media_folders()
|
|
||||||
base_path = folders[folder_id]["path"]
|
|
||||||
file_path_in_folder = f"{path}/{item['name']}" if path else item['name']
|
file_path_in_folder = f"{path}/{item['name']}" if path else item['name']
|
||||||
# Handle platform path separators
|
|
||||||
separator = '\\' if '\\' in base_path else '/'
|
|
||||||
# Ensure base_path doesn't end with separator to avoid double separators
|
|
||||||
base_path_clean = base_path.rstrip('/\\')
|
|
||||||
absolute_path = f"{base_path_clean}{separator}{file_path_in_folder.replace('/', separator)}"
|
absolute_path = f"{base_path_clean}{separator}{file_path_in_folder.replace('/', separator)}"
|
||||||
|
|
||||||
children.append(
|
children.append(
|
||||||
|
|||||||
110
custom_components/remote_media_player/number.py
Normal file
110
custom_components/remote_media_player/number.py
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
"""Number platform for Remote Media Player integration (display brightness)."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.components.number import NumberEntity, NumberMode
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from .api_client import MediaServerClient, MediaServerError
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entry: ConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Set up display brightness number 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 = [
|
||||||
|
DisplayBrightnessNumber(
|
||||||
|
client=client,
|
||||||
|
entry=entry,
|
||||||
|
monitor=monitor,
|
||||||
|
)
|
||||||
|
for monitor in monitors
|
||||||
|
if monitor.get("brightness") is not None
|
||||||
|
]
|
||||||
|
|
||||||
|
if entities:
|
||||||
|
async_add_entities(entities)
|
||||||
|
_LOGGER.info("Added %d display brightness entities", len(entities))
|
||||||
|
|
||||||
|
|
||||||
|
class DisplayBrightnessNumber(NumberEntity):
|
||||||
|
"""Number entity for controlling display brightness."""
|
||||||
|
|
||||||
|
_attr_has_entity_name = True
|
||||||
|
_attr_native_min_value = 0
|
||||||
|
_attr_native_max_value = 100
|
||||||
|
_attr_native_step = 1
|
||||||
|
_attr_native_unit_of_measurement = "%"
|
||||||
|
_attr_mode = NumberMode.SLIDER
|
||||||
|
_attr_icon = "mdi:brightness-6"
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
client: MediaServerClient,
|
||||||
|
entry: ConfigEntry,
|
||||||
|
monitor: dict[str, Any],
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the display brightness entity."""
|
||||||
|
self._client = client
|
||||||
|
self._entry = entry
|
||||||
|
self._monitor_id: int = monitor["id"]
|
||||||
|
self._monitor_name: str = monitor.get("name", f"Monitor {monitor['id']}")
|
||||||
|
self._resolution: str | None = monitor.get("resolution")
|
||||||
|
self._attr_native_value = monitor.get("brightness")
|
||||||
|
|
||||||
|
# Use resolution in name to disambiguate same-name monitors
|
||||||
|
display_name = self._monitor_name
|
||||||
|
if self._resolution:
|
||||||
|
display_name = f"{self._monitor_name} ({self._resolution})"
|
||||||
|
|
||||||
|
self._attr_unique_id = f"{entry.entry_id}_display_brightness_{self._monitor_id}"
|
||||||
|
self._attr_name = f"Display {display_name} Brightness"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_info(self) -> DeviceInfo:
|
||||||
|
"""Return device info."""
|
||||||
|
return DeviceInfo(
|
||||||
|
identifiers={(DOMAIN, self._entry.entry_id)},
|
||||||
|
name=self._entry.title,
|
||||||
|
manufacturer="Remote Media Player",
|
||||||
|
model="Media Server",
|
||||||
|
)
|
||||||
|
|
||||||
|
async def async_set_native_value(self, value: float) -> None:
|
||||||
|
"""Set the brightness value."""
|
||||||
|
try:
|
||||||
|
await self._client.set_display_brightness(self._monitor_id, int(value))
|
||||||
|
self._attr_native_value = int(value)
|
||||||
|
self.async_write_ha_state()
|
||||||
|
except MediaServerError as err:
|
||||||
|
_LOGGER.error("Failed to set brightness for monitor %d: %s", self._monitor_id, err)
|
||||||
|
|
||||||
|
async def async_update(self) -> None:
|
||||||
|
"""Fetch updated brightness from the server."""
|
||||||
|
try:
|
||||||
|
monitors = await self._client.get_display_monitors()
|
||||||
|
for monitor in monitors:
|
||||||
|
if monitor["id"] == self._monitor_id:
|
||||||
|
self._attr_native_value = monitor.get("brightness")
|
||||||
|
break
|
||||||
|
except MediaServerError as err:
|
||||||
|
_LOGGER.error("Failed to update brightness for monitor %d: %s", self._monitor_id, err)
|
||||||
125
custom_components/remote_media_player/switch.py
Normal file
125
custom_components/remote_media_player/switch.py
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
"""Switch platform for Remote Media Player integration (display power)."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.components.switch import SwitchEntity, SwitchDeviceClass
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from .api_client import MediaServerClient, MediaServerError
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
_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
|
||||||
|
|
||||||
|
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._monitor_name: str = monitor.get("name", f"Monitor {monitor['id']}")
|
||||||
|
self._resolution: str | None = monitor.get("resolution")
|
||||||
|
self._attr_is_on = monitor.get("power_on", True)
|
||||||
|
|
||||||
|
# Use resolution in name to disambiguate same-name monitors
|
||||||
|
display_name = self._monitor_name
|
||||||
|
if self._resolution:
|
||||||
|
display_name = f"{self._monitor_name} ({self._resolution})"
|
||||||
|
|
||||||
|
self._attr_unique_id = f"{entry.entry_id}_display_power_{self._monitor_id}"
|
||||||
|
self._attr_name = f"Display {display_name} Power"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def icon(self) -> str:
|
||||||
|
"""Return icon based on power state."""
|
||||||
|
return "mdi:monitor" if self._attr_is_on else "mdi:monitor-off"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_info(self) -> DeviceInfo:
|
||||||
|
"""Return device info."""
|
||||||
|
return DeviceInfo(
|
||||||
|
identifiers={(DOMAIN, self._entry.entry_id)},
|
||||||
|
name=self._entry.title,
|
||||||
|
manufacturer="Remote Media Player",
|
||||||
|
model="Media Server",
|
||||||
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
Reference in New Issue
Block a user