Add global variables from integration configuration. Thanks, Dwains.
This commit is contained in:
parent
c9495d3bbc
commit
1878c92c91
28
README.md
28
README.md
@ -252,6 +252,34 @@ elements:
|
|||||||
|
|
||||||
# FAQ
|
# FAQ
|
||||||
|
|
||||||
|
### How can I do global variables?
|
||||||
|
You can add variables to the `lovelace_gen` configuration in `configuration.yaml` and then refernce them in lovelace using `{{ _global }}`.
|
||||||
|
|
||||||
|
E.g.:
|
||||||
|
```yaml
|
||||||
|
lovelace_gen:
|
||||||
|
rooms:
|
||||||
|
- living_room
|
||||||
|
- kitchen
|
||||||
|
- bed_room
|
||||||
|
```
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
type: entities
|
||||||
|
entities:
|
||||||
|
{% for room in _global.rooms %}
|
||||||
|
- type: custom:auto-entities
|
||||||
|
card:
|
||||||
|
type: custom:fold-entity-row
|
||||||
|
head:
|
||||||
|
type: section
|
||||||
|
label: {{ room|capitalize }}
|
||||||
|
filter:
|
||||||
|
include:
|
||||||
|
- area: {{ room }}
|
||||||
|
{% endfor %}
|
||||||
|
```
|
||||||
|
|
||||||
### Can I use this for my general Home Assistant configuration?
|
### Can I use this for my general Home Assistant configuration?
|
||||||
It's called **lovelace**\_gen for a reason...
|
It's called **lovelace**\_gen for a reason...
|
||||||
That being said - it *might* work. Or it might not. There's really no way to
|
That being said - it *might* work. Or it might not. There's really no way to
|
||||||
|
@ -14,6 +14,8 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
jinja = jinja2.Environment(loader=jinja2.FileSystemLoader("/"))
|
jinja = jinja2.Environment(loader=jinja2.FileSystemLoader("/"))
|
||||||
|
|
||||||
|
llgen_config = {}
|
||||||
|
|
||||||
def load_yaml(fname, args={}):
|
def load_yaml(fname, args={}):
|
||||||
try:
|
try:
|
||||||
ll_gen = False
|
ll_gen = False
|
||||||
@ -22,7 +24,7 @@ def load_yaml(fname, args={}):
|
|||||||
ll_gen = True
|
ll_gen = True
|
||||||
|
|
||||||
if ll_gen:
|
if ll_gen:
|
||||||
stream = io.StringIO(jinja.get_template(fname).render(args))
|
stream = io.StringIO(jinja.get_template(fname).render({**args, "_global": llgen_config}))
|
||||||
stream.name = fname
|
stream.name = fname
|
||||||
return loader.yaml.load(stream, Loader=loader.SafeLineLoader) or OrderedDict()
|
return loader.yaml.load(stream, Loader=loader.SafeLineLoader) or OrderedDict()
|
||||||
else:
|
else:
|
||||||
@ -60,5 +62,6 @@ loader.load_yaml = load_yaml
|
|||||||
loader.yaml.SafeLoader.add_constructor("!include", _include_yaml)
|
loader.yaml.SafeLoader.add_constructor("!include", _include_yaml)
|
||||||
loader.yaml.SafeLoader.add_constructor("!file", _uncache_file)
|
loader.yaml.SafeLoader.add_constructor("!file", _uncache_file)
|
||||||
|
|
||||||
async def async_setup(*_):
|
async def async_setup(hass, config):
|
||||||
|
llgen_config.update(config.get("lovelace_gen"));
|
||||||
return True
|
return True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user