From 9358b5089e8e3a8fd664487cf449557b9b6c9757 Mon Sep 17 00:00:00 2001 From: Jason Hunter Date: Wed, 9 Sep 2020 16:19:30 -0400 Subject: [PATCH] Sort Local Media Source and fix media class (#39858) --- homeassistant/components/media_source/const.py | 11 +++++++++++ .../components/media_source/local_source.py | 13 ++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/media_source/const.py b/homeassistant/components/media_source/const.py index d50a8b1c404..68a8244c3ce 100644 --- a/homeassistant/components/media_source/const.py +++ b/homeassistant/components/media_source/const.py @@ -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[^/]+)?(?P.+)?") diff --git a/homeassistant/components/media_source/local_source.py b/homeassistant/components/media_source/local_source.py index 774ee64d852..a558de775f8 100644 --- a/homeassistant/components/media_source/local_source.py +++ b/homeassistant/components/media_source/local_source.py @@ -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