From ebfd034e20349d4c04d53f3ae92b8e571a354f91 Mon Sep 17 00:00:00 2001 From: Ferry Cools Date: Fri, 13 Dec 2024 07:29:15 +0100 Subject: [PATCH] Fix counting entity states * Add state filters to getCountTemplate() States `unavailable` and `unknown` are now excluded from the return value of `getCountTemplate()`, since there's no interest in counting those states. This change affect all entities. * Add states to unclosed cover count Aside from state `open`, the cover-chip and -view, will include state `opening` and `closing` to the count of unclosed covers. --- src/Helper.ts | 14 ++++++++++++-- src/chips/CoverChip.ts | 2 +- src/translations/en.json | 3 ++- src/translations/nl.json | 3 ++- src/views/CoverView.ts | 4 ++-- 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Helper.ts b/src/Helper.ts index 4ef868a..45b215f 100644 --- a/src/Helper.ts +++ b/src/Helper.ts @@ -218,9 +218,10 @@ class Helper { * Get a template string to define the number of a given domain's entities with a certain state. * * States are compared against a given value by a given operator. + * States `unavailable` and `unknown` are always excluded. * * @param {string} domain The domain of the entities. - * @param {string} operator The Comparison operator between state and value. + * @param {string} operator The comparison operator between state and value. * @param {string} value The value to which the state is compared against. * * @return {string} The template string. @@ -265,7 +266,16 @@ class Helper { states.push(...newStates); } - return `{% set entities = [${states}] %} {{ entities | selectattr('state','${operator}','${value}') | list | count }}`; + return ( + `{% set entities = [${states}] %} + {{ entities + | selectattr('state','${operator}','${value}') + | selectattr('state','ne','unavailable') + | selectattr('state','ne','unknown') + | list + | count + }}` + ); } /** diff --git a/src/chips/CoverChip.ts b/src/chips/CoverChip.ts index 5fae7b8..5dc43f7 100644 --- a/src/chips/CoverChip.ts +++ b/src/chips/CoverChip.ts @@ -22,7 +22,7 @@ class CoverChip extends AbstractChip { type: "template", icon: "mdi:window-open", icon_color: "cyan", - content: Helper.getCountTemplate("cover", "eq", "open"), + content: Helper.getCountTemplate("cover", "search", "(open|opening|closing)"), tap_action: { action: "none", }, diff --git a/src/translations/en.json b/src/translations/en.json index 038d8ab..4a77cdb 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -28,7 +28,8 @@ "numbers": "Numbers", "off": "Off", "on": "On", - "open": "Open" + "open": "Open", + "unclosed": "Unclosed" }, "input_select": { "input_selects": "Input Selects" diff --git a/src/translations/nl.json b/src/translations/nl.json index 5d17f5f..dbdebfb 100644 --- a/src/translations/nl.json +++ b/src/translations/nl.json @@ -28,7 +28,8 @@ "numbers": "Nummers", "off": "Uit", "on": "Aan", - "open": "Open" + "open": "Open", + "unclosed": "Ongesloten" }, "input_select": { "input_selects": "Lijsten" diff --git a/src/views/CoverView.ts b/src/views/CoverView.ts index 4722e46..4ac4dee 100644 --- a/src/views/CoverView.ts +++ b/src/views/CoverView.ts @@ -51,8 +51,8 @@ class CoverView extends AbstractView { #viewControllerCardConfig: cards.ControllerCardOptions = { title: Helper.customLocalize("cover.all_covers"), subtitle: - `${Helper.getCountTemplate(CoverView.#domain, "eq", "open")} ${Helper.customLocalize("cover.covers")} ` - + Helper.customLocalize("generic.open"), + `${Helper.getCountTemplate(CoverView.#domain, "search", "(open|opening|closing)")} ${Helper.customLocalize("cover.covers")} ` + + Helper.customLocalize("generic.unclosed"), }; /**