Add media dirs core configuration (#40071)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
1 parent
ce86112612
commit
5d518b5365
9 files changed
+157
-40
No files matched your search
@@ -5,6 +5,7 @@ from homeassistant.components import media_source
|
||||
from homeassistant.components.media_player.const import MEDIA_CLASS_DIRECTORY
|
||||
from homeassistant.components.media_player.errors import BrowseError
|
||||
from homeassistant.components.media_source import const
|
||||
from homeassistant.components.media_source.error import Unresolvable
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.async_mock import patch
|
||||
@@ -62,11 +63,23 @@ async def test_async_resolve_media(hass):
|
||||
assert await async_setup_component(hass, const.DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Test no media content
|
||||
media = await media_source.async_resolve_media(hass, "")
|
||||
media = await media_source.async_resolve_media(
|
||||
hass,
|
||||
media_source.generate_media_source_id(const.DOMAIN, "media/test.mp3"),
|
||||
)
|
||||
assert isinstance(media, media_source.models.PlayMedia)
|
||||
|
||||
|
||||
async def test_async_unresolve_media(hass):
|
||||
"""Test browse media."""
|
||||
assert await async_setup_component(hass, const.DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Test no media content
|
||||
with pytest.raises(Unresolvable):
|
||||
await media_source.async_resolve_media(hass, "")
|
||||
|
||||
|
||||
async def test_websocket_browse_media(hass, hass_ws_client):
|
||||
"""Test browse media websocket."""
|
||||
assert await async_setup_component(hass, const.DOMAIN, {})
|
||||
@@ -127,7 +140,7 @@ async def test_websocket_resolve_media(hass, hass_ws_client):
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
media = media_source.models.PlayMedia("/media/test.mp3", "audio/mpeg")
|
||||
media = media_source.models.PlayMedia("/local_source/media/test.mp3", "audio/mpeg")
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.media_source.async_resolve_media",
|
||||
|
||||
@@ -3,11 +3,18 @@ import pytest
|
||||
|
||||
from homeassistant.components import media_source
|
||||
from homeassistant.components.media_source import const
|
||||
from homeassistant.config import async_process_ha_core_config
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
async def test_async_browse_media(hass):
|
||||
"""Test browse media."""
|
||||
local_media = hass.config.path("media")
|
||||
await async_process_ha_core_config(
|
||||
hass, {"media_dirs": {"media": local_media, "recordings": local_media}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await async_setup_component(hass, const.DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -40,27 +47,53 @@ async def test_async_browse_media(hass):
|
||||
assert str(excinfo.value) == "Invalid path."
|
||||
|
||||
# Test successful listing
|
||||
media = await media_source.async_browse_media(
|
||||
hass, f"{const.URI_SCHEME}{const.DOMAIN}"
|
||||
)
|
||||
assert media
|
||||
|
||||
media = await media_source.async_browse_media(
|
||||
hass, f"{const.URI_SCHEME}{const.DOMAIN}/media/."
|
||||
)
|
||||
assert media
|
||||
|
||||
media = await media_source.async_browse_media(
|
||||
hass, f"{const.URI_SCHEME}{const.DOMAIN}/recordings/."
|
||||
)
|
||||
assert media
|
||||
|
||||
|
||||
async def test_media_view(hass, hass_client):
|
||||
"""Test media view."""
|
||||
local_media = hass.config.path("media")
|
||||
await async_process_ha_core_config(
|
||||
hass, {"media_dirs": {"media": local_media, "recordings": local_media}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await async_setup_component(hass, const.DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
client = await hass_client()
|
||||
|
||||
# Protects against non-existent files
|
||||
resp = await client.get("/media/invalid.txt")
|
||||
resp = await client.get("/local_source/media/invalid.txt")
|
||||
assert resp.status == 404
|
||||
|
||||
resp = await client.get("/local_source/recordings/invalid.txt")
|
||||
assert resp.status == 404
|
||||
|
||||
# Protects against non-media files
|
||||
resp = await client.get("/media/not_media.txt")
|
||||
resp = await client.get("/local_source/media/not_media.txt")
|
||||
assert resp.status == 404
|
||||
|
||||
# Protects against unknown local media sources
|
||||
resp = await client.get("/local_source/unknown_source/not_media.txt")
|
||||
assert resp.status == 404
|
||||
|
||||
# Fetch available media
|
||||
resp = await client.get("/media/test.mp3")
|
||||
resp = await client.get("/local_source/media/test.mp3")
|
||||
assert resp.status == 200
|
||||
|
||||
resp = await client.get("/local_source/recordings/test.mp3")
|
||||
assert resp.status == 200
|
||||
Reference in new issue
Block a user