From 1bd6392a4ce2cbc6e79172f70ea991cf27d8efe8 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Thu, 29 Jul 2021 20:26:22 +0200 Subject: [PATCH] Add text when no statistics found (#9642) * Add text when no statistics found * Update src/components/entity/ha-statistic-picker.ts Co-authored-by: Paulus Schoutsen * fix typos * Update src/components/entity/ha-statistic-picker.ts * Prettier Co-authored-by: Paulus Schoutsen --- src/components/entity/ha-entities-picker.ts | 3 + src/components/entity/ha-statistic-picker.ts | 115 +++++++++++------- src/components/entity/ha-statistics-picker.ts | 3 + src/panels/config/core/ha-config-core-form.ts | 2 +- .../components/ha-energy-device-settings.ts | 2 +- .../components/ha-energy-grid-settings.ts | 2 +- .../components/ha-energy-solar-settings.ts | 2 +- src/translations/en.json | 7 ++ 8 files changed, 91 insertions(+), 45 deletions(-) diff --git a/src/components/entity/ha-entities-picker.ts b/src/components/entity/ha-entities-picker.ts index fff8041e72..19c20886fa 100644 --- a/src/components/entity/ha-entities-picker.ts +++ b/src/components/entity/ha-entities-picker.ts @@ -131,6 +131,9 @@ class HaEntitiesPickerLight extends LitElement { private async _addEntity(event: PolymerChangedEvent) { event.stopPropagation(); const toAdd = event.detail.value; + if (!toAdd) { + return; + } (event.currentTarget as any).value = ""; if (!toAdd) { return; diff --git a/src/components/entity/ha-statistic-picker.ts b/src/components/entity/ha-statistic-picker.ts index 912188ea8d..aa09f92d25 100644 --- a/src/components/entity/ha-statistic-picker.ts +++ b/src/components/entity/ha-statistic-picker.ts @@ -22,46 +22,12 @@ import { compare } from "../../common/string/compare"; import { getStatisticIds, StatisticsMetaData } from "../../data/history"; import { PolymerChangedEvent } from "../../polymer-types"; import { HomeAssistant } from "../../types"; +import { documentationUrl } from "../../util/documentation-url"; import "../ha-combo-box"; import type { HaComboBox } from "../ha-combo-box"; import "../ha-svg-icon"; import "./state-badge"; -// vaadin-combo-box-item - -const rowRenderer: ComboBoxLitRenderer<{ - id: string; - name: string; - state?: HassEntity; -}> = (item) => html` - - - - - ${item.name} - ${item.id} - - `; - @customElement("ha-statistic-picker") export class HaStatisticPicker extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @@ -99,6 +65,53 @@ export class HaStatisticPicker extends LitElement { private _init = false; + private _rowRenderer: ComboBoxLitRenderer<{ + id: string; + name: string; + state?: HassEntity; + }> = (item) => html` + + + + + ${item.name} + ${item.id === "" || item.id === "__missing" + ? html`${this.hass.localize( + "ui.components.statistic-picker.learn_more" + )}` + : item.id} + + `; + private _getStatistics = memoizeOne( ( statisticIds: StatisticsMetaData[], @@ -110,7 +123,7 @@ export class HaStatisticPicker extends LitElement { { id: "", name: this.hass.localize( - "ui.components.statistics-picker.no_statistics" + "ui.components.statistic-picker.no_statistics" ), }, ]; @@ -142,10 +155,27 @@ export class HaStatisticPicker extends LitElement { }); }); - if (output.length === 1) { - return output; + if (!output.length) { + return [ + { + id: "", + name: this.hass.localize("ui.components.statistic-picker.no_match"), + }, + ]; } - return output.sort((a, b) => compare(a.name || "", b.name || "")); + + if (output.length > 1) { + output.sort((a, b) => compare(a.name || "", b.name || "")); + } + + output.push({ + id: "__missing", + name: this.hass.localize( + "ui.components.statistic-picker.missing_entity" + ), + }); + + return output; } ); @@ -195,7 +225,7 @@ export class HaStatisticPicker extends LitElement { ? this.hass.localize("ui.components.statistic-picker.statistic") : this.label} .value=${this._value} - .renderer=${rowRenderer} + .renderer=${this._rowRenderer} .disabled=${this.disabled} item-value-path="id" item-id-path="id" @@ -216,7 +246,10 @@ export class HaStatisticPicker extends LitElement { private _statisticChanged(ev: PolymerChangedEvent) { ev.stopPropagation(); - const newValue = ev.detail.value; + let newValue = ev.detail.value; + if (newValue === "__missing") { + newValue = ""; + } if (newValue !== this._value) { this._setValue(newValue); diff --git a/src/components/entity/ha-statistics-picker.ts b/src/components/entity/ha-statistics-picker.ts index 3ad6af9203..22a6593ffd 100644 --- a/src/components/entity/ha-statistics-picker.ts +++ b/src/components/entity/ha-statistics-picker.ts @@ -90,6 +90,9 @@ class HaStatisticsPicker extends LitElement { private async _addStatistic(event: PolymerChangedEvent) { event.stopPropagation(); const toAdd = event.detail.value; + if (!toAdd) { + return; + } (event.currentTarget as any).value = ""; if (!toAdd) { return; diff --git a/src/panels/config/core/ha-config-core-form.ts b/src/panels/config/core/ha-config-core-form.ts index 980f1cd5f9..edc69e73e4 100644 --- a/src/panels/config/core/ha-config-core-form.ts +++ b/src/panels/config/core/ha-config-core-form.ts @@ -155,7 +155,7 @@ class ConfigCoreForm extends LitElement { ${this.hass.localize( "ui.panel.config.core.section.core.core_config.find_currency_value" )}