thomasloven / hass-lovelace_gen

🔹 Improve the lovelace yaml parser for Home Assistant
MIT License
221 stars 22 forks source link

alternate template engines possible? #13

Open dkebler opened 4 years ago

dkebler commented 4 years ago

First of all...excellent and super useful component for groups of related entities like my bank of irrigation solenoids, thx.

Being a nodejs coder and pretty much a noob at python I'm finding jinja2 painful.

I understand the backend of HA is python but that shouldn't mean that lovelace can't support template engines using another language? Javascript for example...aaahh. If so how hard would that be to include others?

i.e. one of these https://colorlib.com/wp/top-templating-engines-for-javascript/

Jade/Pug is a good one and there is a maintained python package for that. https://pypi.org/project/pypugjs/

In the meantime can you give me a little jinja2 help?

I have this working

# lovelace_gen
{% set zones = ['1','2','3'] %}
icon: mdi:water
cards:
  - type: vertical-stack
    cards:
    - type: markdown
      content: >
        # Irrigation
    {% for zone in zones %}
    - type: entity-button
      entity: switch.irrigation_zone_{{ zone }}
      icon: mdi:water
      name: zone
      tap_action:
      action: toggle
    {% endfor %}

but I want to use a hash but can't find any jinga2 examples to help out and of have no idea how python does object hash.

# lovelace_gen
?? set a hash?? seems set is for iterable only
{% zones = { '1':'Front North', '2':'Front South', '3':'Front Beds'} %}
icon: mdi:water
cards:
  - type: vertical-stack
    cards:
    - type: markdown
      content: >
        # Irrigation
    {% for zone in Object.keys(zones) %}  // Object.keys is JS :-)
    - type: entity-button
      entity: switch.irrigation_zone_{{ zone }}
      icon: mdi:water
      name: {{ zones[zone] }}
      tap_action:
       action: toggle
    {% endfor %}
dkebler commented 4 years ago

I'd still be interested in alternative template engines but I did figure out how to iterate the keys/values of a object hash in jinja

# lovelace_gen
{% set zones = { '1':'Front North', '2':'Front South', '3':'Front Beds'} %}
icon: mdi:water
cards:
  - type: vertical-stack
    cards:
    - type: markdown
      content: >
        # Irrigation
    - type: horizontal-stack
      cards:
        {% for zone,name in zones.items() %}
        - type: entity-button
          entity: switch.irrigation_zone_{{ zone }}
          icon: mdi:water
          name: {{ name }}
          tap_action:
            action: toggle
        {% endfor %}