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

Add Exclude Template Suppport #137

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ entities:
- <entity>
- <entity>
filter:
template: <template>
include_template: <template>
exclude_template: <template>
include:
- <filter>
- <filter>
Expand All @@ -43,7 +44,8 @@ sort: <sort_method>
- `card:` **Required.** The card to display. Specify this as you would specify any normal lovelace card, but ommit the `entities:` parameter.
- `entities:` Any entities added here will be added to the card before any filters are applied.
- `filter:`
- `template:` A jinja2 template evaluating to a whitespace- or comma-separated list of entity ids to include
- `include_template:` A jinja2 template evaluating to a whitespace- or comma-separated list of entity ids to include
- `exclude_template:` A jinja2 template evaluating to a whitespace- or comma-separated list of entity ids to exclude
- `include:` A list of filters specifying which entities to add to the card
- `exclude:` A list of filters specifying which entities to remove from the card
- `show_empty:` Whether to display the card if it has no entities. Default: `true`.
Expand Down Expand Up @@ -80,9 +82,10 @@ The filter section `template` takes a jinja2 template which evaluates to a list
## How it works
`auto-entities` creates a list of entities by:
1. Including every entitiy given in `entities:` (this allow nesting of `auto-entities`if you'd want to do that for some reason...)
2. Include every entity listed in a `filter.template` evaluation
2. Include every entity listed in a `filter.include_template` evaluation
3. Include all entities that matches **ALL** options of **ANY** filter in the `filter.include` section. The same entity may be included several times by different filters.
4. Remove all entities that matches **ALL** options on **ANY** filter in the `filter.exclude` section.
5. Remove every entity listed in a `filter.exclude_template` evaluation

It then creates a card based on the configuration given in `card:`, and fills in `entities:` of that card with the entities from above.

Expand Down Expand Up @@ -310,7 +313,7 @@ type: custom:auto-entities
card:
type: entities
filter:
template: |
include_template: |
{% for light in states.light %}
{% if light.state == "on" %}
{{ light.entity_id}},
Expand All @@ -324,7 +327,7 @@ filter:
>
> One is to redefine the template, e.g.:
> ```yaml
> template: |
> include_template: |
> {%if is_state('light.bed_light','on')%}light.bed_light{%endif%}
> {%if is_state('light.kitchen_lights','on')%}light.kitchen_lights{%endif%}
> {%if is_state('light.ceiling_lights','on')%}light.ceiling_lights{%endif%}
Expand All @@ -333,7 +336,7 @@ filter:
> The other option is to add a list of entities to monitor:
> ```yaml
> filter:
> template: |
> include_template: |
> ...etc...
> entity_ids:
> - light.bed_light
Expand Down
40 changes: 33 additions & 7 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,33 @@ class AutoEntities extends LitElement {
this._config = config;
this.hass = this.hass;
}
if(config.filter && config.filter.template) {
this.template = "";
if(String(config.filter.template).includes("{%") || String(config.filter.template).includes("{{")) {
// backwards compatibility
if(config.filter && config.filter.template && !config.filter.include_template){
config.filter.include_template = config.filter.template;
}

if(config.filter && config.filter.include_template) {
this.include_template = "";
if(String(config.filter.include_template).includes("{%") || String(config.filter.include_template).includes("{{")) {
subscribeRenderTemplate(null, (res) => {
this.template = res;
this.include_template = res;
this._getEntities();
}, {
template: config.filter.template,
template: config.filter.include_template,
variables: {config},
entity_ids: config.filter.entity_ids,
});
}
}

if(config.filter && config.filter.exclude_template){
this.exclude_template = "";
if(String(config.filter.exclude_template).includes("{%") || String(config.filter.exclude_template).includes("{{")) {
subscribeRenderTemplate(null, (res) => {
this.exclude_template = res;
this._getEntities();
}, {
template: config.filter.exclude_template,
variables: {config},
entity_ids: config.filter.entity_ids,
});
Expand Down Expand Up @@ -65,8 +84,8 @@ class AutoEntities extends LitElement {

if(!this.hass || !this._config.filter) return entities;

if(this.template) {
entities = entities.concat(this.template.split(/[\s,]+/).map(format_entities));
if(this.include_template) {
entities = entities.concat(this.include_template.split(/[\s,]+/).map(format_entities));
}
entities = entities.filter(Boolean);

Expand Down Expand Up @@ -110,6 +129,13 @@ class AutoEntities extends LitElement {
}
}

if(this.exclude_template){
let entitiesToRemove = this.exclude_template.split(/[\s,]+/);
entities = entities.filter((e) => {
return !entitiesToRemove.includes(e.entity);
});
}

if(this._config.sort) {
// Sort everything
entities = entities.sort(entity_sorter(this.hass, this._config.sort));
Expand Down