Skip to content

Commit

Permalink
Fix counting entity states
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
DigiLive authored Dec 13, 2024
1 parent 9041e0b commit ebfd034
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
14 changes: 12 additions & 2 deletions src/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}}`
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/chips/CoverChip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
3 changes: 2 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"numbers": "Numbers",
"off": "Off",
"on": "On",
"open": "Open"
"open": "Open",
"unclosed": "Unclosed"
},
"input_select": {
"input_selects": "Input Selects"
Expand Down
3 changes: 2 additions & 1 deletion src/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"numbers": "Nummers",
"off": "Uit",
"on": "Aan",
"open": "Open"
"open": "Open",
"unclosed": "Ongesloten"
},
"input_select": {
"input_selects": "Lijsten"
Expand Down
4 changes: 2 additions & 2 deletions src/views/CoverView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
};

/**
Expand Down

0 comments on commit ebfd034

Please sign in to comment.