mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Deprecate register_static_path in favor of async_register_static_paths (#119895)
* Deprecate register_static_path in favor of async_register_static_path `hass.http.register_static_path` is deprecated because it does blocking I/O in the event loop, instead call `await hass.http.async_register_static_path([StaticPathConfig(url_path, path, cache_headers)])` The arguments to `async_register_static_path` are the same as `register_static_path` except they are wrapped in the `StaticPathConfig` dataclass and an iterable of them is accepted to allow registering multiple paths at once to avoid multiple executor jobs. * add date * spacing
This commit is contained in:
parent
b11801df50
commit
6a3778c48e
@ -34,7 +34,7 @@ from homeassistant.components.network import async_get_source_ip
|
|||||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP, SERVER_PORT
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP, SERVER_PORT
|
||||||
from homeassistant.core import Event, HomeAssistant, callback
|
from homeassistant.core import Event, HomeAssistant, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import storage
|
from homeassistant.helpers import frame, storage
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.http import (
|
from homeassistant.helpers.http import (
|
||||||
KEY_ALLOW_CONFIGURED_CORS,
|
KEY_ALLOW_CONFIGURED_CORS,
|
||||||
@ -480,6 +480,16 @@ class HomeAssistantHTTP:
|
|||||||
self, url_path: str, path: str, cache_headers: bool = True
|
self, url_path: str, path: str, cache_headers: bool = True
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Register a folder or file to serve as a static path."""
|
"""Register a folder or file to serve as a static path."""
|
||||||
|
frame.report(
|
||||||
|
"calls hass.http.register_static_path which is deprecated because "
|
||||||
|
"it does blocking I/O in the event loop, instead "
|
||||||
|
"call `await hass.http.async_register_static_path("
|
||||||
|
f'[StaticPathConfig("{url_path}", "{path}", {cache_headers})])`; '
|
||||||
|
"This function will be removed in 2025.7",
|
||||||
|
exclude_integrations={"http"},
|
||||||
|
error_if_core=False,
|
||||||
|
error_if_integration=False,
|
||||||
|
)
|
||||||
configs = [StaticPathConfig(url_path, path, cache_headers)]
|
configs = [StaticPathConfig(url_path, path, cache_headers)]
|
||||||
resources = self._make_static_resources(configs)
|
resources = self._make_static_resources(configs)
|
||||||
self._async_register_static_paths(configs, resources)
|
self._async_register_static_paths(configs, resources)
|
||||||
|
@ -526,3 +526,24 @@ async def test_logging(
|
|||||||
response = await client.get("/api/states/logging.entity")
|
response = await client.get("/api/states/logging.entity")
|
||||||
assert response.status == HTTPStatus.OK
|
assert response.status == HTTPStatus.OK
|
||||||
assert "GET /api/states/logging.entity" not in caplog.text
|
assert "GET /api/states/logging.entity" not in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
async def test_register_static_paths(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
hass_client: ClientSessionGenerator,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
) -> None:
|
||||||
|
"""Test registering a static path with old api."""
|
||||||
|
assert await async_setup_component(hass, "frontend", {})
|
||||||
|
path = str(Path(__file__).parent)
|
||||||
|
hass.http.register_static_path("/something", path)
|
||||||
|
client = await hass_client()
|
||||||
|
resp = await client.get("/something/__init__.py")
|
||||||
|
assert resp.status == HTTPStatus.OK
|
||||||
|
|
||||||
|
assert (
|
||||||
|
"Detected code that calls hass.http.register_static_path "
|
||||||
|
"which is deprecated because it does blocking I/O in the "
|
||||||
|
"event loop, instead call "
|
||||||
|
"`await hass.http.async_register_static_path"
|
||||||
|
) in caplog.text
|
||||||
|
Loading…
x
Reference in New Issue
Block a user