mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Track hidden items in media source (#67096)
This commit is contained in:
parent
c879bf295b
commit
459e6c273b
@ -56,6 +56,7 @@ class BrowseMedia:
|
|||||||
children: list[BrowseMedia] | None = None,
|
children: list[BrowseMedia] | None = None,
|
||||||
children_media_class: str | None = None,
|
children_media_class: str | None = None,
|
||||||
thumbnail: str | None = None,
|
thumbnail: str | None = None,
|
||||||
|
not_shown: int = 0,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize browse media item."""
|
"""Initialize browse media item."""
|
||||||
self.media_class = media_class
|
self.media_class = media_class
|
||||||
@ -67,12 +68,10 @@ class BrowseMedia:
|
|||||||
self.children = children
|
self.children = children
|
||||||
self.children_media_class = children_media_class
|
self.children_media_class = children_media_class
|
||||||
self.thumbnail = thumbnail
|
self.thumbnail = thumbnail
|
||||||
|
self.not_shown = not_shown
|
||||||
|
|
||||||
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."""
|
||||||
if self.children_media_class is None:
|
|
||||||
self.calculate_children_class()
|
|
||||||
|
|
||||||
response = {
|
response = {
|
||||||
"title": self.title,
|
"title": self.title,
|
||||||
"media_class": self.media_class,
|
"media_class": self.media_class,
|
||||||
@ -80,13 +79,18 @@ class BrowseMedia:
|
|||||||
"media_content_id": self.media_content_id,
|
"media_content_id": self.media_content_id,
|
||||||
"can_play": self.can_play,
|
"can_play": self.can_play,
|
||||||
"can_expand": self.can_expand,
|
"can_expand": self.can_expand,
|
||||||
"children_media_class": self.children_media_class,
|
|
||||||
"thumbnail": self.thumbnail,
|
"thumbnail": self.thumbnail,
|
||||||
}
|
}
|
||||||
|
|
||||||
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["children_media_class"] = self.children_media_class
|
||||||
|
|
||||||
if self.children:
|
if self.children:
|
||||||
response["children"] = [
|
response["children"] = [
|
||||||
child.as_dict(parent=False) for child in self.children
|
child.as_dict(parent=False) for child in self.children
|
||||||
|
@ -111,9 +111,11 @@ async def async_browse_media(
|
|||||||
if content_filter is None or item.children is None:
|
if content_filter is None or item.children is None:
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
old_count = len(item.children)
|
||||||
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)
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
|
||||||
|
@ -856,7 +856,6 @@ async def test_entity_browse_media(hass: HomeAssistant, hass_ws_client):
|
|||||||
"media_content_id": "media-source://media_source/local/Epic Sax Guy 10 Hours.mp4",
|
"media_content_id": "media-source://media_source/local/Epic Sax Guy 10 Hours.mp4",
|
||||||
"can_play": True,
|
"can_play": True,
|
||||||
"can_expand": False,
|
"can_expand": False,
|
||||||
"children_media_class": None,
|
|
||||||
"thumbnail": None,
|
"thumbnail": None,
|
||||||
}
|
}
|
||||||
assert expected_child_1 in response["result"]["children"]
|
assert expected_child_1 in response["result"]["children"]
|
||||||
@ -868,7 +867,6 @@ async def test_entity_browse_media(hass: HomeAssistant, hass_ws_client):
|
|||||||
"media_content_id": "media-source://media_source/local/test.mp3",
|
"media_content_id": "media-source://media_source/local/test.mp3",
|
||||||
"can_play": True,
|
"can_play": True,
|
||||||
"can_expand": False,
|
"can_expand": False,
|
||||||
"children_media_class": None,
|
|
||||||
"thumbnail": None,
|
"thumbnail": None,
|
||||||
}
|
}
|
||||||
assert expected_child_2 in response["result"]["children"]
|
assert expected_child_2 in response["result"]["children"]
|
||||||
@ -912,7 +910,6 @@ async def test_entity_browse_media_audio_only(
|
|||||||
"media_content_id": "media-source://media_source/local/Epic Sax Guy 10 Hours.mp4",
|
"media_content_id": "media-source://media_source/local/Epic Sax Guy 10 Hours.mp4",
|
||||||
"can_play": True,
|
"can_play": True,
|
||||||
"can_expand": False,
|
"can_expand": False,
|
||||||
"children_media_class": None,
|
|
||||||
"thumbnail": None,
|
"thumbnail": None,
|
||||||
}
|
}
|
||||||
assert expected_child_1 not in response["result"]["children"]
|
assert expected_child_1 not in response["result"]["children"]
|
||||||
@ -924,7 +921,6 @@ async def test_entity_browse_media_audio_only(
|
|||||||
"media_content_id": "media-source://media_source/local/test.mp3",
|
"media_content_id": "media-source://media_source/local/test.mp3",
|
||||||
"can_play": True,
|
"can_play": True,
|
||||||
"can_expand": False,
|
"can_expand": False,
|
||||||
"children_media_class": None,
|
|
||||||
"thumbnail": None,
|
"thumbnail": None,
|
||||||
}
|
}
|
||||||
assert expected_child_2 in response["result"]["children"]
|
assert expected_child_2 in response["result"]["children"]
|
||||||
@ -1861,7 +1857,6 @@ async def test_cast_platform_browse_media(hass: HomeAssistant, hass_ws_client):
|
|||||||
"media_content_id": "",
|
"media_content_id": "",
|
||||||
"can_play": False,
|
"can_play": False,
|
||||||
"can_expand": True,
|
"can_expand": True,
|
||||||
"children_media_class": None,
|
|
||||||
"thumbnail": "https://brands.home-assistant.io/_/spotify/logo.png",
|
"thumbnail": "https://brands.home-assistant.io/_/spotify/logo.png",
|
||||||
}
|
}
|
||||||
assert expected_child in response["result"]["children"]
|
assert expected_child in response["result"]["children"]
|
||||||
@ -1888,6 +1883,7 @@ async def test_cast_platform_browse_media(hass: HomeAssistant, hass_ws_client):
|
|||||||
"children_media_class": None,
|
"children_media_class": None,
|
||||||
"thumbnail": None,
|
"thumbnail": None,
|
||||||
"children": [],
|
"children": [],
|
||||||
|
"not_shown": 0,
|
||||||
}
|
}
|
||||||
assert response["result"] == expected_response
|
assert response["result"] == expected_response
|
||||||
|
|
||||||
|
@ -960,7 +960,6 @@ async def test_browse_media(
|
|||||||
"media_content_id": "media-source://media_source/local/Epic Sax Guy 10 Hours.mp4",
|
"media_content_id": "media-source://media_source/local/Epic Sax Guy 10 Hours.mp4",
|
||||||
"can_play": True,
|
"can_play": True,
|
||||||
"can_expand": False,
|
"can_expand": False,
|
||||||
"children_media_class": None,
|
|
||||||
"thumbnail": None,
|
"thumbnail": None,
|
||||||
}
|
}
|
||||||
assert expected_child_video in response["result"]["children"]
|
assert expected_child_video in response["result"]["children"]
|
||||||
@ -972,7 +971,6 @@ async def test_browse_media(
|
|||||||
"media_content_id": "media-source://media_source/local/test.mp3",
|
"media_content_id": "media-source://media_source/local/test.mp3",
|
||||||
"can_play": True,
|
"can_play": True,
|
||||||
"can_expand": False,
|
"can_expand": False,
|
||||||
"children_media_class": None,
|
|
||||||
"thumbnail": None,
|
"thumbnail": None,
|
||||||
}
|
}
|
||||||
assert expected_child_audio in response["result"]["children"]
|
assert expected_child_audio in response["result"]["children"]
|
||||||
|
@ -5,6 +5,7 @@ from http import HTTPStatus
|
|||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant.components import media_player
|
from homeassistant.components import media_player
|
||||||
|
from homeassistant.components.media_player.browse_media import BrowseMedia
|
||||||
from homeassistant.components.websocket_api.const import TYPE_RESULT
|
from homeassistant.components.websocket_api.const import TYPE_RESULT
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF
|
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
@ -166,7 +167,14 @@ async def test_media_browse(hass, hass_ws_client):
|
|||||||
media_player.SUPPORT_BROWSE_MEDIA,
|
media_player.SUPPORT_BROWSE_MEDIA,
|
||||||
), patch(
|
), patch(
|
||||||
"homeassistant.components.media_player.MediaPlayerEntity.async_browse_media",
|
"homeassistant.components.media_player.MediaPlayerEntity.async_browse_media",
|
||||||
return_value={"bla": "yo"},
|
return_value=BrowseMedia(
|
||||||
|
media_class=media_player.MEDIA_CLASS_DIRECTORY,
|
||||||
|
media_content_id="mock-id",
|
||||||
|
media_content_type="mock-type",
|
||||||
|
title="Mock Title",
|
||||||
|
can_play=False,
|
||||||
|
can_expand=True,
|
||||||
|
),
|
||||||
) as mock_browse_media:
|
) as mock_browse_media:
|
||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
@ -183,7 +191,18 @@ async def test_media_browse(hass, hass_ws_client):
|
|||||||
assert msg["id"] == 5
|
assert msg["id"] == 5
|
||||||
assert msg["type"] == TYPE_RESULT
|
assert msg["type"] == TYPE_RESULT
|
||||||
assert msg["success"]
|
assert msg["success"]
|
||||||
assert msg["result"] == {"bla": "yo"}
|
assert msg["result"] == {
|
||||||
|
"title": "Mock Title",
|
||||||
|
"media_class": "directory",
|
||||||
|
"media_content_type": "mock-type",
|
||||||
|
"media_content_id": "mock-id",
|
||||||
|
"can_play": False,
|
||||||
|
"can_expand": True,
|
||||||
|
"children_media_class": None,
|
||||||
|
"thumbnail": None,
|
||||||
|
"not_shown": 0,
|
||||||
|
"children": [],
|
||||||
|
}
|
||||||
assert mock_browse_media.mock_calls[0][1] == ("album", "abcd")
|
assert mock_browse_media.mock_calls[0][1] == ("album", "abcd")
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
|
@ -58,6 +58,7 @@ async def test_async_browse_media(hass):
|
|||||||
assert media.title == "media"
|
assert media.title == "media"
|
||||||
assert len(media.children) == 1, media.children
|
assert len(media.children) == 1, media.children
|
||||||
media.children[0].title = "Epic Sax Guy 10 Hours"
|
media.children[0].title = "Epic Sax Guy 10 Hours"
|
||||||
|
assert media.not_shown == 1
|
||||||
|
|
||||||
# Test invalid media content
|
# Test invalid media content
|
||||||
with pytest.raises(BrowseError):
|
with pytest.raises(BrowseError):
|
||||||
|
@ -103,10 +103,10 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
|
|||||||
),
|
),
|
||||||
"can_play": False,
|
"can_play": False,
|
||||||
"can_expand": True,
|
"can_expand": True,
|
||||||
"children_media_class": "directory",
|
|
||||||
"thumbnail": None,
|
"thumbnail": None,
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"not_shown": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
media = await media_source.async_browse_media(
|
media = await media_source.async_browse_media(
|
||||||
@ -135,10 +135,10 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
|
|||||||
),
|
),
|
||||||
"can_play": False,
|
"can_play": False,
|
||||||
"can_expand": True,
|
"can_expand": True,
|
||||||
"children_media_class": "directory",
|
|
||||||
"thumbnail": None,
|
"thumbnail": None,
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"not_shown": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
media = await media_source.async_browse_media(
|
media = await media_source.async_browse_media(
|
||||||
@ -166,7 +166,6 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
|
|||||||
),
|
),
|
||||||
"can_play": False,
|
"can_play": False,
|
||||||
"can_expand": True,
|
"can_expand": True,
|
||||||
"children_media_class": "video",
|
|
||||||
"thumbnail": None,
|
"thumbnail": None,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -179,10 +178,10 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
|
|||||||
),
|
),
|
||||||
"can_play": False,
|
"can_play": False,
|
||||||
"can_expand": True,
|
"can_expand": True,
|
||||||
"children_media_class": "image",
|
|
||||||
"thumbnail": None,
|
"thumbnail": None,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
"not_shown": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
client.async_get_movies = AsyncMock(return_value=TEST_MOVIES)
|
client.async_get_movies = AsyncMock(return_value=TEST_MOVIES)
|
||||||
@ -213,10 +212,10 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
|
|||||||
),
|
),
|
||||||
"can_play": False,
|
"can_play": False,
|
||||||
"can_expand": True,
|
"can_expand": True,
|
||||||
"children_media_class": "directory",
|
|
||||||
"thumbnail": None,
|
"thumbnail": None,
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"not_shown": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
client.get_movie_url = Mock(return_value="http://movie")
|
client.get_movie_url = Mock(return_value="http://movie")
|
||||||
@ -248,7 +247,6 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
|
|||||||
),
|
),
|
||||||
"can_play": True,
|
"can_play": True,
|
||||||
"can_expand": False,
|
"can_expand": False,
|
||||||
"children_media_class": None,
|
|
||||||
"thumbnail": "http://movie",
|
"thumbnail": "http://movie",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -262,7 +260,6 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
|
|||||||
),
|
),
|
||||||
"can_play": True,
|
"can_play": True,
|
||||||
"can_expand": False,
|
"can_expand": False,
|
||||||
"children_media_class": None,
|
|
||||||
"thumbnail": "http://movie",
|
"thumbnail": "http://movie",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -276,10 +273,10 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None:
|
|||||||
),
|
),
|
||||||
"can_play": True,
|
"can_play": True,
|
||||||
"can_expand": False,
|
"can_expand": False,
|
||||||
"children_media_class": None,
|
|
||||||
"thumbnail": "http://movie",
|
"thumbnail": "http://movie",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
"not_shown": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -326,10 +323,10 @@ async def test_async_browse_media_images_success(hass: HomeAssistant) -> None:
|
|||||||
),
|
),
|
||||||
"can_play": False,
|
"can_play": False,
|
||||||
"can_expand": False,
|
"can_expand": False,
|
||||||
"children_media_class": None,
|
|
||||||
"thumbnail": "http://image",
|
"thumbnail": "http://image",
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"not_shown": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -479,4 +476,5 @@ async def test_async_resolve_media_failure(hass: HomeAssistant) -> None:
|
|||||||
"children_media_class": "video",
|
"children_media_class": "video",
|
||||||
"thumbnail": None,
|
"thumbnail": None,
|
||||||
"children": [],
|
"children": [],
|
||||||
|
"not_shown": 0,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user