Improve not shown handling (#67247)

pull/67315/head
Paulus Schoutsen 2022-02-25 11:52:14 -08:00 committed by GitHub
parent 069e70ff03
commit 0a6c8f8e6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 2 deletions

View File

@ -81,11 +81,13 @@ class CameraMediaSource(MediaSource):
# Root. List cameras.
component: EntityComponent = self.hass.data[DOMAIN]
children = []
not_shown = 0
for camera in component.entities:
camera = cast(Camera, camera)
stream_type = camera.frontend_stream_type
if stream_type not in supported_stream_types:
not_shown += 1
continue
children.append(
@ -111,4 +113,5 @@ class CameraMediaSource(MediaSource):
can_expand=True,
children_media_class=MEDIA_CLASS_VIDEO,
children=children,
not_shown=not_shown,
)

View File

@ -119,7 +119,7 @@ async def async_browse_media(
item.children = [
child for child in item.children if child.can_expand or content_filter(child)
]
item.not_shown = old_count - len(item.children)
item.not_shown += old_count - len(item.children)
return item

View File

@ -35,6 +35,7 @@ async def test_browsing_filter_non_hls(hass, mock_camera_web_rtc):
assert item is not None
assert item.title == "Camera"
assert len(item.children) == 0
assert item.not_shown == 2
async def test_resolving(hass, mock_camera_hls):

View File

@ -6,7 +6,7 @@ import yarl
from homeassistant.components import media_source
from homeassistant.components.media_player import MEDIA_CLASS_DIRECTORY, BrowseError
from homeassistant.components.media_source import const
from homeassistant.components.media_source import const, models
from homeassistant.setup import async_setup_component
@ -60,6 +60,30 @@ async def test_async_browse_media(hass):
media.children[0].title = "Epic Sax Guy 10 Hours"
assert media.not_shown == 1
# Test content filter adds to original not_shown
orig_browse = models.MediaSourceItem.async_browse
async def not_shown_browse(self):
"""Patch browsed item to set not_shown base value."""
item = await orig_browse(self)
item.not_shown = 10
return item
with patch(
"homeassistant.components.media_source.models.MediaSourceItem.async_browse",
not_shown_browse,
):
media = await media_source.async_browse_media(
hass,
"",
content_filter=lambda item: item.media_content_type.startswith("video/"),
)
assert isinstance(media, media_source.models.BrowseMediaSource)
assert media.title == "media"
assert len(media.children) == 1, media.children
media.children[0].title = "Epic Sax Guy 10 Hours"
assert media.not_shown == 11
# Test invalid media content
with pytest.raises(BrowseError):
await media_source.async_browse_media(hass, "invalid")