From b2735924448937cbf26fbcfeb638a7f44ee62f0b Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Tue, 14 May 2024 09:40:42 +0000 Subject: [PATCH 1/3] Remove empty grid state --- .../components/ha-energy-grid-settings.ts | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/panels/config/energy/components/ha-energy-grid-settings.ts b/src/panels/config/energy/components/ha-energy-grid-settings.ts index eeb6234e9935..5de5def0ca42 100644 --- a/src/panels/config/energy/components/ha-energy-grid-settings.ts +++ b/src/panels/config/energy/components/ha-energy-grid-settings.ts @@ -449,11 +449,7 @@ export class EnergyGridSettings extends LitElement { ), }; - try { - await this._savePreferences(preferences); - } catch (err: any) { - showAlertDialog(this, { title: `Failed to save config: ${err.message}` }); - } + await this._deleteSource(preferences); } private async _deleteToSource(ev) { @@ -479,16 +475,34 @@ export class EnergyGridSettings extends LitElement { ), }; - try { - await this._savePreferences(preferences); - } catch (err: any) { - showAlertDialog(this, { title: `Failed to save config: ${err.message}` }); + await this._deleteSource(preferences); + } + + private async _deleteSource(preferences: EnergyPreferences) { + // Check if grid sources became an empty type and remove if so + const hasEmptyGridSources = preferences.energy_sources.some( + (source) => + source.type === "grid" && + source.flow_from.length === 0 && + source.flow_to.length === 0 + ); + + if (hasEmptyGridSources) { + preferences.energy_sources = preferences.energy_sources.filter( + (source) => source.type !== "grid" + ); } + + await this._savePreferences(preferences); } private async _savePreferences(preferences: EnergyPreferences) { - const result = await saveEnergyPreferences(this.hass, preferences); - fireEvent(this, "value-changed", { value: result }); + try { + const result = await saveEnergyPreferences(this.hass, preferences); + fireEvent(this, "value-changed", { value: result }); + } catch (err: any) { + showAlertDialog(this, { title: `Failed to save config: ${err.message}` }); + } } static get styles(): CSSResultGroup { From 7f5453644a86fe2fb5adb51581cb553fbb83056a Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Thu, 23 May 2024 18:47:17 +0000 Subject: [PATCH 2/3] Apply feedback --- .../energy/components/ha-energy-grid-settings.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/panels/config/energy/components/ha-energy-grid-settings.ts b/src/panels/config/energy/components/ha-energy-grid-settings.ts index 5de5def0ca42..037ba97add14 100644 --- a/src/panels/config/energy/components/ha-energy-grid-settings.ts +++ b/src/panels/config/energy/components/ha-energy-grid-settings.ts @@ -449,7 +449,8 @@ export class EnergyGridSettings extends LitElement { ), }; - await this._deleteSource(preferences); + const cleanedPreferences = this._removeEmptySources(preferences); + await this._savePreferences(cleanedPreferences); } private async _deleteToSource(ev) { @@ -475,10 +476,11 @@ export class EnergyGridSettings extends LitElement { ), }; - await this._deleteSource(preferences); + const cleanedPreferences = this._removeEmptySources(preferences); + await this._savePreferences(cleanedPreferences); } - private async _deleteSource(preferences: EnergyPreferences) { + private _removeEmptySources(preferences: EnergyPreferences) { // Check if grid sources became an empty type and remove if so const hasEmptyGridSources = preferences.energy_sources.some( (source) => @@ -493,7 +495,7 @@ export class EnergyGridSettings extends LitElement { ); } - await this._savePreferences(preferences); + return preferences; } private async _savePreferences(preferences: EnergyPreferences) { From 1218c7132db7730edc6b7968aba94cc8a892cf9c Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Thu, 23 May 2024 19:09:25 +0000 Subject: [PATCH 3/3] AI review --- src/data/energy.ts | 2 +- .../components/ha-energy-grid-settings.ts | 25 ++++++++++--------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/data/energy.ts b/src/data/energy.ts index 61d23aca0e53..c4d66110746e 100644 --- a/src/data/energy.ts +++ b/src/data/energy.ts @@ -173,7 +173,7 @@ export interface WaterSourceTypeEnergyPreference { unit_of_measurement?: string | null; } -type EnergySource = +export type EnergySource = | SolarSourceTypeEnergyPreference | GridSourceTypeEnergyPreference | BatterySourceTypeEnergyPreference diff --git a/src/panels/config/energy/components/ha-energy-grid-settings.ts b/src/panels/config/energy/components/ha-energy-grid-settings.ts index 037ba97add14..fd8dfa2c9e6b 100644 --- a/src/panels/config/energy/components/ha-energy-grid-settings.ts +++ b/src/panels/config/energy/components/ha-energy-grid-settings.ts @@ -22,6 +22,7 @@ import { EnergyPreferencesValidation, energySourcesByType, EnergyValidationIssue, + EnergySource, FlowFromGridSourceEnergyPreference, FlowToGridSourceEnergyPreference, GridSourceTypeEnergyPreference, @@ -482,18 +483,18 @@ export class EnergyGridSettings extends LitElement { private _removeEmptySources(preferences: EnergyPreferences) { // Check if grid sources became an empty type and remove if so - const hasEmptyGridSources = preferences.energy_sources.some( - (source) => - source.type === "grid" && - source.flow_from.length === 0 && - source.flow_to.length === 0 - ); - - if (hasEmptyGridSources) { - preferences.energy_sources = preferences.energy_sources.filter( - (source) => source.type !== "grid" - ); - } + preferences.energy_sources = preferences.energy_sources.reduce< + EnergySource[] + >((acc, source) => { + if ( + source.type !== "grid" || + source.flow_from.length > 0 || + source.flow_to.length > 0 + ) { + acc.push(source); + } + return acc; + }, []); return preferences; }