Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: use component-local variables in the configuration #63

Closed
nagyrobi opened this issue Jul 19, 2021 · 0 comments
Closed

Comments

@nagyrobi
Copy link
Collaborator

nagyrobi commented Jul 19, 2021

Is your feature request related to a problem? Please describe.
Current way of creating configurations supports templates which is great but sometimes it becomes quite hard to maintain changes to objects belonging to the same functionality or entity in Home Assistant. The same entity_id is referenced uniquely a lot of times.

For example in the thermostat/climate example climate.thermostat_1 is referenced 52 times.
In media player example media_player.sound_my_room is referenced 61 times.

This is even more true for pXbY object references, where all objects are on the same page. X could be a variable which could be specified at the beginning, and changing objects page can be done in a single place (likewise in the jsonl file, where page can be specified only once for the following objects).

It would be good to be able to use this for any repetitive part of the configuration, not just entity ids or page numbers.

Would make it easier to share code and multiply it to pages/plates simply by having to adjust only once the variables at the beginning.

Describe the solution you'd like
In the plate configuration, under the plate identifier slug (same level with objects) have a dictionary with user-defined variables which could be referenced in the configuration.
When parsing the configuration, resolve the variables back to their values before applying the template engine of HA. I think a programatic search and replace (regex?) would be enough.
Config should be possible something like:

openhasp:
  plate35:
    variables:
      - climate_thermostat_1: "climate.thermostat_room_1"  # assign to variable climate_thermostat_1 the name of entity in HA
      - _page_thermostat_: 6
      - media_player_1: "media_player.sound_my_room" 
      - media_player_sources: '["TV","Jazzy","Rádió 1","Retró","Sláger","Best FM","Forrás","Jzy Soul","Info"]'
      - _page_media_player_: 3

    objects:  # resolve the variables within the templates 
      - obj: "p_page_thermostat_b41"  # on/off switch
        properties:
          "val": "{{ 0 if (is_state('climate_thermostat_1', 'off') or is_state('climate_thermostat_1', 'unavailable')) else 1 }}"
        event:
          "down":
            - service_template: >
                {% if val == 0 -%}
                climate.turn_on
                {% else -%}
                climate.turn_off
                {% endif -%}
              entity_id: "climate_thermostat_1"

      - obj: "p_page_media_player_b2" # artist
        properties:
          "text": "{{ state_attr('media_player_1','media_artist') if state_attr('media_player_1','media_artist') else '-' }}"

      - obj: "p_page_media_player_b40" # sources buttons
        properties:  
          "options": media_player_sources
          "toggle": '{{ 1 if ( not state_attr("media_player_1","source") in media_player_sources) or (is_state("media_player_1","unavailable")) -}}'
          "val": >
            {% if not (is_state('media_player_1','unavailable')) -%}
            {% if not state_attr(media_player_1','source') in media_player_sources -%}-1{% else -%}
            {% for source in media_player_sources -%}
            {{loop.index - 1 if source == state_attr('media_player_1','source') }}
            {%-endfor%}
            {%- endif %}
            {%- endif %}

Describe alternatives you've considered
Currently the users have to search and replace all occurences in the config, when applying the examples to their plates, or when duplicating/moving code from one plate/page to another.

Additional context
N/A

akloeckner added a commit to akloeckner/openHASP-custom-component that referenced this issue Aug 8, 2023
This upgrades the current "list of services" restriction to allow the full syntax of HA scripts.

fixes HASwitchPlate#65

As a side-effect this also requires to generate a context for the script to run.

see HASwitchPlate#96

This can also be seen as a preparatory step to allow defining HASPObject-wide variables.

see HASwitchPlate#63
akloeckner added a commit to akloeckner/openHASP-custom-component that referenced this issue May 15, 2024
This upgrades the current "list of services" restriction to allow the full syntax of HA scripts.

fixes HASwitchPlate#65

As a side-effect this also requires to generate a context for the script to run.

see HASwitchPlate#96

This can also be seen as a preparatory step to allow defining HASPObject-wide variables.

see HASwitchPlate#63
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant