Skip to content

Commit

Permalink
Stop lovelace_gen from breaking all the yaml which is not in lovelace...
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Aug 7, 2019
1 parent 878a96c commit 483172d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -125,6 +132,7 @@ cards:

`button_card.yaml`
```yaml
# lovelace_gen
{% if entity.startswith("light") %}
type: light
{% else %}
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -214,6 +224,7 @@ cards:

`lovelace/floorplan.yaml`
```yaml
# lovelace_gen
{% macro lamp(entity, x, y) -%}
{% if lamps %}
- type: state-icon
Expand Down
16 changes: 13 additions & 3 deletions custom_components/lovelace_gen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import io
import time
from collections import OrderedDict

import jinja2

Expand All @@ -15,9 +16,18 @@

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)
Expand Down

0 comments on commit 483172d

Please sign in to comment.