Add media browser integration for Home Assistant
- Implement async_browse_media() to enable browsing media folders through HA Media Browser UI - Add async_play_media() to handle file playback from media browser - Add play_media_file service for automation support - Add BROWSE_MEDIA and PLAY_MEDIA feature flags - Implement media browser API client methods (get_media_folders, browse_folder, play_media_file) - Fix path separator handling for cross-platform compatibility Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,11 +8,16 @@ from datetime import datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.media_player import (
|
||||
BrowseMedia,
|
||||
MediaPlayerEntity,
|
||||
MediaPlayerEntityFeature,
|
||||
MediaPlayerState,
|
||||
MediaType,
|
||||
)
|
||||
from homeassistant.components.media_player.const import (
|
||||
MediaClass,
|
||||
)
|
||||
from urllib.parse import quote, unquote
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
@@ -303,6 +308,8 @@ class RemoteMediaPlayerEntity(CoordinatorEntity[MediaPlayerCoordinator], MediaPl
|
||||
| MediaPlayerEntityFeature.SEEK
|
||||
| MediaPlayerEntityFeature.TURN_ON
|
||||
| MediaPlayerEntityFeature.TURN_OFF
|
||||
| MediaPlayerEntityFeature.BROWSE_MEDIA
|
||||
| MediaPlayerEntityFeature.PLAY_MEDIA
|
||||
)
|
||||
|
||||
@property
|
||||
@@ -489,3 +496,151 @@ class RemoteMediaPlayerEntity(CoordinatorEntity[MediaPlayerCoordinator], MediaPl
|
||||
await self.coordinator.client.toggle()
|
||||
except MediaServerError as err:
|
||||
_LOGGER.error("Failed to toggle: %s", err)
|
||||
|
||||
# Media Browser support
|
||||
|
||||
@staticmethod
|
||||
def _encode_media_id(folder_id: str, path: str = "") -> str:
|
||||
"""Encode folder_id and path into media_content_id.
|
||||
|
||||
Format: folder_id|encoded_path
|
||||
Root folder: folder_id|
|
||||
"""
|
||||
return f"{folder_id}|{quote(path, safe='')}"
|
||||
|
||||
@staticmethod
|
||||
def _decode_media_id(media_content_id: str) -> tuple[str, str]:
|
||||
"""Decode media_content_id into folder_id and path.
|
||||
|
||||
Returns:
|
||||
Tuple of (folder_id, path)
|
||||
"""
|
||||
if not media_content_id or "|" not in media_content_id:
|
||||
return "", ""
|
||||
folder_id, encoded_path = media_content_id.split("|", 1)
|
||||
path = unquote(encoded_path) if encoded_path else ""
|
||||
return folder_id, path
|
||||
|
||||
async def async_browse_media(
|
||||
self,
|
||||
media_content_type: str | None = None,
|
||||
media_content_id: str | None = None,
|
||||
) -> BrowseMedia:
|
||||
"""Implement the media browsing.
|
||||
|
||||
Args:
|
||||
media_content_type: Type of media (unused, but required by HA)
|
||||
media_content_id: ID in format "folder_id|path" or None for root
|
||||
|
||||
Returns:
|
||||
BrowseMedia object with children
|
||||
"""
|
||||
_LOGGER.debug("Browse media: type=%s, id=%s", media_content_type, media_content_id)
|
||||
|
||||
# Root level - list all folders
|
||||
if not media_content_id:
|
||||
folders = await self.coordinator.client.get_media_folders()
|
||||
|
||||
children = [
|
||||
BrowseMedia(
|
||||
title=config["label"],
|
||||
media_class=MediaClass.DIRECTORY,
|
||||
media_content_type=MediaType.MUSIC, # All folders show as music
|
||||
media_content_id=self._encode_media_id(folder_id, ""),
|
||||
can_play=False,
|
||||
can_expand=True,
|
||||
)
|
||||
for folder_id, config in folders.items()
|
||||
if config.get("enabled", True)
|
||||
]
|
||||
|
||||
return BrowseMedia(
|
||||
title="Media Folders",
|
||||
media_class=MediaClass.DIRECTORY,
|
||||
media_content_type=MediaType.MUSIC,
|
||||
media_content_id="",
|
||||
can_play=False,
|
||||
can_expand=True,
|
||||
children=children,
|
||||
)
|
||||
|
||||
# Browse specific folder
|
||||
folder_id, path = self._decode_media_id(media_content_id)
|
||||
|
||||
if not folder_id:
|
||||
raise ValueError("Invalid media_content_id format")
|
||||
|
||||
# Get folder contents from API
|
||||
browse_data = await self.coordinator.client.browse_folder(folder_id, path, offset=0, limit=1000)
|
||||
|
||||
children = []
|
||||
for item in browse_data.get("items", []):
|
||||
if item["type"] == "folder":
|
||||
# Subfolder
|
||||
item_path = f"{path}/{item['name']}" if path else item['name']
|
||||
children.append(
|
||||
BrowseMedia(
|
||||
title=item["name"],
|
||||
media_class=MediaClass.DIRECTORY,
|
||||
media_content_type=MediaType.MUSIC,
|
||||
media_content_id=self._encode_media_id(folder_id, item_path),
|
||||
can_play=False,
|
||||
can_expand=True,
|
||||
)
|
||||
)
|
||||
elif item.get("is_media", False):
|
||||
# Media file
|
||||
# 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']
|
||||
# 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)}"
|
||||
|
||||
children.append(
|
||||
BrowseMedia(
|
||||
title=item["name"],
|
||||
media_class=MediaClass.MUSIC,
|
||||
media_content_type=MediaType.MUSIC,
|
||||
media_content_id=absolute_path, # Use absolute path as ID for playback
|
||||
can_play=True,
|
||||
can_expand=False,
|
||||
)
|
||||
)
|
||||
|
||||
# Get current folder label
|
||||
current_title = path.split("/")[-1] if path else browse_data.get("label", folder_id)
|
||||
|
||||
return BrowseMedia(
|
||||
title=current_title,
|
||||
media_class=MediaClass.DIRECTORY,
|
||||
media_content_type=MediaType.MUSIC,
|
||||
media_content_id=media_content_id,
|
||||
can_play=False,
|
||||
can_expand=True,
|
||||
children=children,
|
||||
)
|
||||
|
||||
async def async_play_media(
|
||||
self, media_type: str, media_id: str, **kwargs: Any
|
||||
) -> None:
|
||||
"""Play a media file.
|
||||
|
||||
Args:
|
||||
media_type: Type of media (unused)
|
||||
media_id: Absolute file path to media file
|
||||
**kwargs: Additional arguments (unused)
|
||||
"""
|
||||
_LOGGER.debug("Play media: type=%s, id=%s", media_type, media_id)
|
||||
|
||||
try:
|
||||
# media_id is the absolute file path from browse_media
|
||||
await self.coordinator.client.play_media_file(media_id)
|
||||
|
||||
# Request immediate status update
|
||||
await self.coordinator.async_request_refresh()
|
||||
except MediaServerError as err:
|
||||
_LOGGER.error("Failed to play media file: %s", err)
|
||||
|
||||
Reference in New Issue
Block a user