Improve not shown handling (#67247)
This commit is contained in:
parent
069e70ff03
commit
0a6c8f8e6c
4 changed files with 30 additions and 2 deletions
|
@ -81,11 +81,13 @@ class CameraMediaSource(MediaSource):
|
||||||
# Root. List cameras.
|
# Root. List cameras.
|
||||||
component: EntityComponent = self.hass.data[DOMAIN]
|
component: EntityComponent = self.hass.data[DOMAIN]
|
||||||
children = []
|
children = []
|
||||||
|
not_shown = 0
|
||||||
for camera in component.entities:
|
for camera in component.entities:
|
||||||
camera = cast(Camera, camera)
|
camera = cast(Camera, camera)
|
||||||
stream_type = camera.frontend_stream_type
|
stream_type = camera.frontend_stream_type
|
||||||
|
|
||||||
if stream_type not in supported_stream_types:
|
if stream_type not in supported_stream_types:
|
||||||
|
not_shown += 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
children.append(
|
children.append(
|
||||||
|
@ -111,4 +113,5 @@ class CameraMediaSource(MediaSource):
|
||||||
can_expand=True,
|
can_expand=True,
|
||||||
children_media_class=MEDIA_CLASS_VIDEO,
|
children_media_class=MEDIA_CLASS_VIDEO,
|
||||||
children=children,
|
children=children,
|
||||||
|
not_shown=not_shown,
|
||||||
)
|
)
|
||||||
|
|
|
@ -119,7 +119,7 @@ async def async_browse_media(
|
||||||
item.children = [
|
item.children = [
|
||||||
child for child in item.children if child.can_expand or content_filter(child)
|
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
|
return item
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,7 @@ async def test_browsing_filter_non_hls(hass, mock_camera_web_rtc):
|
||||||
assert item is not None
|
assert item is not None
|
||||||
assert item.title == "Camera"
|
assert item.title == "Camera"
|
||||||
assert len(item.children) == 0
|
assert len(item.children) == 0
|
||||||
|
assert item.not_shown == 2
|
||||||
|
|
||||||
|
|
||||||
async def test_resolving(hass, mock_camera_hls):
|
async def test_resolving(hass, mock_camera_hls):
|
||||||
|
|
|
@ -6,7 +6,7 @@ import yarl
|
||||||
|
|
||||||
from homeassistant.components import media_source
|
from homeassistant.components import media_source
|
||||||
from homeassistant.components.media_player import MEDIA_CLASS_DIRECTORY, BrowseError
|
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
|
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"
|
media.children[0].title = "Epic Sax Guy 10 Hours"
|
||||||
assert media.not_shown == 1
|
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
|
# Test invalid media content
|
||||||
with pytest.raises(BrowseError):
|
with pytest.raises(BrowseError):
|
||||||
await media_source.async_browse_media(hass, "invalid")
|
await media_source.async_browse_media(hass, "invalid")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue