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
@@ -205,6 +205,7 @@ async def async_test_home_assistant(loop):
|
||||
hass.config.elevation = 0
|
||||
hass.config.time_zone = date_util.get_time_zone("US/Pacific")
|
||||
hass.config.units = METRIC_SYSTEM
|
||||
hass.config.media_dirs = {"media": get_test_config_dir("media")}
|
||||
hass.config.skip_pip = True
|
||||
|
||||
hass.config_entries = config_entries.ConfigEntries(hass, {})
|
||||
|
||||
@@ -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
|
||||
@@ -440,6 +440,7 @@ async def test_loading_configuration(hass):
|
||||
"allowlist_external_dirs": "/etc",
|
||||
"external_url": "https://www.example.com",
|
||||
"internal_url": "http://example.local",
|
||||
"media_dirs": {"mymedia": "/usr"},
|
||||
},
|
||||
)
|
||||
|
||||
@@ -453,6 +454,8 @@ async def test_loading_configuration(hass):
|
||||
assert hass.config.internal_url == "http://example.local"
|
||||
assert len(hass.config.allowlist_external_dirs) == 3
|
||||
assert "/etc" in hass.config.allowlist_external_dirs
|
||||
assert "/usr" in hass.config.allowlist_external_dirs
|
||||
assert hass.config.media_dirs == {"mymedia": "/usr"}
|
||||
assert hass.config.config_source == config_util.SOURCE_YAML
|
||||
|
||||
|
||||
@@ -483,6 +486,22 @@ async def test_loading_configuration_temperature_unit(hass):
|
||||
assert hass.config.config_source == config_util.SOURCE_YAML
|
||||
|
||||
|
||||
async def test_loading_configuration_default_media_dirs_docker(hass):
|
||||
"""Test loading core config onto hass object."""
|
||||
with patch("homeassistant.config.is_docker_env", return_value=True):
|
||||
await config_util.async_process_ha_core_config(
|
||||
hass,
|
||||
{
|
||||
"name": "Huis",
|
||||
},
|
||||
)
|
||||
|
||||
assert hass.config.location_name == "Huis"
|
||||
assert len(hass.config.allowlist_external_dirs) == 2
|
||||
assert "/media" in hass.config.allowlist_external_dirs
|
||||
assert hass.config.media_dirs == {"media": "/media"}
|
||||
|
||||
|
||||
async def test_loading_configuration_from_packages(hass):
|
||||
"""Test loading packages config onto hass object config."""
|
||||
await config_util.async_process_ha_core_config(
|
||||
|
||||
Reference in new issue
Block a user