Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove empty electricity grid state #20794

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/data/energy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export interface WaterSourceTypeEnergyPreference {
unit_of_measurement?: string | null;
}

type EnergySource =
export type EnergySource =
| SolarSourceTypeEnergyPreference
| GridSourceTypeEnergyPreference
| BatterySourceTypeEnergyPreference
Expand Down
39 changes: 28 additions & 11 deletions src/panels/config/energy/components/ha-energy-grid-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
EnergyPreferencesValidation,
energySourcesByType,
EnergyValidationIssue,
EnergySource,
FlowFromGridSourceEnergyPreference,
FlowToGridSourceEnergyPreference,
GridSourceTypeEnergyPreference,
Expand Down Expand Up @@ -449,11 +450,8 @@ export class EnergyGridSettings extends LitElement {
),
};

try {
await this._savePreferences(preferences);
} catch (err: any) {
showAlertDialog(this, { title: `Failed to save config: ${err.message}` });
}
const cleanedPreferences = this._removeEmptySources(preferences);
await this._savePreferences(cleanedPreferences);
silamon marked this conversation as resolved.
Show resolved Hide resolved
}

private async _deleteToSource(ev) {
Expand All @@ -479,18 +477,37 @@ export class EnergyGridSettings extends LitElement {
),
};

const cleanedPreferences = this._removeEmptySources(preferences);
await this._savePreferences(cleanedPreferences);
}

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) {
try {
await this._savePreferences(preferences);
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}` });
silamon marked this conversation as resolved.
Show resolved Hide resolved
}
}

private async _savePreferences(preferences: EnergyPreferences) {
const result = await saveEnergyPreferences(this.hass, preferences);
fireEvent(this, "value-changed", { value: result });
}

static get styles(): CSSResultGroup {
return [haStyle, energyCardStyles];
}
Expand Down
Loading