From 4f342eae2717e164fb11038661f63b71473a82f9 Mon Sep 17 00:00:00 2001 From: cgtobi Date: Wed, 9 Sep 2020 14:12:11 +0200 Subject: [PATCH] Fix Kodi media browser (#39840) * Refactor * Make linter happy * Only return at the end * Handle exception --- homeassistant/components/kodi/browse_media.py | 112 +++++++++++------- 1 file changed, 67 insertions(+), 45 deletions(-) diff --git a/homeassistant/components/kodi/browse_media.py b/homeassistant/components/kodi/browse_media.py index cae982078db..3fc3b40cd38 100644 --- a/homeassistant/components/kodi/browse_media.py +++ b/homeassistant/components/kodi/browse_media.py @@ -1,14 +1,17 @@ """Support for media browsing.""" +import logging -from homeassistant.components.media_player import BrowseMedia +from homeassistant.components.media_player import BrowseError, 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_MUSIC, MEDIA_CLASS_PLAYLIST, MEDIA_CLASS_SEASON, + MEDIA_CLASS_TRACK, MEDIA_CLASS_TV_SHOW, MEDIA_TYPE_ALBUM, MEDIA_TYPE_ARTIST, @@ -26,26 +29,24 @@ PLAYABLE_MEDIA_TYPES = [ MEDIA_TYPE_TRACK, ] -EXPANDABLE_MEDIA_TYPES = [ - MEDIA_TYPE_ALBUM, - MEDIA_TYPE_ARTIST, - MEDIA_TYPE_PLAYLIST, - MEDIA_TYPE_TVSHOW, - MEDIA_TYPE_SEASON, - "library_music", -] - CONTENT_TYPE_MEDIA_CLASS = { - "library_music": MEDIA_CLASS_DIRECTORY, + "library_music": MEDIA_CLASS_MUSIC, MEDIA_TYPE_SEASON: MEDIA_CLASS_SEASON, MEDIA_TYPE_ALBUM: MEDIA_CLASS_ALBUM, MEDIA_TYPE_ARTIST: MEDIA_CLASS_ARTIST, MEDIA_TYPE_MOVIE: MEDIA_CLASS_MOVIE, MEDIA_TYPE_PLAYLIST: MEDIA_CLASS_PLAYLIST, + MEDIA_TYPE_TRACK: MEDIA_CLASS_TRACK, MEDIA_TYPE_TVSHOW: MEDIA_CLASS_TV_SHOW, MEDIA_TYPE_EPISODE: MEDIA_CLASS_EPISODE, } +_LOGGER = logging.getLogger(__name__) + + +class UnknownMediaType(BrowseError): + """Unknown media type.""" + async def build_item_response(media_library, payload): """Create response payload for the provided media query.""" @@ -141,16 +142,23 @@ async def build_item_response(media_library, payload): title = season["seasondetails"]["label"] if media is None: - return + return None + + children = [] + for item in media: + try: + children.append(item_payload(item, media_library)) + except UnknownMediaType: + pass return BrowseMedia( media_class=CONTENT_TYPE_MEDIA_CLASS[search_type], - media_content_id=payload["search_id"], + media_content_id=search_id, media_content_type=search_type, title=title, can_play=search_type in PLAYABLE_MEDIA_TYPES and search_id, can_expand=True, - children=[item_payload(item, media_library) for item in media], + children=children, thumbnail=thumbnail, ) @@ -161,46 +169,60 @@ def item_payload(item, media_library): Used by async_browse_media. """ - if "songid" in item: - media_content_type = MEDIA_TYPE_TRACK - media_content_id = f"{item['songid']}" - elif "albumid" in item: - media_content_type = MEDIA_TYPE_ALBUM - media_content_id = f"{item['albumid']}" - elif "artistid" in item: - media_content_type = MEDIA_TYPE_ARTIST - media_content_id = f"{item['artistid']}" - elif "movieid" in item: - media_content_type = MEDIA_TYPE_MOVIE - media_content_id = f"{item['movieid']}" - elif "episodeid" in item: - media_content_type = MEDIA_TYPE_EPISODE - media_content_id = f"{item['episodeid']}" - elif "seasonid" in item: - media_content_type = MEDIA_TYPE_SEASON - media_content_id = f"{item['tvshowid']}/{item['season']}" - elif "tvshowid" in item: - media_content_type = MEDIA_TYPE_TVSHOW - media_content_id = f"{item['tvshowid']}" - else: - # this case is for the top folder of each type - # possible content types: album, artist, movie, library_music, tvshow - media_content_type = item.get("type") - media_content_id = "" - title = item["label"] - can_play = media_content_type in PLAYABLE_MEDIA_TYPES and media_content_id - can_expand = media_content_type in EXPANDABLE_MEDIA_TYPES thumbnail = item.get("thumbnail") if thumbnail: thumbnail = media_library.thumbnail_url(thumbnail) - if media_content_type == MEDIA_TYPE_MOVIE and not media_content_id: - media_class = MEDIA_CLASS_DIRECTORY + if "songid" in item: + media_content_type = MEDIA_TYPE_TRACK + media_content_id = f"{item['songid']}" + can_play = True + can_expand = False + elif "albumid" in item: + media_content_type = MEDIA_TYPE_ALBUM + media_content_id = f"{item['albumid']}" + can_play = True + can_expand = True + elif "artistid" in item: + media_content_type = MEDIA_TYPE_ARTIST + media_content_id = f"{item['artistid']}" + can_play = True + can_expand = True + elif "movieid" in item: + media_content_type = MEDIA_TYPE_MOVIE + media_content_id = f"{item['movieid']}" + can_play = True + can_expand = False + elif "episodeid" in item: + media_content_type = MEDIA_TYPE_EPISODE + media_content_id = f"{item['episodeid']}" + can_play = True + can_expand = False + elif "seasonid" in item: + media_content_type = MEDIA_TYPE_SEASON + media_content_id = f"{item['tvshowid']}/{item['season']}" + can_play = False + can_expand = True + elif "tvshowid" in item: + media_content_type = MEDIA_TYPE_TVSHOW + media_content_id = f"{item['tvshowid']}" + can_play = False can_expand = True else: + # this case is for the top folder of each type + # possible content types: album, artist, movie, library_music, tvshow + media_content_type = item["type"] + media_content_id = "" + can_play = False + can_expand = True + + try: media_class = CONTENT_TYPE_MEDIA_CLASS[media_content_type] + except KeyError as err: + _LOGGER.debug("Unknown media type received: %s", media_content_type) + raise UnknownMediaType from err return BrowseMedia( title=title,