diff --git a/homeassistant/components/camera/media_source.py b/homeassistant/components/camera/media_source.py index c61cbef146a..ab7661fefe2 100644 --- a/homeassistant/components/camera/media_source.py +++ b/homeassistant/components/camera/media_source.py @@ -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, ) diff --git a/homeassistant/components/media_source/__init__.py b/homeassistant/components/media_source/__init__.py index 2bcd80a39ab..3c42016f8f7 100644 --- a/homeassistant/components/media_source/__init__.py +++ b/homeassistant/components/media_source/__init__.py @@ -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 diff --git a/tests/components/camera/test_media_source.py b/tests/components/camera/test_media_source.py index 3a3558419e5..54d6ef6279e 100644 --- a/tests/components/camera/test_media_source.py +++ b/tests/components/camera/test_media_source.py @@ -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): diff --git a/tests/components/media_source/test_init.py b/tests/components/media_source/test_init.py index 2655000efc9..491b1972cb6 100644 --- a/tests/components/media_source/test_init.py +++ b/tests/components/media_source/test_init.py @@ -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")