Add blog post for async_register_static_paths (#2222)

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
J. Nick Koston 2024-06-19 16:20:44 -05:00 committed by GitHub
parent 199270c42e
commit b9e6f10378
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 2 deletions

View File

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

View File

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