Documentation for Jinja import (#26436)

This commit is contained in:
David Poll 2023-03-13 03:01:14 -07:00 committed by GitHub
parent 9026d0ad20
commit 93f3e1efa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -65,6 +65,39 @@ extensions:
* [Loop Controls](https://jinja.palletsprojects.com/en/3.0.x/extensions/#loop-controls) (`break` and `continue`)
### Reusing Templates
You can write reusable Jinja templates by adding them to a `custom_jinja` folder under your
configuration directory. All template files must have the `.jinja` extension and be less than 5MiB.
Templates in this folder will be loaded at startup. To reload the templates without
restarting Home Assistant, invoke the `homeassistant.reload_custom_jinja` service.
Once the templates are loaded, Jinja [includes](https://jinja.palletsprojects.com/en/3.0.x/templates/#include) and [imports](https://jinja.palletsprojects.com/en/3.0.x/templates/#import) will work
using `config/custom_jinja` as the base directory.
For example, you might define a macro in a template in `config/custom_jinja/formatter.jinja`:
{% raw %}
```text
{% macro format_entity(entity_id) %}
{{ state_attr(entity_id, 'friendly_name') }} - {{ states(entity_id) }}
{% endmacro %}
```
{% endraw %}
In your automations, you could then reuse this macro by importing it:
{% raw %}
```text
{% from 'formatter.jinja' import format_entity %}
{{ format_entity('sensor.temperature') }}
```
{% endraw %}
## Home Assistant template extensions
Extensions allow templates to access all of the Home Assistant specific states and adds other convenience functions and filters.

View File

@ -29,11 +29,16 @@ Reload all YAML configuration that can be reloaded without restarting Home Assis
It calls the `reload` service on all domains that have it available. Additionally,
it reloads the core configuration (equivalent to calling
`homeassistant.reload_core_config`) and themes (`frontend.reload_themes`).
`homeassistant.reload_core_config`), themes (`frontend.reload_themes`), and custom Jinja (`homeassistant.reload_custom_jinja`).
Prior to reloading, a basic configuration check is performed. If that fails, the reload
will not be performed and will raise an error.
### Service `homeassistant.reload_custom_jinja`
Reload all Jinja templates in the `config/custom_jinja` directory. Changes to these templates
will take effect the next time an importing template is rendered.
### Service `homeassistant.reload_config_entry`
Reloads an integration config entry.