From b9e6f1037856d6a4ca8681f294bc7614936c97ad Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 19 Jun 2024 16:20:44 -0500 Subject: [PATCH] Add blog post for async_register_static_paths (#2222) Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- blog/2021-01-29-custom-integration-changes.md | 10 ++++-- .../2024-06-18-async_register_static_paths.md | 32 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 blog/2024-06-18-async_register_static_paths.md diff --git a/blog/2021-01-29-custom-integration-changes.md b/blog/2021-01-29-custom-integration-changes.md index ac286d9c..a7e55d16 100644 --- a/blog/2021-01-29-custom-integration-changes.md +++ b/blog/2021-01-29-custom-integration-changes.md @@ -40,14 +40,20 @@ If you are using the `hassfest` GitHub action, you will now start to see warning ## Serving files -Making resources available to the user is a common use case for custom integrations, whether that is images, panels, or enhancements the user can use in Lovelace. The only way one should serve static files from a path is to use `hass.http.register_static_path`. Use this method and avoid using your own, as this can lead to serious bugs or security issues. +Making resources available to the user is a common use case for custom integrations, whether that is images, panels, or enhancements the user can use in Lovelace. The only way one should serve static files from a path is to use `hass.http.async_register_static_paths`. Use this method and avoid using your own, as this can lead to serious bugs or security issues. ```python from pathlib import Path +from homeassisant.components.http import StaticPathConfig should_cache = False files_path = Path(__file__).parent / "static" -hass.http.register_static_path("/api/my_integration/static", str(files_path), should_cache) +files2_path = Path(__file__).parent / "static2" + +await hass.http.async_register_static_paths([ + StaticPathConfig("/api/my_integration/static", str(files_path), should_cache), + StaticPathConfig("/api/my_integration/static2", str(files2_path), should_cache) +]) ``` That's it for this update about custom integrations. Keep doing awesome stuff! Until next time 👋 diff --git a/blog/2024-06-18-async_register_static_paths.md b/blog/2024-06-18-async_register_static_paths.md new file mode 100644 index 00000000..df1100ec --- /dev/null +++ b/blog/2024-06-18-async_register_static_paths.md @@ -0,0 +1,32 @@ +--- +author: J. Nick Koston +authorURL: https://github.com/bdraco +title: Making http path registration async safe with `async_register_static_paths` +--- + +`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_paths([StaticPathConfig(url_path, path, cache_headers)])` + +The arguments to `async_register_static_paths` 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. + +For example, if your integration called `hass.http.register_static_path("/integrations/photos", "/config/photos", True)`, it should now call `await hass.http.async_register_static_paths([StaticPathConfig("/integrations/photos", "/config/photos", True)])` + +The `StaticPathConfig` `dataclass` should be imported from `homeassistant.components.http` + +`hass.http.register_static_path` will be removed in 2025.7 + +## Example + +```python +from pathlib import Path +from homeassistant.components.http import StaticPathConfig + +should_cache = False +files_path = Path(__file__).parent / "static" +files2_path = Path(__file__).parent / "static2" + +await hass.http.async_register_static_paths([ + StaticPathConfig("/api/my_integration/static", str(files_path), should_cache), + StaticPathConfig("/api/my_integration/static2", str(files2_path), should_cache) +]) +``` +