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:
J. Nick Koston 2024-06-18 20:51:24 -05:00 committed by GitHub
parent b11801df50
commit 6a3778c48e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 1 deletions

View File

@ -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)

View File

@ -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