Skip to content

Commit

Permalink
fix: helpers for aggregate chips
Browse files Browse the repository at this point in the history
  • Loading branch information
Lebe1ge committed Jan 2, 2025
1 parent a8b169f commit 9e32ddd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 28 deletions.
32 changes: 18 additions & 14 deletions custom_components/linus_dashboard/www/linus-strategy.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

26 changes: 15 additions & 11 deletions src/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,11 @@ class Helper {
if (newStates) states.push(...newStates);
}

return `{% set entities = [${states}] %}{{ (entities | selectattr('state', 'ne', 'unknown') | selectattr('state', 'ne', 'unavailable') | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', '${device_class}') | map(attribute='state') | map('float') | sum / entities | length) | round(1) }} {% if ${states[0]}.attributes.unit_of_measurement is defined %} {{ ${states[0]}.attributes.unit_of_measurement }}{% endif %}`;
return `
{% set entities = [${states}] | selectattr('state', 'ne', 'unknown') | selectattr('state', 'ne', 'unavailable') | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', '${device_class}') | map(attribute='state') | map('float') | list %}
{% if entities | length > 0 %}
{{ (entities | sum / entities | length) | round(1) }} {% if ${states[0]}.attributes.unit_of_measurement is defined %} {{ ${states[0]}.attributes.unit_of_measurement }}{% endif %}
{% endif %}`;
}

/**
Expand Down Expand Up @@ -854,8 +858,8 @@ class Helper {

if (device_class === "battery") {
return `
{% set entities = [${states}] %}
{% set bl = entities | selectattr('state', 'ne', 'unknown') | selectattr('state', 'ne', 'unavailable') | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', '${device_class}') | map(attribute='state') | map('float') | sum / entities | length %}
{% set entities = [${states}] | selectattr('state', 'ne', 'unknown') | selectattr('state', 'ne', 'unavailable') | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', '${device_class}') | map(attribute='state') | map('float') | list %}
{% set bl = entities | sum / entities | length %}
{% if bl < 20 %}
red
{% elif bl < 30 %}
Expand All @@ -870,8 +874,8 @@ class Helper {

if (device_class === "temperature") {
return `
{% set entities = [${states}] %}
{% set bl = entities | selectattr('state', 'ne', 'unknown') | selectattr('state', 'ne', 'unavailable') | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', '${device_class}') | map(attribute='state') | map('float') | sum / entities | length %}
{% set entities = [${states}] | selectattr('state', 'ne', 'unknown') | selectattr('state', 'ne', 'unavailable') | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', '${device_class}') | map(attribute='state') | map('float') | list %}
{% set bl = entities | sum / entities | length %}
{% if bl < 20 %}
blue
{% elif bl < 30 %}
Expand All @@ -886,8 +890,8 @@ class Helper {

if (device_class === "humidity") {
return `
{% set entities = [${states}] %}
{% set humidity = entities | selectattr('state', 'ne', 'unknown') | selectattr('state', 'ne', 'unavailable') | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', '${device_class}') | map(attribute='state') | map('float') | sum / entities | length %}
{% set entities = [${states}] | selectattr('state', 'ne', 'unknown') | selectattr('state', 'ne', 'unavailable') | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', '${device_class}') | map(attribute='state') | map('float') | list %}
{% set humidity = entities | sum / entities | length %}
{% if humidity < 30 %}
blue
{% elif humidity >= 30 and humidity <= 60 %}
Expand Down Expand Up @@ -919,8 +923,8 @@ class Helper {

if (device_class === "battery") {
return `
{% set entities = [${states}] %}
{% set bl = entities | selectattr('state', 'ne', 'unknown') | selectattr('state', 'ne', 'unavailable') | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', '${device_class}') | map(attribute='state') | map('float') | sum / entities | length %}
{% set entities = [${states}] | selectattr('state', 'ne', 'unknown') | selectattr('state', 'ne', 'unavailable') | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', '${device_class}') | map(attribute='state') | map('float') | list %}
{% set bl = entities | sum / entities | length %}
{% if bl == 'unknown' or bl == 'unavailable' %}
{% elif bl < 10 %} mdi:battery-outline
{% elif bl < 20 %} mdi:battery-10
Expand All @@ -939,8 +943,8 @@ class Helper {

if (device_class === "temperature") {
return `
{% set entities = [${states}] %}
{% set bl = entities | selectattr('state', 'ne', 'unknown') | selectattr('state', 'ne', 'unavailable') | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', '${device_class}') | map(attribute='state') | map('float') | sum / entities | length %}
{% set entities = [${states}] | selectattr('state', 'ne', 'unknown') | selectattr('state', 'ne', 'unavailable') | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'eq', '${device_class}') | map(attribute='state') | map('float') | list %}
{% set bl = entities | sum / entities | length %}
{% if bl < 20 %}
mdi:thermometer-low
{% elif bl < 30 %}
Expand Down
2 changes: 1 addition & 1 deletion src/views/AbstractView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ abstract class AbstractView {
titleNavigate: slugify(floor.name)
};
if (this.#domain) {
if (!AGGREGATE_DOMAINS.includes(this.#domain) || this.#device_class) {
if (floor.floor_id !== UNDISCLOSED && (!AGGREGATE_DOMAINS.includes(this.#domain) || this.#device_class)) {
titleSectionOptions.showControls = Helper.strategyOptions.domains[this.#domain].showControls;
titleSectionOptions.extraControls = Helper.strategyOptions.domains[this.#domain].extraControls;
titleSectionOptions.controlChipOptions = {
Expand Down
2 changes: 1 addition & 1 deletion src/views/HomeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class HomeView {
type: "custom:mushroom-chips-card",
alignment: "end",
chips: [
temperature &&
floor.floor_id !== UNDISCLOSED && temperature &&
new AggregateChip({
device_class: "temperature",
show_content: true,
Expand Down

0 comments on commit 9e32ddd

Please sign in to comment.