diff --git a/README.md b/README.md index 79f8de7..d9209f5 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,13 @@ To rerender the frontend, use the Refresh option from the three-dots-menu in Lov ![refresh](https://user-images.githubusercontent.com/1299821/62565489-2e655780-b887-11e9-86a1-2de868a4dc7d.png) +### Second of all + +Any yaml file that is to be processed with `lovelace_gen` *MUST* have the following as its first line: + +```yaml +# lovelace_gen +``` ### Let's continue @@ -125,6 +132,7 @@ cards: `button_card.yaml` ```yaml +# lovelace_gen {% if entity.startswith("light") %} type: light {% else %} @@ -168,6 +176,7 @@ This can also be used for pictures. ## Example `ui_lovelace.yaml` ```yaml +# lovelace_gen resources: # When you regenerate, the browser cache for this file will be invalidated - url: !file /local/card-mod.js @@ -180,6 +189,7 @@ views: `lovelace/my_cool_view.yaml` ```yaml +# lovelace_gen {% set my_lights = ["light.bed_light", "light.kitchen_lights", "light.ceiling_lights"] %} title: My view cards: @@ -214,6 +224,7 @@ cards: `lovelace/floorplan.yaml` ```yaml +# lovelace_gen {% macro lamp(entity, x, y) -%} {% if lamps %} - type: state-icon diff --git a/custom_components/lovelace_gen/__init__.py b/custom_components/lovelace_gen/__init__.py index 45793fa..3b5cc73 100644 --- a/custom_components/lovelace_gen/__init__.py +++ b/custom_components/lovelace_gen/__init__.py @@ -3,6 +3,7 @@ import logging import json import io import time +from collections import OrderedDict import jinja2 @@ -15,9 +16,18 @@ jinja = jinja2.Environment(loader=jinja2.FileSystemLoader("/")) def load_yaml(fname, args={}): try: - stream = io.StringIO(jinja.get_template(fname).render(args)) - stream.name = fname - return loader.yaml.load(stream, Loader=loader.SafeLineLoader) or {} + ll_gen = False + with open(fname, encoding="utf-8") as f: + if f.readline().lower().startswith("# lovelace_gen"): + ll_gen = True + + if ll_gen: + stream = io.StringIO(jinja.get_template(fname).render(args)) + stream.name = fname + return loader.yaml.load(stream, Loader=loader.SafeLineLoader) or OrderedDict() + else: + with open(fname, encoding="utf-8") as config_file: + return loader.yaml.load(config_file, Loader=loader.SafeLineLoader) or OrderedDict() except loader.yaml.YAMLError as exc: _LOGGER.error(str(exc)) raise HomeAssistantError(exc)