From c47ba65c3b6113faf0d5f47b3e1763ec345e46c9 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Tue, 18 Dec 2018 04:15:32 -0600 Subject: [PATCH] :broom: remove unused configs when empty (#2347) * Remove unused configs when empty * Handle unused numbers & fix glance entities * Sneak in the chunk names --- .../lovelace/cards/hui-alarm-panel-card.ts | 2 +- .../lovelace/cards/hui-entities-card.ts | 2 +- src/panels/lovelace/cards/hui-gauge-card.ts | 2 +- src/panels/lovelace/cards/hui-glance-card.ts | 2 +- src/panels/lovelace/cards/hui-iframe-card.ts | 2 +- src/panels/lovelace/cards/hui-light-card.ts | 2 +- .../lovelace/cards/hui-markdown-card.ts | 2 +- .../lovelace/cards/hui-media-control-card.js | 2 +- .../lovelace/cards/hui-plant-status-card.js | 2 +- src/panels/lovelace/cards/hui-sensor-card.ts | 2 +- .../lovelace/cards/hui-shopping-list-card.ts | 2 +- .../lovelace/cards/hui-thermostat-card.ts | 2 +- .../hui-alarm-panel-card-editor.ts | 12 +++++--- .../hui-entities-card-editor.ts | 14 +++++---- .../config-elements/hui-gauge-card-editor.ts | 15 +++++++--- .../config-elements/hui-glance-card-editor.ts | 29 ++++++++++++------- .../config-elements/hui-iframe-card-editor.ts | 6 +++- .../config-elements/hui-light-card-editor.ts | 12 +++++--- .../hui-markdown-card-editor.ts | 12 +++++--- .../hui-media-control-card-editor.ts | 12 +++++--- .../hui-plant-status-card-editor.ts | 12 +++++--- .../config-elements/hui-sensor-card-editor.ts | 18 +++++++----- .../hui-shopping-list-editor.ts | 12 +++++--- .../hui-thermostat-card-editor.ts | 6 +++- 24 files changed, 119 insertions(+), 65 deletions(-) diff --git a/src/panels/lovelace/cards/hui-alarm-panel-card.ts b/src/panels/lovelace/cards/hui-alarm-panel-card.ts index ae83f1b7ca..35f49fe8ef 100644 --- a/src/panels/lovelace/cards/hui-alarm-panel-card.ts +++ b/src/panels/lovelace/cards/hui-alarm-panel-card.ts @@ -41,7 +41,7 @@ export interface Config extends LovelaceCardConfig { class HuiAlarmPanelCard extends hassLocalizeLitMixin(LitElement) implements LovelaceCard { public static async getConfigElement() { - await import("../editor/config-elements/hui-alarm-panel-card-editor"); + await import(/* webpackChunkName: "hui-alarm-panel-card-editor" */ "../editor/config-elements/hui-alarm-panel-card-editor"); return document.createElement("hui-alarm-panel-card-editor"); } diff --git a/src/panels/lovelace/cards/hui-entities-card.ts b/src/panels/lovelace/cards/hui-entities-card.ts index 8fd4e5437d..13e36603f6 100644 --- a/src/panels/lovelace/cards/hui-entities-card.ts +++ b/src/panels/lovelace/cards/hui-entities-card.ts @@ -40,7 +40,7 @@ export interface Config extends LovelaceCardConfig { class HuiEntitiesCard extends hassLocalizeLitMixin(LitElement) implements LovelaceCard { public static async getConfigElement(): Promise { - await import("../editor/config-elements/hui-entities-card-editor"); + await import(/* webpackChunkName: "hui-entities-card-editor" */ "../editor/config-elements/hui-entities-card-editor"); return document.createElement("hui-entities-card-editor"); } diff --git a/src/panels/lovelace/cards/hui-gauge-card.ts b/src/panels/lovelace/cards/hui-gauge-card.ts index cedbec8af9..dd72db1355 100644 --- a/src/panels/lovelace/cards/hui-gauge-card.ts +++ b/src/panels/lovelace/cards/hui-gauge-card.ts @@ -47,7 +47,7 @@ export const severityMap = { class HuiGaugeCard extends LitElement implements LovelaceCard { public static async getConfigElement(): Promise { - await import("../editor/config-elements/hui-gauge-card-editor"); + await import(/* webpackChunkName: "hui-gauge-card-editor" */ "../editor/config-elements/hui-gauge-card-editor"); return document.createElement("hui-gauge-card-editor"); } public static getStubConfig(): object { diff --git a/src/panels/lovelace/cards/hui-glance-card.ts b/src/panels/lovelace/cards/hui-glance-card.ts index 537bc405e2..7d07f62b49 100644 --- a/src/panels/lovelace/cards/hui-glance-card.ts +++ b/src/panels/lovelace/cards/hui-glance-card.ts @@ -41,7 +41,7 @@ export interface Config extends LovelaceCardConfig { export class HuiGlanceCard extends hassLocalizeLitMixin(LitElement) implements LovelaceCard { public static async getConfigElement(): Promise { - await import("../editor/config-elements/hui-glance-card-editor"); + await import(/* webpackChunkName: "hui-glance-card-editor" */ "../editor/config-elements/hui-glance-card-editor"); return document.createElement("hui-glance-card-editor"); } public static getStubConfig(): object { diff --git a/src/panels/lovelace/cards/hui-iframe-card.ts b/src/panels/lovelace/cards/hui-iframe-card.ts index bc67b4f8d4..0b43162179 100644 --- a/src/panels/lovelace/cards/hui-iframe-card.ts +++ b/src/panels/lovelace/cards/hui-iframe-card.ts @@ -15,7 +15,7 @@ export interface Config extends LovelaceCardConfig { export class HuiIframeCard extends LitElement implements LovelaceCard { public static async getConfigElement(): Promise { - await import("../editor/config-elements/hui-iframe-card-editor"); + await import(/* webpackChunkName: "hui-iframe-card-editor" */ "../editor/config-elements/hui-iframe-card-editor"); return document.createElement("hui-iframe-card-editor"); } public static getStubConfig(): object { diff --git a/src/panels/lovelace/cards/hui-light-card.ts b/src/panels/lovelace/cards/hui-light-card.ts index b7556f700a..22e58e4ca8 100644 --- a/src/panels/lovelace/cards/hui-light-card.ts +++ b/src/panels/lovelace/cards/hui-light-card.ts @@ -48,7 +48,7 @@ export interface Config extends LovelaceCardConfig { export class HuiLightCard extends hassLocalizeLitMixin(LitElement) implements LovelaceCard { public static async getConfigElement(): Promise { - await import("../editor/config-elements/hui-light-card-editor"); + await import(/* webpackChunkName: "hui-light-card-editor" */ "../editor/config-elements/hui-light-card-editor"); return document.createElement("hui-light-card-editor"); } public static getStubConfig(): object { diff --git a/src/panels/lovelace/cards/hui-markdown-card.ts b/src/panels/lovelace/cards/hui-markdown-card.ts index 4c33ebf4bc..e61be9c63c 100644 --- a/src/panels/lovelace/cards/hui-markdown-card.ts +++ b/src/panels/lovelace/cards/hui-markdown-card.ts @@ -15,7 +15,7 @@ export interface Config extends LovelaceCardConfig { export class HuiMarkdownCard extends LitElement implements LovelaceCard { public static async getConfigElement(): Promise { - await import("../editor/config-elements/hui-markdown-card-editor"); + await import(/* webpackChunkName: "hui-markdown-card-editor" */ "../editor/config-elements/hui-markdown-card-editor"); return document.createElement("hui-markdown-card-editor"); } public static getStubConfig(): object { diff --git a/src/panels/lovelace/cards/hui-media-control-card.js b/src/panels/lovelace/cards/hui-media-control-card.js index f80b137363..dc78585aa4 100644 --- a/src/panels/lovelace/cards/hui-media-control-card.js +++ b/src/panels/lovelace/cards/hui-media-control-card.js @@ -9,7 +9,7 @@ export const Config = { class HuiMediaControlCard extends LegacyWrapperCard { static async getConfigElement() { - await import("../editor/config-elements/hui-media-control-card-editor"); + await import(/* webpackChunkName: "hui-media-control-card-editor" */ "../editor/config-elements/hui-media-control-card-editor"); return document.createElement("hui-media-control-card-editor"); } diff --git a/src/panels/lovelace/cards/hui-plant-status-card.js b/src/panels/lovelace/cards/hui-plant-status-card.js index b8646786fe..d45c8cc2a7 100644 --- a/src/panels/lovelace/cards/hui-plant-status-card.js +++ b/src/panels/lovelace/cards/hui-plant-status-card.js @@ -10,7 +10,7 @@ export const Config = { class HuiPlantStatusCard extends LegacyWrapperCard { static async getConfigElement() { - await import("../editor/config-elements/hui-plant-status-card-editor"); + await import(/* webpackChunkName: "hui-plant-status-card-editor" */ "../editor/config-elements/hui-plant-status-card-editor"); return document.createElement("hui-plant-status-card-editor"); } diff --git a/src/panels/lovelace/cards/hui-sensor-card.ts b/src/panels/lovelace/cards/hui-sensor-card.ts index 2b1f40c990..2a8394bea4 100755 --- a/src/panels/lovelace/cards/hui-sensor-card.ts +++ b/src/panels/lovelace/cards/hui-sensor-card.ts @@ -146,7 +146,7 @@ export interface Config extends LovelaceCardConfig { class HuiSensorCard extends LitElement implements LovelaceCard { public static async getConfigElement(): Promise { - await import("../editor/config-elements/hui-sensor-card-editor"); + await import(/* webpackChunkName: "hui-sensor-card-editor" */ "../editor/config-elements/hui-sensor-card-editor"); return document.createElement("hui-sensor-card-editor"); } diff --git a/src/panels/lovelace/cards/hui-shopping-list-card.ts b/src/panels/lovelace/cards/hui-shopping-list-card.ts index dc28ae5d4d..c0cff3b086 100644 --- a/src/panels/lovelace/cards/hui-shopping-list-card.ts +++ b/src/panels/lovelace/cards/hui-shopping-list-card.ts @@ -26,7 +26,7 @@ export interface Config extends LovelaceCardConfig { class HuiShoppingListCard extends hassLocalizeLitMixin(LitElement) implements LovelaceCard { public static async getConfigElement(): Promise { - await import("../editor/config-elements/hui-shopping-list-editor"); + await import(/* webpackChunkName: "hui-shopping-list-editor" */ "../editor/config-elements/hui-shopping-list-editor"); return document.createElement("hui-shopping-list-card-editor"); } public static getStubConfig(): object { diff --git a/src/panels/lovelace/cards/hui-thermostat-card.ts b/src/panels/lovelace/cards/hui-thermostat-card.ts index b0f099b496..c39a2c3b66 100644 --- a/src/panels/lovelace/cards/hui-thermostat-card.ts +++ b/src/panels/lovelace/cards/hui-thermostat-card.ts @@ -58,7 +58,7 @@ function formatTemp(temps: string[]): string { export class HuiThermostatCard extends hassLocalizeLitMixin(LitElement) implements LovelaceCard { public static async getConfigElement(): Promise { - await import("../editor/config-elements/hui-thermostat-card-editor"); + await import(/* webpackChunkName: "hui-thermostat-card-editor" */ "../editor/config-elements/hui-thermostat-card-editor"); return document.createElement("hui-thermostat-card-editor"); } diff --git a/src/panels/lovelace/editor/config-elements/hui-alarm-panel-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-alarm-panel-card-editor.ts index 85c78a5f12..f364c3cb7b 100644 --- a/src/panels/lovelace/editor/config-elements/hui-alarm-panel-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-alarm-panel-card-editor.ts @@ -173,10 +173,14 @@ export class HuiAlarmPanelCardEditor extends hassLocalizeLitMixin(LitElement) return; } if (target.configValue) { - this._config = { - ...this._config, - [target.configValue!]: target.value, - }; + if (target.value === "") { + delete this._config[target.configValue!]; + } else { + this._config = { + ...this._config, + [target.configValue!]: target.value, + }; + } } fireEvent(this, "config-changed", { config: this._config }); } diff --git a/src/panels/lovelace/editor/config-elements/hui-entities-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-entities-card-editor.ts index 846b379738..c13b303a1a 100644 --- a/src/panels/lovelace/editor/config-elements/hui-entities-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-entities-card-editor.ts @@ -115,11 +115,15 @@ export class HuiEntitiesCardEditor extends hassLocalizeLitMixin(LitElement) this._config.entities = ev.detail.entities; this._configEntities = processEditorEntities(this._config.entities); } else if (target.configValue) { - this._config = { - ...this._config, - [target.configValue]: - target.checked !== undefined ? target.checked : target.value, - }; + if (target.value === "") { + delete this._config[target.configValue!]; + } else { + this._config = { + ...this._config, + [target.configValue]: + target.checked !== undefined ? target.checked : target.value, + }; + } } fireEvent(this, "config-changed", { config: this._config }); diff --git a/src/panels/lovelace/editor/config-elements/hui-gauge-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-gauge-card-editor.ts index 6a430f514c..de72b02709 100644 --- a/src/panels/lovelace/editor/config-elements/hui-gauge-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-gauge-card-editor.ts @@ -218,11 +218,18 @@ export class HuiGaugeCardEditor extends hassLocalizeLitMixin(LitElement) const target = ev.target! as EditorTarget; if (target.configValue) { - let value: any = target.value; - if (target.type === "number") { - value = Number(value); + if ( + target.value === "" || + (target.type === "number" && isNaN(Number(target.value))) + ) { + delete this._config[target.configValue!]; + } else { + let value: any = target.value; + if (target.type === "number") { + value = Number(value); + } + this._config = { ...this._config, [target.configValue!]: value }; } - this._config = { ...this._config, [target.configValue!]: value }; } fireEvent(this, "config-changed", { config: this._config }); } diff --git a/src/panels/lovelace/editor/config-elements/hui-glance-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-glance-card-editor.ts index f18aff598d..da8b1ff2cd 100644 --- a/src/panels/lovelace/editor/config-elements/hui-glance-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-glance-card-editor.ts @@ -64,8 +64,8 @@ export class HuiGlanceCardEditor extends hassLocalizeLitMixin(LitElement) return this._config!.theme || "Backend-selected"; } - get _columns(): string { - return this._config!.columns ? String(this._config!.columns) : ""; + get _columns(): number { + return this._config!.columns || NaN; } protected render(): TemplateResult { @@ -126,22 +126,29 @@ export class HuiGlanceCardEditor extends hassLocalizeLitMixin(LitElement) } const target = ev.target! as EditorTarget; - if (this[`_${target.configValue}`] === target.value) { + if (target.configValue && this[`_${target.configValue}`] === target.value) { return; } if (ev.detail && ev.detail.entities) { this._config.entities = ev.detail.entities; this._configEntities = processEditorEntities(this._config.entities); } else if (target.configValue) { - let value: any = target.value; - if (target.type === "number") { - value = Number(value); + if ( + target.value === "" || + (target.type === "number" && isNaN(Number(target.value))) + ) { + delete this._config[target.configValue!]; + } else { + let value: any = target.value; + if (target.type === "number") { + value = Number(value); + } + this._config = { + ...this._config, + [target.configValue!]: + target.checked !== undefined ? target.checked : value, + }; } - this._config = { - ...this._config, - [target.configValue!]: - target.checked !== undefined ? target.checked : value, - }; } fireEvent(this, "config-changed", { config: this._config }); } diff --git a/src/panels/lovelace/editor/config-elements/hui-iframe-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-iframe-card-editor.ts index 78531eeff6..673156032d 100644 --- a/src/panels/lovelace/editor/config-elements/hui-iframe-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-iframe-card-editor.ts @@ -92,7 +92,11 @@ export class HuiIframeCardEditor extends hassLocalizeLitMixin(LitElement) return; } if (target.configValue) { - this._config = { ...this._config, [target.configValue!]: value }; + if (target.value === "") { + delete this._config[target.configValue!]; + } else { + this._config = { ...this._config, [target.configValue!]: value }; + } } fireEvent(this, "config-changed", { config: this._config }); } diff --git a/src/panels/lovelace/editor/config-elements/hui-light-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-light-card-editor.ts index 26c2a4b922..ba65142b1b 100644 --- a/src/panels/lovelace/editor/config-elements/hui-light-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-light-card-editor.ts @@ -91,10 +91,14 @@ export class HuiLightCardEditor extends hassLocalizeLitMixin(LitElement) return; } if (target.configValue) { - this._config = { - ...this._config, - [target.configValue!]: target.value, - }; + if (target.value === "") { + delete this._config[target.configValue!]; + } else { + this._config = { + ...this._config, + [target.configValue!]: target.value, + }; + } } fireEvent(this, "config-changed", { config: this._config }); } diff --git a/src/panels/lovelace/editor/config-elements/hui-markdown-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-markdown-card-editor.ts index b37d60da94..c4b002eff1 100644 --- a/src/panels/lovelace/editor/config-elements/hui-markdown-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-markdown-card-editor.ts @@ -77,10 +77,14 @@ export class HuiMarkdownCardEditor extends hassLocalizeLitMixin(LitElement) return; } if (target.configValue) { - this._config = { - ...this._config, - [target.configValue!]: target.value, - }; + if (target.value === "") { + delete this._config[target.configValue!]; + } else { + this._config = { + ...this._config, + [target.configValue!]: target.value, + }; + } } fireEvent(this, "config-changed", { config: this._config }); } diff --git a/src/panels/lovelace/editor/config-elements/hui-media-control-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-media-control-card-editor.ts index 04b13271e1..b6ac24fe3f 100644 --- a/src/panels/lovelace/editor/config-elements/hui-media-control-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-media-control-card-editor.ts @@ -62,10 +62,14 @@ export class HuiMediaControlCardEditor extends hassLocalizeLitMixin(LitElement) return; } if (target.configValue) { - this._config = { - ...this._config, - [target.configValue!]: target.value, - }; + if (target.value === "") { + delete this._config[target.configValue!]; + } else { + this._config = { + ...this._config, + [target.configValue!]: target.value, + }; + } } fireEvent(this, "config-changed", { config: this._config }); } diff --git a/src/panels/lovelace/editor/config-elements/hui-plant-status-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-plant-status-card-editor.ts index 037d557b2e..d321f52b76 100644 --- a/src/panels/lovelace/editor/config-elements/hui-plant-status-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-plant-status-card-editor.ts @@ -79,10 +79,14 @@ export class HuiPlantStatusCardEditor extends hassLocalizeLitMixin(LitElement) return; } if (target.configValue) { - this._config = { - ...this._config, - [target.configValue!]: target.value, - }; + if (target.value === "") { + delete this._config[target.configValue!]; + } else { + this._config = { + ...this._config, + [target.configValue!]: target.value, + }; + } } fireEvent(this, "config-changed", { config: this._config }); } diff --git a/src/panels/lovelace/editor/config-elements/hui-sensor-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-sensor-card-editor.ts index 28391d0473..56a6335319 100644 --- a/src/panels/lovelace/editor/config-elements/hui-sensor-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-sensor-card-editor.ts @@ -171,14 +171,18 @@ export class HuiSensorCardEditor extends hassLocalizeLitMixin(LitElement) return; } if (target.configValue) { - let value: any = target.value; - if (target.type === "number") { - value = Number(value); + if ( + target.value === "" || + (target.type === "number" && isNaN(Number(target.value))) + ) { + delete this._config[target.configValue!]; + } else { + let value: any = target.value; + if (target.type === "number") { + value = Number(value); + } + this._config = { ...this._config, [target.configValue!]: value }; } - this._config = { - ...this._config, - [target.configValue!]: value, - }; } fireEvent(this, "config-changed", { config: this._config }); } diff --git a/src/panels/lovelace/editor/config-elements/hui-shopping-list-editor.ts b/src/panels/lovelace/editor/config-elements/hui-shopping-list-editor.ts index ae56d6ab02..6bd987db6a 100644 --- a/src/panels/lovelace/editor/config-elements/hui-shopping-list-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-shopping-list-editor.ts @@ -60,10 +60,14 @@ export class HuiShoppingListEditor extends hassLocalizeLitMixin(LitElement) return; } if (target.configValue) { - this._config = { - ...this._config, - [target.configValue!]: target.value, - }; + if (target.value === "") { + delete this._config[target.configValue!]; + } else { + this._config = { + ...this._config, + [target.configValue!]: target.value, + }; + } } fireEvent(this, "config-changed", { config: this._config }); } diff --git a/src/panels/lovelace/editor/config-elements/hui-thermostat-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-thermostat-card-editor.ts index 9cc1033027..e1db48583e 100644 --- a/src/panels/lovelace/editor/config-elements/hui-thermostat-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-thermostat-card-editor.ts @@ -91,7 +91,11 @@ export class HuiThermostatCardEditor extends hassLocalizeLitMixin(LitElement) return; } if (target.configValue) { - this._config = { ...this._config, [target.configValue!]: target.value }; + if (target.value === "") { + delete this._config[target.configValue!]; + } else { + this._config = { ...this._config, [target.configValue!]: target.value }; + } } fireEvent(this, "config-changed", { config: this._config }); }