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

Example Function "is_valid_entity" verifies given entity_ids if they are exposed, visible, etc. #204

Open
jleinenbach opened this issue Apr 25, 2024 · 0 comments

Comments

@jleinenbach
Copy link

- spec:
    name: is_valid_entity
    description: >
      Checks if a given entity_id is exposed, verifies its configuration and visibility.
    parameters:
      type: object
      properties:
        entity_id:
          type: string
          description: The entity_id of the Home Assistant entity to check.
      required:
        - entity_id
  function:
    type: composite
    sequence:
      - type: sqlite
        query: >-
          {% if entity_id is defined and entity_id %}
            SELECT '{{ is_exposed(entity_id) }}' AS result;
          {% else %}
            SELECT 'undefined' AS result;
          {% endif %}
        response_variable: exposure_status
      - type: template
        value_template: >-
          {%- if entity_id is defined and entity_id %}
            {%- set entity_check = states[entity_id] %}
            {%- if entity_check %}
              {%- if entity_check.state not in ['unknown', 'unavailable'] %}
                {%- set exposure_info = 'Is exposed: ' ~ exposure_status %}
                {%- set config_info = ' Has config entry.' if config_entry_id(entity_id) else ' No config entry.' %}
                {%- set hidden_info = ' Is hidden.' if is_hidden_entity(entity_id) else ' Is not hidden.' %}
                {{ exposure_info ~ config_info ~ hidden_info }}
                {%- if has_value(entity_id) %}
                  {{ ' ' ~ entity_id ~ ' has a valid value.' }}
                {%- else %}
                  {{ ' ' ~ entity_id ~ ' does not have a valid value.' }}
                {%- endif %}
              {%- else %}
                {{ 'Entity state is ' ~ entity_check.state ~ ', cannot determine exposure reliably.' }}
              {%- endif %}
            {%- else %}
              {{ 'The specified entity_id ' ~ entity_id ~ ' does not exist. Use a search function to get the correct entity_id.' }}
            {%- endif %}
          {%- else %}
            {{ 'The specified entity_id is invalid or not provided. Please provide a valid entity_id.' }}
          {%- endif %}
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