From 9e31b2bb2922fb52f9a5759f9aaf8723d859142a Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Thu, 10 Aug 2023 01:59:07 -0700 Subject: [PATCH] Filter duplicate entries in energy solar/battery/gas/water/devices (#17538) --- .../components/ha-energy-battery-settings.ts | 6 ++++++ .../components/ha-energy-device-settings.ts | 2 ++ .../components/ha-energy-gas-settings.ts | 6 ++++++ .../components/ha-energy-solar-settings.ts | 6 ++++++ .../components/ha-energy-water-settings.ts | 6 ++++++ .../dialogs/dialog-energy-battery-settings.ts | 21 +++++++++++++++++++ .../dialogs/dialog-energy-device-settings.ts | 7 +++++++ .../dialogs/dialog-energy-gas-settings.ts | 7 +++++++ .../dialog-energy-grid-flow-settings.ts | 1 + .../dialogs/dialog-energy-solar-settings.ts | 7 +++++++ .../dialogs/dialog-energy-water-settings.ts | 7 +++++++ .../energy/dialogs/show-dialogs-energy.ts | 5 +++++ 12 files changed, 81 insertions(+) diff --git a/src/panels/config/energy/components/ha-energy-battery-settings.ts b/src/panels/config/energy/components/ha-energy-battery-settings.ts index 9d27716a8a..a33d9cd34c 100644 --- a/src/panels/config/energy/components/ha-energy-battery-settings.ts +++ b/src/panels/config/energy/components/ha-energy-battery-settings.ts @@ -146,6 +146,9 @@ export class EnergyBatterySettings extends LitElement { private _addSource() { showEnergySettingsBatteryDialog(this, { + battery_sources: this.preferences.energy_sources.filter( + (src) => src.type === "battery" + ) as BatterySourceTypeEnergyPreference[], saveCallback: async (source) => { await this._savePreferences({ ...this.preferences, @@ -160,6 +163,9 @@ export class EnergyBatterySettings extends LitElement { ev.currentTarget.closest(".row").source; showEnergySettingsBatteryDialog(this, { source: { ...origSource }, + battery_sources: this.preferences.energy_sources.filter( + (src) => src.type === "battery" + ) as BatterySourceTypeEnergyPreference[], saveCallback: async (newSource) => { await this._savePreferences({ ...this.preferences, diff --git a/src/panels/config/energy/components/ha-energy-device-settings.ts b/src/panels/config/energy/components/ha-energy-device-settings.ts index dc2df5472f..8d132b05fd 100644 --- a/src/panels/config/energy/components/ha-energy-device-settings.ts +++ b/src/panels/config/energy/components/ha-energy-device-settings.ts @@ -116,6 +116,8 @@ export class EnergyDeviceSettings extends LitElement { private _addDevice() { showEnergySettingsDeviceDialog(this, { + device_consumptions: this.preferences + .device_consumption as DeviceConsumptionEnergyPreference[], saveCallback: async (device) => { await this._savePreferences({ ...this.preferences, diff --git a/src/panels/config/energy/components/ha-energy-gas-settings.ts b/src/panels/config/energy/components/ha-energy-gas-settings.ts index 135d686ebc..28fb946eaf 100644 --- a/src/panels/config/energy/components/ha-energy-gas-settings.ts +++ b/src/panels/config/energy/components/ha-energy-gas-settings.ts @@ -136,6 +136,9 @@ export class EnergyGasSettings extends LitElement { this.preferences, this.statsMetadata ), + gas_sources: this.preferences.energy_sources.filter( + (src) => src.type === "gas" + ) as GasSourceTypeEnergyPreference[], saveCallback: async (source) => { delete source.unit_of_measurement; await this._savePreferences({ @@ -157,6 +160,9 @@ export class EnergyGasSettings extends LitElement { origSource.stat_energy_from ), metadata: this.statsMetadata?.[origSource.stat_energy_from], + gas_sources: this.preferences.energy_sources.filter( + (src) => src.type === "gas" + ) as GasSourceTypeEnergyPreference[], saveCallback: async (newSource) => { await this._savePreferences({ ...this.preferences, diff --git a/src/panels/config/energy/components/ha-energy-solar-settings.ts b/src/panels/config/energy/components/ha-energy-solar-settings.ts index ccc7e492e7..4c03fba645 100644 --- a/src/panels/config/energy/components/ha-energy-solar-settings.ts +++ b/src/panels/config/energy/components/ha-energy-solar-settings.ts @@ -149,6 +149,9 @@ export class EnergySolarSettings extends LitElement { private _addSource() { showEnergySettingsSolarDialog(this, { info: this.info!, + solar_sources: this.preferences.energy_sources.filter( + (src) => src.type === "solar" + ) as SolarSourceTypeEnergyPreference[], saveCallback: async (source) => { await this._savePreferences({ ...this.preferences, @@ -164,6 +167,9 @@ export class EnergySolarSettings extends LitElement { showEnergySettingsSolarDialog(this, { info: this.info!, source: { ...origSource }, + solar_sources: this.preferences.energy_sources.filter( + (src) => src.type === "solar" + ) as SolarSourceTypeEnergyPreference[], saveCallback: async (newSource) => { await this._savePreferences({ ...this.preferences, diff --git a/src/panels/config/energy/components/ha-energy-water-settings.ts b/src/panels/config/energy/components/ha-energy-water-settings.ts index 278b3f747e..4cba61de2e 100644 --- a/src/panels/config/energy/components/ha-energy-water-settings.ts +++ b/src/panels/config/energy/components/ha-energy-water-settings.ts @@ -135,6 +135,9 @@ export class EnergyWaterSettings extends LitElement { private _addSource() { showEnergySettingsWaterDialog(this, { + water_sources: this.preferences.energy_sources.filter( + (src) => src.type === "water" + ) as WaterSourceTypeEnergyPreference[], saveCallback: async (source) => { delete source.unit_of_measurement; await this._savePreferences({ @@ -151,6 +154,9 @@ export class EnergyWaterSettings extends LitElement { showEnergySettingsWaterDialog(this, { source: { ...origSource }, metadata: this.statsMetadata?.[origSource.stat_energy_from], + water_sources: this.preferences.energy_sources.filter( + (src) => src.type === "water" + ) as WaterSourceTypeEnergyPreference[], saveCallback: async (newSource) => { await this._savePreferences({ ...this.preferences, diff --git a/src/panels/config/energy/dialogs/dialog-energy-battery-settings.ts b/src/panels/config/energy/dialogs/dialog-energy-battery-settings.ts index 0c76f4f78b..daa7af0b68 100644 --- a/src/panels/config/energy/dialogs/dialog-energy-battery-settings.ts +++ b/src/panels/config/energy/dialogs/dialog-energy-battery-settings.ts @@ -32,6 +32,8 @@ export class DialogEnergyBatterySettings @state() private _error?: string; + private _excludeList?: string[]; + public async showDialog( params: EnergySettingsBatteryDialogParams ): Promise { @@ -42,12 +44,23 @@ export class DialogEnergyBatterySettings this._energy_units = ( await getSensorDeviceClassConvertibleUnits(this.hass, "energy") ).units; + const allSources: string[] = []; + this._params.battery_sources.forEach((entry) => { + allSources.push(entry.stat_energy_from); + allSources.push(entry.stat_energy_to); + }); + this._excludeList = allSources.filter( + (id) => + id !== this._source?.stat_energy_from && + id !== this._source?.stat_energy_to + ); } public closeDialog(): void { this._params = undefined; this._source = undefined; this._error = undefined; + this._excludeList = undefined; fireEvent(this, "dialog-closed", { dialog: this.localName }); } @@ -85,6 +98,10 @@ export class DialogEnergyBatterySettings .label=${this.hass.localize( "ui.panel.config.energy.battery.dialog.energy_into_battery" )} + .excludeStatistics=${[ + ...(this._excludeList || []), + this._source.stat_energy_from, + ]} @value-changed=${this._statisticToChanged} dialogInitialFocus > @@ -96,6 +113,10 @@ export class DialogEnergyBatterySettings .label=${this.hass.localize( "ui.panel.config.energy.battery.dialog.energy_out_of_battery" )} + .excludeStatistics=${[ + ...(this._excludeList || []), + this._source.stat_energy_to, + ]} @value-changed=${this._statisticFromChanged} > diff --git a/src/panels/config/energy/dialogs/dialog-energy-device-settings.ts b/src/panels/config/energy/dialogs/dialog-energy-device-settings.ts index a9e6c86445..34cd014d87 100644 --- a/src/panels/config/energy/dialogs/dialog-energy-device-settings.ts +++ b/src/panels/config/energy/dialogs/dialog-energy-device-settings.ts @@ -32,6 +32,8 @@ export class DialogEnergyDeviceSettings @state() private _error?: string; + private _excludeList?: string[]; + public async showDialog( params: EnergySettingsDeviceDialogParams ): Promise { @@ -39,12 +41,16 @@ export class DialogEnergyDeviceSettings this._energy_units = ( await getSensorDeviceClassConvertibleUnits(this.hass, "energy") ).units; + this._excludeList = this._params.device_consumptions.map( + (entry) => entry.stat_consumption + ); } public closeDialog(): void { this._params = undefined; this._device = undefined; this._error = undefined; + this._excludeList = undefined; fireEvent(this, "dialog-closed", { dialog: this.localName }); } @@ -81,6 +87,7 @@ export class DialogEnergyDeviceSettings .label=${this.hass.localize( "ui.panel.config.energy.device_consumption.dialog.device_consumption_energy" )} + .excludeStatistics=${this._excludeList} @value-changed=${this._statisticChanged} dialogInitialFocus > diff --git a/src/panels/config/energy/dialogs/dialog-energy-gas-settings.ts b/src/panels/config/energy/dialogs/dialog-energy-gas-settings.ts index 75faae59fe..e0ccbd17a1 100644 --- a/src/panels/config/energy/dialogs/dialog-energy-gas-settings.ts +++ b/src/panels/config/energy/dialogs/dialog-energy-gas-settings.ts @@ -49,6 +49,8 @@ export class DialogEnergyGasSettings @state() private _error?: string; + private _excludeList?: string[]; + public async showDialog( params: EnergySettingsGasDialogParams ): Promise { @@ -74,6 +76,9 @@ export class DialogEnergyGasSettings this._gas_units = ( await getSensorDeviceClassConvertibleUnits(this.hass, "gas") ).units; + this._excludeList = this._params.gas_sources + .map((entry) => entry.stat_energy_from) + .filter((id) => id !== this._source?.stat_energy_from); } public closeDialog(): void { @@ -81,6 +86,7 @@ export class DialogEnergyGasSettings this._source = undefined; this._pickedDisplayUnit = undefined; this._error = undefined; + this._excludeList = undefined; fireEvent(this, "dialog-closed", { dialog: this.localName }); } @@ -139,6 +145,7 @@ export class DialogEnergyGasSettings .label=${this.hass.localize( "ui.panel.config.energy.gas.dialog.gas_usage" )} + .excludeStatistics=${this._excludeList} @value-changed=${this._statisticChanged} dialogInitialFocus > diff --git a/src/panels/config/energy/dialogs/dialog-energy-grid-flow-settings.ts b/src/panels/config/energy/dialogs/dialog-energy-grid-flow-settings.ts index fece7e15ae..f0683d474a 100644 --- a/src/panels/config/energy/dialogs/dialog-energy-grid-flow-settings.ts +++ b/src/panels/config/energy/dialogs/dialog-energy-grid-flow-settings.ts @@ -101,6 +101,7 @@ export class DialogEnergyGridFlowSettings this._source = undefined; this._pickedDisplayUnit = undefined; this._error = undefined; + this._excludeList = undefined; fireEvent(this, "dialog-closed", { dialog: this.localName }); } diff --git a/src/panels/config/energy/dialogs/dialog-energy-solar-settings.ts b/src/panels/config/energy/dialogs/dialog-energy-solar-settings.ts index dd05a30612..68327d6614 100644 --- a/src/panels/config/energy/dialogs/dialog-energy-solar-settings.ts +++ b/src/panels/config/energy/dialogs/dialog-energy-solar-settings.ts @@ -44,6 +44,8 @@ export class DialogEnergySolarSettings @state() private _error?: string; + private _excludeList?: string[]; + public async showDialog( params: EnergySettingsSolarDialogParams ): Promise { @@ -56,12 +58,16 @@ export class DialogEnergySolarSettings this._energy_units = ( await getSensorDeviceClassConvertibleUnits(this.hass, "energy") ).units; + this._excludeList = this._params.solar_sources + .map((entry) => entry.stat_energy_from) + .filter((id) => id !== this._source?.stat_energy_from); } public closeDialog(): void { this._params = undefined; this._source = undefined; this._error = undefined; + this._excludeList = undefined; fireEvent(this, "dialog-closed", { dialog: this.localName }); } @@ -97,6 +103,7 @@ export class DialogEnergySolarSettings .label=${this.hass.localize( "ui.panel.config.energy.solar.dialog.solar_production_energy" )} + .excludeStatistics=${this._excludeList} @value-changed=${this._statisticChanged} dialogInitialFocus > diff --git a/src/panels/config/energy/dialogs/dialog-energy-water-settings.ts b/src/panels/config/energy/dialogs/dialog-energy-water-settings.ts index c2f6b2718e..15ab3f8328 100644 --- a/src/panels/config/energy/dialogs/dialog-energy-water-settings.ts +++ b/src/panels/config/energy/dialogs/dialog-energy-water-settings.ts @@ -44,6 +44,8 @@ export class DialogEnergyWaterSettings @state() private _error?: string; + private _excludeList?: string[]; + public async showDialog( params: EnergySettingsWaterDialogParams ): Promise { @@ -66,6 +68,9 @@ export class DialogEnergyWaterSettings this._water_units = ( await getSensorDeviceClassConvertibleUnits(this.hass, "water") ).units; + this._excludeList = this._params.water_sources + .map((entry) => entry.stat_energy_from) + .filter((id) => id !== this._source?.stat_energy_from); } public closeDialog(): void { @@ -73,6 +78,7 @@ export class DialogEnergyWaterSettings this._source = undefined; this._error = undefined; this._pickedDisplayUnit = undefined; + this._excludeList = undefined; fireEvent(this, "dialog-closed", { dialog: this.localName }); } @@ -128,6 +134,7 @@ export class DialogEnergyWaterSettings .label=${this.hass.localize( "ui.panel.config.energy.water.dialog.water_usage" )} + .excludeStatistics=${this._excludeList} @value-changed=${this._statisticChanged} dialogInitialFocus > diff --git a/src/panels/config/energy/dialogs/show-dialogs-energy.ts b/src/panels/config/energy/dialogs/show-dialogs-energy.ts index ac04b03087..997999a69c 100644 --- a/src/panels/config/energy/dialogs/show-dialogs-energy.ts +++ b/src/panels/config/energy/dialogs/show-dialogs-energy.ts @@ -44,11 +44,13 @@ export interface EnergySettingsGridFlowToDialogParams { export interface EnergySettingsSolarDialogParams { info: EnergyInfo; source?: SolarSourceTypeEnergyPreference; + solar_sources: SolarSourceTypeEnergyPreference[]; saveCallback: (source: SolarSourceTypeEnergyPreference) => Promise; } export interface EnergySettingsBatteryDialogParams { source?: BatterySourceTypeEnergyPreference; + battery_sources: BatterySourceTypeEnergyPreference[]; saveCallback: (source: BatterySourceTypeEnergyPreference) => Promise; } @@ -56,16 +58,19 @@ export interface EnergySettingsGasDialogParams { source?: GasSourceTypeEnergyPreference; allowedGasUnitClass?: EnergyGasUnitClass; metadata?: StatisticsMetaData; + gas_sources: GasSourceTypeEnergyPreference[]; saveCallback: (source: GasSourceTypeEnergyPreference) => Promise; } export interface EnergySettingsWaterDialogParams { source?: WaterSourceTypeEnergyPreference; metadata?: StatisticsMetaData; + water_sources: WaterSourceTypeEnergyPreference[]; saveCallback: (source: WaterSourceTypeEnergyPreference) => Promise; } export interface EnergySettingsDeviceDialogParams { + device_consumptions: DeviceConsumptionEnergyPreference[]; saveCallback: (device: DeviceConsumptionEnergyPreference) => Promise; }