Skip to content

Commit

Permalink
Sort Local Media Source and fix media class (home-assistant#39858)
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterjm authored and balloob committed Sep 9, 2020
1 parent 5ae0844 commit 578c1b2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
11 changes: 11 additions & 0 deletions homeassistant/components/media_source/const.py
Original file line number Diff line number Diff line change
@@ -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>.+)?")
13 changes: 10 additions & 3 deletions homeassistant/components/media_source/local_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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


Expand Down Expand Up @@ -112,11 +112,15 @@ def _build_item_response(self, source_dir_id: str, path: Path, is_child=False):
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,
Expand All @@ -132,6 +136,9 @@ def _build_item_response(self, source_dir_id: str, path: Path, is_child=False):
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


Expand Down

0 comments on commit 578c1b2

Please sign in to comment.