Sort Local Media Source and fix media class (#39858)

pull/39754/head
Jason Hunter 2020-09-09 16:19:30 -04:00 committed by GitHub
parent b7ad83c655
commit 9358b5089e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -1,7 +1,18 @@
"""Constants for the media_source integration."""
import re
from homeassistant.components.media_player.const import (
MEDIA_CLASS_IMAGE,
MEDIA_CLASS_MUSIC,
MEDIA_CLASS_VIDEO,
)
DOMAIN = "media_source"
MEDIA_MIME_TYPES = ("audio", "video", "image")
MEDIA_CLASS_MAP = {
"audio": MEDIA_CLASS_MUSIC,
"video": MEDIA_CLASS_VIDEO,
"image": MEDIA_CLASS_IMAGE,
}
URI_SCHEME = "media-source://"
URI_SCHEME_REGEX = re.compile(r"^media-source://(?P<domain>[^/]+)?(?P<identifier>.+)?")

View File

@ -12,7 +12,7 @@ from homeassistant.components.media_source.error import Unresolvable
from homeassistant.core import HomeAssistant, callback
from homeassistant.util import sanitize_path
from .const import DOMAIN, MEDIA_MIME_TYPES
from .const import DOMAIN, MEDIA_CLASS_MAP, MEDIA_MIME_TYPES
from .models import BrowseMediaSource, MediaSource, MediaSourceItem, PlayMedia
@ -112,11 +112,15 @@ class LocalSource(MediaSource):
if is_dir:
title += "/"
media_class = MEDIA_CLASS_MAP.get(
mime_type and mime_type.split("/")[0], MEDIA_CLASS_DIRECTORY
)
media = BrowseMediaSource(
domain=DOMAIN,
identifier=f"{source_dir_id}/{path.relative_to(self.hass.config.path('media'))}",
media_class=MEDIA_CLASS_DIRECTORY,
media_content_type="directory",
media_class=media_class,
media_content_type=mime_type or "",
title=title,
can_play=is_file,
can_expand=is_dir,
@ -132,6 +136,9 @@ class LocalSource(MediaSource):
if child:
media.children.append(child)
# Sort children showing directories first, then by name
media.children.sort(key=lambda child: (child.can_play, child.title))
return media