Restore children media class (#67409)

This commit is contained in:
Paulus Schoutsen 2022-03-01 15:14:14 -08:00 committed by GitHub
parent 57705bc13d
commit d68ada74cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 11 deletions

View File

@ -580,6 +580,7 @@ class DmsDeviceSource:
children=children, children=children,
) )
if media_source.children:
media_source.calculate_children_class() media_source.calculate_children_class()
return media_source return media_source
@ -650,6 +651,7 @@ class DmsDeviceSource:
thumbnail=self._didl_thumbnail_url(item), thumbnail=self._didl_thumbnail_url(item),
) )
if media_source.children:
media_source.calculate_children_class() media_source.calculate_children_class()
return media_source return media_source

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import Any
from urllib.parse import quote from urllib.parse import quote
import yarl import yarl
@ -74,11 +75,15 @@ class BrowseMedia:
def as_dict(self, *, parent: bool = True) -> dict: def as_dict(self, *, parent: bool = True) -> dict:
"""Convert Media class to browse media dictionary.""" """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, "title": self.title,
"media_class": self.media_class, "media_class": self.media_class,
"media_content_type": self.media_content_type, "media_content_type": self.media_content_type,
"media_content_id": self.media_content_id, "media_content_id": self.media_content_id,
"children_media_class": self.children_media_class,
"can_play": self.can_play, "can_play": self.can_play,
"can_expand": self.can_expand, "can_expand": self.can_expand,
"thumbnail": self.thumbnail, "thumbnail": self.thumbnail,
@ -87,11 +92,7 @@ class BrowseMedia:
if not parent: if not parent:
return response return response
if self.children_media_class is None:
self.calculate_children_class()
response["not_shown"] = self.not_shown response["not_shown"] = self.not_shown
response["children_media_class"] = self.children_media_class
if self.children: if self.children:
response["children"] = [ response["children"] = [
@ -104,11 +105,8 @@ class BrowseMedia:
def calculate_children_class(self) -> None: def calculate_children_class(self) -> None:
"""Count the children media classes and calculate the correct class.""" """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 self.children_media_class = MEDIA_CLASS_DIRECTORY
assert self.children is not None
proposed_class = self.children[0].media_class proposed_class = self.children[0].media_class
if all(child.media_class == proposed_class for child in self.children): if all(child.media_class == proposed_class for child in self.children):
self.children_media_class = proposed_class self.children_media_class = proposed_class

View File

@ -872,6 +872,7 @@ async def test_entity_browse_media(hass: HomeAssistant, hass_ws_client):
"can_play": True, "can_play": True,
"can_expand": False, "can_expand": False,
"thumbnail": None, "thumbnail": None,
"children_media_class": None,
} }
assert expected_child_1 in response["result"]["children"] 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_play": True,
"can_expand": False, "can_expand": False,
"thumbnail": None, "thumbnail": None,
"children_media_class": None,
} }
assert expected_child_2 in response["result"]["children"] assert expected_child_2 in response["result"]["children"]
@ -926,6 +928,7 @@ async def test_entity_browse_media_audio_only(
"can_play": True, "can_play": True,
"can_expand": False, "can_expand": False,
"thumbnail": None, "thumbnail": None,
"children_media_class": None,
} }
assert expected_child_1 not in response["result"]["children"] 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_play": True,
"can_expand": False, "can_expand": False,
"thumbnail": None, "thumbnail": None,
"children_media_class": None,
} }
assert expected_child_2 in response["result"]["children"] 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_play": False,
"can_expand": True, "can_expand": True,
"thumbnail": "https://brands.home-assistant.io/_/spotify/logo.png", "thumbnail": "https://brands.home-assistant.io/_/spotify/logo.png",
"children_media_class": None,
} }
assert expected_child in response["result"]["children"] assert expected_child in response["result"]["children"]

View File

@ -961,6 +961,7 @@ async def test_browse_media(
"can_play": True, "can_play": True,
"can_expand": False, "can_expand": False,
"thumbnail": None, "thumbnail": None,
"children_media_class": None,
} }
assert expected_child_video in response["result"]["children"] assert expected_child_video in response["result"]["children"]
@ -972,6 +973,7 @@ async def test_browse_media(
"can_play": True, "can_play": True,
"can_expand": False, "can_expand": False,
"thumbnail": None, "thumbnail": None,
"children_media_class": None,
} }
assert expected_child_audio in response["result"]["children"] assert expected_child_audio in response["result"]["children"]

View File

@ -111,6 +111,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
"can_play": False, "can_play": False,
"can_expand": True, "can_expand": True,
"thumbnail": None, "thumbnail": None,
"children_media_class": "directory",
} }
], ],
"not_shown": 0, "not_shown": 0,
@ -143,6 +144,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
"can_play": False, "can_play": False,
"can_expand": True, "can_expand": True,
"thumbnail": None, "thumbnail": None,
"children_media_class": "directory",
} }
], ],
"not_shown": 0, "not_shown": 0,
@ -174,6 +176,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
"can_play": False, "can_play": False,
"can_expand": True, "can_expand": True,
"thumbnail": None, "thumbnail": None,
"children_media_class": "video",
}, },
{ {
"title": "Images", "title": "Images",
@ -186,6 +189,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
"can_play": False, "can_play": False,
"can_expand": True, "can_expand": True,
"thumbnail": None, "thumbnail": None,
"children_media_class": "image",
}, },
], ],
"not_shown": 0, "not_shown": 0,
@ -220,6 +224,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
"can_play": False, "can_play": False,
"can_expand": True, "can_expand": True,
"thumbnail": None, "thumbnail": None,
"children_media_class": "directory",
} }
], ],
"not_shown": 0, "not_shown": 0,
@ -255,6 +260,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
"can_play": True, "can_play": True,
"can_expand": False, "can_expand": False,
"thumbnail": "http://movie", "thumbnail": "http://movie",
"children_media_class": None,
}, },
{ {
"title": "00-36-49.mp4", "title": "00-36-49.mp4",
@ -268,6 +274,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
"can_play": True, "can_play": True,
"can_expand": False, "can_expand": False,
"thumbnail": "http://movie", "thumbnail": "http://movie",
"children_media_class": None,
}, },
{ {
"title": "00-02-27.mp4", "title": "00-02-27.mp4",
@ -281,6 +288,7 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
"can_play": True, "can_play": True,
"can_expand": False, "can_expand": False,
"thumbnail": "http://movie", "thumbnail": "http://movie",
"children_media_class": None,
}, },
], ],
"not_shown": 0, "not_shown": 0,
@ -331,6 +339,7 @@ async def test_async_browse_media_images_success(hass: HomeAssistant) -> None:
"can_play": False, "can_play": False,
"can_expand": False, "can_expand": False,
"thumbnail": "http://image", "thumbnail": "http://image",
"children_media_class": None,
} }
], ],
"not_shown": 0, "not_shown": 0,