From d68ada74ccebaa0c1b6986b3be9cf4d73eca7cae Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 1 Mar 2022 15:14:14 -0800 Subject: [PATCH] Restore children media class (#67409) --- homeassistant/components/dlna_dms/dms.py | 6 ++++-- .../components/media_player/browse_media.py | 16 +++++++--------- tests/components/cast/test_media_player.py | 5 +++++ tests/components/dlna_dmr/test_media_player.py | 2 ++ tests/components/motioneye/test_media_source.py | 9 +++++++++ 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/dlna_dms/dms.py b/homeassistant/components/dlna_dms/dms.py index 74774a84821..94ac67b0f1a 100644 --- a/homeassistant/components/dlna_dms/dms.py +++ b/homeassistant/components/dlna_dms/dms.py @@ -580,7 +580,8 @@ class DmsDeviceSource: children=children, ) - media_source.calculate_children_class() + if media_source.children: + media_source.calculate_children_class() return media_source @@ -650,7 +651,8 @@ class DmsDeviceSource: thumbnail=self._didl_thumbnail_url(item), ) - media_source.calculate_children_class() + if media_source.children: + media_source.calculate_children_class() return media_source diff --git a/homeassistant/components/media_player/browse_media.py b/homeassistant/components/media_player/browse_media.py index 26494e4c8a7..fa825042817 100644 --- a/homeassistant/components/media_player/browse_media.py +++ b/homeassistant/components/media_player/browse_media.py @@ -3,6 +3,7 @@ from __future__ import annotations from datetime import timedelta import logging +from typing import Any from urllib.parse import quote import yarl @@ -74,11 +75,15 @@ class BrowseMedia: def as_dict(self, *, parent: bool = True) -> dict: """Convert Media class to browse media dictionary.""" - response = { + if self.children_media_class is None and self.children: + self.calculate_children_class() + + response: dict[str, Any] = { "title": self.title, "media_class": self.media_class, "media_content_type": self.media_content_type, "media_content_id": self.media_content_id, + "children_media_class": self.children_media_class, "can_play": self.can_play, "can_expand": self.can_expand, "thumbnail": self.thumbnail, @@ -87,11 +92,7 @@ class BrowseMedia: if not parent: return response - if self.children_media_class is None: - self.calculate_children_class() - response["not_shown"] = self.not_shown - response["children_media_class"] = self.children_media_class if self.children: response["children"] = [ @@ -104,11 +105,8 @@ class BrowseMedia: def calculate_children_class(self) -> None: """Count the children media classes and calculate the correct class.""" - if self.children is None or len(self.children) == 0: - return - self.children_media_class = MEDIA_CLASS_DIRECTORY - + assert self.children is not None proposed_class = self.children[0].media_class if all(child.media_class == proposed_class for child in self.children): self.children_media_class = proposed_class diff --git a/tests/components/cast/test_media_player.py b/tests/components/cast/test_media_player.py index fa70ed17eb9..4cf96f1a965 100644 --- a/tests/components/cast/test_media_player.py +++ b/tests/components/cast/test_media_player.py @@ -872,6 +872,7 @@ async def test_entity_browse_media(hass: HomeAssistant, hass_ws_client): "can_play": True, "can_expand": False, "thumbnail": None, + "children_media_class": None, } assert expected_child_1 in response["result"]["children"] @@ -883,6 +884,7 @@ async def test_entity_browse_media(hass: HomeAssistant, hass_ws_client): "can_play": True, "can_expand": False, "thumbnail": None, + "children_media_class": None, } assert expected_child_2 in response["result"]["children"] @@ -926,6 +928,7 @@ async def test_entity_browse_media_audio_only( "can_play": True, "can_expand": False, "thumbnail": None, + "children_media_class": None, } assert expected_child_1 not in response["result"]["children"] @@ -937,6 +940,7 @@ async def test_entity_browse_media_audio_only( "can_play": True, "can_expand": False, "thumbnail": None, + "children_media_class": None, } assert expected_child_2 in response["result"]["children"] @@ -1873,6 +1877,7 @@ async def test_cast_platform_browse_media(hass: HomeAssistant, hass_ws_client): "can_play": False, "can_expand": True, "thumbnail": "https://brands.home-assistant.io/_/spotify/logo.png", + "children_media_class": None, } assert expected_child in response["result"]["children"] diff --git a/tests/components/dlna_dmr/test_media_player.py b/tests/components/dlna_dmr/test_media_player.py index 896968557c1..a9ac5946f30 100644 --- a/tests/components/dlna_dmr/test_media_player.py +++ b/tests/components/dlna_dmr/test_media_player.py @@ -961,6 +961,7 @@ async def test_browse_media( "can_play": True, "can_expand": False, "thumbnail": None, + "children_media_class": None, } assert expected_child_video in response["result"]["children"] @@ -972,6 +973,7 @@ async def test_browse_media( "can_play": True, "can_expand": False, "thumbnail": None, + "children_media_class": None, } assert expected_child_audio in response["result"]["children"] diff --git a/tests/components/motioneye/test_media_source.py b/tests/components/motioneye/test_media_source.py index 6c0e46b34c6..6979d5c645d 100644 --- a/tests/components/motioneye/test_media_source.py +++ b/tests/components/motioneye/test_media_source.py @@ -111,6 +111,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None: "can_play": False, "can_expand": True, "thumbnail": None, + "children_media_class": "directory", } ], "not_shown": 0, @@ -143,6 +144,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None: "can_play": False, "can_expand": True, "thumbnail": None, + "children_media_class": "directory", } ], "not_shown": 0, @@ -174,6 +176,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None: "can_play": False, "can_expand": True, "thumbnail": None, + "children_media_class": "video", }, { "title": "Images", @@ -186,6 +189,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None: "can_play": False, "can_expand": True, "thumbnail": None, + "children_media_class": "image", }, ], "not_shown": 0, @@ -220,6 +224,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None: "can_play": False, "can_expand": True, "thumbnail": None, + "children_media_class": "directory", } ], "not_shown": 0, @@ -255,6 +260,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None: "can_play": True, "can_expand": False, "thumbnail": "http://movie", + "children_media_class": None, }, { "title": "00-36-49.mp4", @@ -268,6 +274,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None: "can_play": True, "can_expand": False, "thumbnail": "http://movie", + "children_media_class": None, }, { "title": "00-02-27.mp4", @@ -281,6 +288,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None: "can_play": True, "can_expand": False, "thumbnail": "http://movie", + "children_media_class": None, }, ], "not_shown": 0, @@ -331,6 +339,7 @@ async def test_async_browse_media_images_success(hass: HomeAssistant) -> None: "can_play": False, "can_expand": False, "thumbnail": "http://image", + "children_media_class": None, } ], "not_shown": 0,