Example Function "is_valid_entity" verifies given entity_ids if they are exposed, visible, etc.
jleinenbach opened this issue · comments
Jens Leinenbach commented
- 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 %}