thomasloven / hass-lovelace_gen

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

Reusing macros from templates in lovelace_gen. #52

Open benjycov opened 1 year ago

benjycov commented 1 year ago

I've taken my code reuse to a whole new level and now find myself in the scenario where I have macros that I want to both call from within templates and lovelace_gen. The problem of course is the path, because HA templates look directly in /config/custom_templates, and lovelace_gen needs it to be explicit.

{% macro example() %}
{%- set source_path = '/config/custom_templates/' -%}
{% from source_path + 'data.jinja' import my_data %}
---  do some stuff ---
{% endmacro %}

Can anyone think of any way of detecting from within the macro if its running under lovelace_gen or HA?

I've tried by using the lovelace_gen _global variables, in HA you can use the states function to test the lack of existence of a _global.something, but of course states() isn't available to lovelace_gen, also is defined/undefined doesn't work in HA as it throws an error before it checks it.

The only work arounds I have are to pass a variable in telling the macro where its being called from, or by maintaining two code sets (or wrapping the code somehow) with the source_path set differently, neither are perfect solutions.

Any ideas?

wbyoung commented 7 months ago

I've just started exploring this integration. In addition to the above, I've noticed that break is not supported in Jinja templates, so it seems like the extensions HA enables are not enabled either.