mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-18 06:46:35 +00:00
Remove empty electricity grid state (#20794)
* Remove empty grid state * Apply feedback * AI review
This commit is contained in:
parent
f617426808
commit
5d044a06eb
@ -173,7 +173,7 @@ export interface WaterSourceTypeEnergyPreference {
|
|||||||
unit_of_measurement?: string | null;
|
unit_of_measurement?: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
type EnergySource =
|
export type EnergySource =
|
||||||
| SolarSourceTypeEnergyPreference
|
| SolarSourceTypeEnergyPreference
|
||||||
| GridSourceTypeEnergyPreference
|
| GridSourceTypeEnergyPreference
|
||||||
| BatterySourceTypeEnergyPreference
|
| BatterySourceTypeEnergyPreference
|
||||||
|
@ -22,6 +22,7 @@ import {
|
|||||||
EnergyPreferencesValidation,
|
EnergyPreferencesValidation,
|
||||||
energySourcesByType,
|
energySourcesByType,
|
||||||
EnergyValidationIssue,
|
EnergyValidationIssue,
|
||||||
|
EnergySource,
|
||||||
FlowFromGridSourceEnergyPreference,
|
FlowFromGridSourceEnergyPreference,
|
||||||
FlowToGridSourceEnergyPreference,
|
FlowToGridSourceEnergyPreference,
|
||||||
GridSourceTypeEnergyPreference,
|
GridSourceTypeEnergyPreference,
|
||||||
@ -449,11 +450,8 @@ export class EnergyGridSettings extends LitElement {
|
|||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
const cleanedPreferences = this._removeEmptySources(preferences);
|
||||||
await this._savePreferences(preferences);
|
await this._savePreferences(cleanedPreferences);
|
||||||
} catch (err: any) {
|
|
||||||
showAlertDialog(this, { title: `Failed to save config: ${err.message}` });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _deleteToSource(ev) {
|
private async _deleteToSource(ev) {
|
||||||
@ -479,16 +477,35 @@ export class EnergyGridSettings extends LitElement {
|
|||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
const cleanedPreferences = this._removeEmptySources(preferences);
|
||||||
await this._savePreferences(preferences);
|
await this._savePreferences(cleanedPreferences);
|
||||||
} catch (err: any) {
|
}
|
||||||
showAlertDialog(this, { title: `Failed to save config: ${err.message}` });
|
|
||||||
}
|
private _removeEmptySources(preferences: EnergyPreferences) {
|
||||||
|
// Check if grid sources became an empty type and remove if so
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _savePreferences(preferences: EnergyPreferences) {
|
private async _savePreferences(preferences: EnergyPreferences) {
|
||||||
const result = await saveEnergyPreferences(this.hass, preferences);
|
try {
|
||||||
fireEvent(this, "value-changed", { value: result });
|
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 {
|
static get styles(): CSSResultGroup {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user