Skip to content

Commit

Permalink
add option to exclude items from card by tag, add tag for disabled items
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsfaber committed Jun 19, 2022
1 parent 3909108 commit 710017b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ Configuration is not *necessary*, except for defining a set of entities which yo
| `show_add_button` | boolean | *true* | Show button for creating new schedules. |
| `display_options` | dictionary | none | Configure which properties are displayed in the overview.<br>See [display options](#display-options) for more info. |
| `tags` | string/list | none | Filter schedules on one or more tags.<br>See [tags](#tags) for more info. |
| `exclude_tags` | string/list | none | Eliminate items from the schedules filtered by `tags`.<br>See [tags](#tags) for more info. |
### Standard configuration

The card includes a _standard configuration_.
Expand Down Expand Up @@ -654,6 +655,7 @@ The effect of assigning tags:
- You can also assign multiple tags to the card. In this case they will not be automatically applied to newly created (you should choose this via 'options').
- You can also assign multiple tags to a schedule. This allows you to make them appear in multiple cards (each with card having its own tag).
- You can assign `tags: none` to a card if you want to have only schedules without a tag showing up here.
- You can assign `tags: enabled` or `tags: disabled` to a card if you want to include (respectively) all enabled or disabled schedules.
- The option to assign tags on schedules is only available on cards which have the `tags` option set.

:warning: **Tip**: If you want to start using tags you will have to go through your current schedules and assign them with tags.
Expand All @@ -672,7 +674,6 @@ Since both cards have the light domain [included](#include), the created schedul

Now by assigning `tags` to the card configurations, you can filter them.


Config for card A:
```yaml
type: custom:scheduler-card
Expand All @@ -692,6 +693,22 @@ Result:

![tags example](https://github.com/nielsfaber/scheduler-card/blob/main/screenshots/tags_example.png?raw=true)


**Excluding tags**

For more advanced filtering it is possible to define `exclude_tags` to the card.

The tag or tags defined in `exclude_tags` will be used to hide schedules from a card.

The `exclude_tags` function works on top of the `tags` function, so you can eliminate schedules from the ones which are included via `tags`.

*Example use-case*

* All schedules for the holiday program are assigned with tag `holiday`.
* All schedules which occur in the weekend are assigned with tag `weekend`.
* By assigning `tags: weekend` and `exlude_tags: holiday` only the weekend schedules which are not part of the holiday program will be shown.


## Translations

The card is available in multiple languages. The card will automatically detect the appropriate translation based on the language setting for your user account in HA.
Expand Down
1 change: 1 addition & 0 deletions src/config-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export function ValidateConfig(config: any) {
});
}
Optional(config, 'tags', ['string', 'array']);
Optional(config, 'exclude_tags', ['string', 'array']);

if (errors.length) {
throw new Error(
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export interface CardConfig extends LovelaceCardConfig {
groups: GroupConfig[];
customize: Dictionary<EntityConfig>;
tags?: string[] | string;
exclude_tags?: string[] | string;
sort_by: string[] | string;
}

Expand Down
22 changes: 18 additions & 4 deletions src/views/scheduler-entities-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,27 @@ const isIncluded = (schedule: Schedule, config: CardConfig) => {
)
return false;

let included = true;

//filter items by tags
const filters = AsArray(config.tags);
if (filters.length) {
if ((schedule.tags || []).some(e => filters.includes(e))) return true;
else if (filters.includes('none') && !schedule.tags?.length) return true;
return false;
included = false;
if ((schedule.tags || []).some(e => filters.includes(e))) included = true;
else if (filters.includes('none') && !schedule.tags?.length) included = true;
else if (filters.includes('enabled') && schedule.enabled) included = true;
else if (filters.includes('disabled') && !schedule.enabled) included = true;
}

//filter items by exclude_tags
const excludeFilters = AsArray(config.exclude_tags);
if (excludeFilters.length && included) {
if ((schedule.tags || []).some(e => excludeFilters.includes(e))) included = false;
else if (excludeFilters.includes('none') && !schedule.tags?.length) included = false;
else if (excludeFilters.includes('enabled') && schedule.enabled) included = false;
else if (excludeFilters.includes('disabled') && !schedule.enabled) included = false;
}
return true;
return included;
};

//check whether entities and tags of schedule are included in configuration OR they should be discovered
Expand Down
7 changes: 5 additions & 2 deletions src/views/scheduler-options-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ export class SchedulerOptionsCard extends LitElement {
const tagEntries = await fetchTags(this.hass!);
const existingTags = tagEntries.map(e => e.name);
const configTags = AsArray(this.config.tags);
this.tags = [...existingTags, ...configTags.filter(e => !existingTags.includes(e) && e != 'none')];
this.tags = [
...existingTags,
...configTags.filter(e => !existingTags.includes(e) && !['none', 'disabled', 'enabled'].includes(e)),
];
}

(await (window as any).loadCardHelpers()).importMoreInfoControl('input_datetime');
Expand Down Expand Up @@ -675,7 +678,7 @@ export class SchedulerOptionsCard extends LitElement {
updateTags(ev: Event) {
let value = ((ev.target as HTMLInputElement).value as unknown) as string[];
value = value.map(e => e.trim());
value = value.filter(e => e != 'none');
value = value.filter(e => !['none', 'disabled', 'enabled'].includes(e));
value.sort(sortAlphabetically);

this.schedule = {
Expand Down

0 comments on commit 710017b

Please sign in to comment.