Add media class browse media attribute (#39770)
This commit is contained in:
1 parent
f075823529
commit
586d7eaba6
14 files changed
+248
-18
No files matched your search
@@ -2,13 +2,31 @@
|
||||
import logging
|
||||
|
||||
from homeassistant.components.media_player import BrowseMedia
|
||||
from homeassistant.components.media_player.const import (
|
||||
MEDIA_CLASS_ALBUM,
|
||||
MEDIA_CLASS_ARTIST,
|
||||
MEDIA_CLASS_DIRECTORY,
|
||||
MEDIA_CLASS_EPISODE,
|
||||
MEDIA_CLASS_MOVIE,
|
||||
MEDIA_CLASS_PLAYLIST,
|
||||
MEDIA_CLASS_SEASON,
|
||||
MEDIA_CLASS_TRACK,
|
||||
MEDIA_CLASS_TV_SHOW,
|
||||
MEDIA_CLASS_VIDEO,
|
||||
)
|
||||
from homeassistant.components.media_player.errors import BrowseError
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
|
||||
class UnknownMediaType(BrowseError):
|
||||
"""Unknown media type."""
|
||||
|
||||
|
||||
EXPANDABLES = ["album", "artist", "playlist", "season", "show"]
|
||||
PLAYLISTS_BROWSE_PAYLOAD = {
|
||||
"title": "Playlists",
|
||||
"media_class": MEDIA_CLASS_PLAYLIST,
|
||||
"media_content_id": "all",
|
||||
"media_content_type": "playlists",
|
||||
"can_play": False,
|
||||
@@ -19,6 +37,18 @@ SPECIAL_METHODS = {
|
||||
"Recently Added": "recentlyAdded",
|
||||
}
|
||||
|
||||
ITEM_TYPE_MEDIA_CLASS = {
|
||||
"album": MEDIA_CLASS_ALBUM,
|
||||
"artist": MEDIA_CLASS_ARTIST,
|
||||
"episode": MEDIA_CLASS_EPISODE,
|
||||
"movie": MEDIA_CLASS_MOVIE,
|
||||
"playlist": MEDIA_CLASS_PLAYLIST,
|
||||
"season": MEDIA_CLASS_SEASON,
|
||||
"show": MEDIA_CLASS_TV_SHOW,
|
||||
"track": MEDIA_CLASS_TRACK,
|
||||
"video": MEDIA_CLASS_VIDEO,
|
||||
}
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -34,11 +64,17 @@ def browse_media(
|
||||
if media is None:
|
||||
return None
|
||||
|
||||
media_info = item_payload(media)
|
||||
try:
|
||||
media_info = item_payload(media)
|
||||
except UnknownMediaType:
|
||||
return None
|
||||
if media_info.can_expand:
|
||||
media_info.children = []
|
||||
for item in media:
|
||||
media_info.children.append(item_payload(item))
|
||||
try:
|
||||
media_info.children.append(item_payload(item))
|
||||
except UnknownMediaType:
|
||||
continue
|
||||
return media_info
|
||||
|
||||
if media_content_id and ":" in media_content_id:
|
||||
@@ -65,6 +101,7 @@ def browse_media(
|
||||
|
||||
payload = {
|
||||
"title": title,
|
||||
"media_class": MEDIA_CLASS_DIRECTORY,
|
||||
"media_content_id": f"{media_content_id}:{special_folder}",
|
||||
"media_content_type": media_content_type,
|
||||
"can_play": False,
|
||||
@@ -75,7 +112,10 @@ def browse_media(
|
||||
method = SPECIAL_METHODS[special_folder]
|
||||
items = getattr(library_or_section, method)()
|
||||
for item in items:
|
||||
payload["children"].append(item_payload(item))
|
||||
try:
|
||||
payload["children"].append(item_payload(item))
|
||||
except UnknownMediaType:
|
||||
continue
|
||||
return BrowseMedia(**payload)
|
||||
|
||||
if media_content_type in ["server", None]:
|
||||
@@ -99,8 +139,14 @@ def browse_media(
|
||||
|
||||
def item_payload(item):
|
||||
"""Create response payload for a single media item."""
|
||||
try:
|
||||
media_class = ITEM_TYPE_MEDIA_CLASS[item.type]
|
||||
except KeyError as err:
|
||||
_LOGGER.debug("Unknown type received: %s", item.type)
|
||||
raise UnknownMediaType from err
|
||||
payload = {
|
||||
"title": item.title,
|
||||
"media_class": media_class,
|
||||
"media_content_id": str(item.ratingKey),
|
||||
"media_content_type": item.type,
|
||||
"can_play": True,
|
||||
@@ -116,6 +162,7 @@ def library_section_payload(section):
|
||||
"""Create response payload for a single library section."""
|
||||
return BrowseMedia(
|
||||
title=section.title,
|
||||
media_class=MEDIA_CLASS_DIRECTORY,
|
||||
media_content_id=section.key,
|
||||
media_content_type="library",
|
||||
can_play=False,
|
||||
@@ -128,6 +175,7 @@ def special_library_payload(parent_payload, special_type):
|
||||
title = f"{special_type} ({parent_payload.title})"
|
||||
return BrowseMedia(
|
||||
title=title,
|
||||
media_class=parent_payload.media_class,
|
||||
media_content_id=f"{parent_payload.media_content_id}:{special_type}",
|
||||
media_content_type=parent_payload.media_content_type,
|
||||
can_play=False,
|
||||
@@ -139,6 +187,7 @@ def server_payload(plex_server):
|
||||
"""Create response payload to describe libraries of the Plex server."""
|
||||
server_info = BrowseMedia(
|
||||
title=plex_server.friendly_name,
|
||||
media_class=MEDIA_CLASS_DIRECTORY,
|
||||
media_content_id=plex_server.machine_identifier,
|
||||
media_content_type="server",
|
||||
can_play=False,
|
||||
@@ -165,7 +214,10 @@ def library_payload(plex_server, library_id):
|
||||
special_library_payload(library_info, "Recently Added")
|
||||
)
|
||||
for item in library.all():
|
||||
library_info.children.append(item_payload(item))
|
||||
try:
|
||||
library_info.children.append(item_payload(item))
|
||||
except UnknownMediaType:
|
||||
continue
|
||||
return library_info
|
||||
|
||||
|
||||
@@ -173,5 +225,8 @@ def playlists_payload(plex_server):
|
||||
"""Create response payload for all available playlists."""
|
||||
playlists_info = {**PLAYLISTS_BROWSE_PAYLOAD, "children": []}
|
||||
for playlist in plex_server.playlists():
|
||||
playlists_info["children"].append(item_payload(playlist))
|
||||
try:
|
||||
playlists_info["children"].append(item_payload(playlist))
|
||||
except UnknownMediaType:
|
||||
continue
|
||||
return BrowseMedia(**playlists_info)
|
||||
Reference in new issue
Block a user