Fix grid onboarding (#9621)

This commit is contained in:
Bram Kragten 2021-07-27 21:34:43 +02:00 committed by GitHub
parent 21a29ed3a5
commit 0c0091375c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 22 deletions

View File

@ -302,7 +302,7 @@ export const fetchStatistics = (
export const calculateStatisticSumGrowth = ( export const calculateStatisticSumGrowth = (
values: StatisticValue[] values: StatisticValue[]
): number | null => { ): number | null => {
if (values.length < 2) { if (!values || values.length < 2) {
return null; return null;
} }
const endSum = values[values.length - 1].sum; const endSum = values[values.length - 1].sum;
@ -324,7 +324,7 @@ export const calculateStatisticsSumGrowth = (
for (const stat of stats) { for (const stat of stats) {
if (!(stat in data)) { if (!(stat in data)) {
return null; continue;
} }
const statGrowth = calculateStatisticSumGrowth(data[stat]); const statGrowth = calculateStatisticSumGrowth(data[stat]);

View File

@ -24,6 +24,7 @@ import {
energySourcesByType, energySourcesByType,
FlowFromGridSourceEnergyPreference, FlowFromGridSourceEnergyPreference,
FlowToGridSourceEnergyPreference, FlowToGridSourceEnergyPreference,
GridSourceTypeEnergyPreference,
saveEnergyPreferences, saveEnergyPreferences,
} from "../../../../data/energy"; } from "../../../../data/energy";
import { showConfigFlowDialog } from "../../../../dialogs/config-flow/show-dialog-config-flow"; import { showConfigFlowDialog } from "../../../../dialogs/config-flow/show-dialog-config-flow";
@ -201,18 +202,33 @@ export class EnergyGridSettings extends LitElement {
private _addFromSource() { private _addFromSource() {
showEnergySettingsGridFlowFromDialog(this, { showEnergySettingsGridFlowFromDialog(this, {
currency: this.preferences.currency, currency: this.preferences.currency,
saveCallback: async (source) => { saveCallback: async (flow) => {
const flowFrom = energySourcesByType(this.preferences).grid![0] let preferences: EnergyPreferences;
.flow_from; const gridSource = this.preferences.energy_sources.find(
(src) => src.type === "grid"
) as GridSourceTypeEnergyPreference | undefined;
const preferences: EnergyPreferences = { if (!gridSource) {
...this.preferences, preferences = {
energy_sources: this.preferences.energy_sources.map((src) => ...this.preferences,
src.type === "grid" energy_sources: [
? { ...src, flow_from: [...flowFrom, source] } ...this.preferences.energy_sources,
: src {
), ...emptyGridSourceEnergyPreference(),
}; flow_from: [flow],
},
],
};
} else {
preferences = {
...this.preferences,
energy_sources: this.preferences.energy_sources.map((src) =>
src.type === "grid"
? { ...src, flow_from: [...gridSource.flow_from, flow] }
: src
),
};
}
await this._savePreferences(preferences); await this._savePreferences(preferences);
}, },
}); });
@ -221,15 +237,33 @@ export class EnergyGridSettings extends LitElement {
private _addToSource() { private _addToSource() {
showEnergySettingsGridFlowToDialog(this, { showEnergySettingsGridFlowToDialog(this, {
currency: this.preferences.currency, currency: this.preferences.currency,
saveCallback: async (source) => { saveCallback: async (flow) => {
const flowTo = energySourcesByType(this.preferences).grid![0].flow_to; let preferences: EnergyPreferences;
const gridSource = this.preferences.energy_sources.find(
(src) => src.type === "grid"
) as GridSourceTypeEnergyPreference | undefined;
const preferences: EnergyPreferences = { if (!gridSource) {
...this.preferences, preferences = {
energy_sources: this.preferences.energy_sources.map((src) => ...this.preferences,
src.type === "grid" ? { ...src, flow_to: [...flowTo, source] } : src energy_sources: [
), ...this.preferences.energy_sources,
}; {
...emptyGridSourceEnergyPreference(),
flow_to: [flow],
},
],
};
} else {
preferences = {
...this.preferences,
energy_sources: this.preferences.energy_sources.map((src) =>
src.type === "grid"
? { ...src, flow_to: [...gridSource.flow_to, flow] }
: src
),
};
}
await this._savePreferences(preferences); await this._savePreferences(preferences);
}, },
}); });

View File

@ -20,7 +20,7 @@ export class EnergySetupWizard extends LitElement implements LovelaceCard {
@state() private _step = 0; @state() private _step = 0;
private _preferences: EnergyPreferences = { @state() private _preferences: EnergyPreferences = {
currency: "€", currency: "€",
energy_sources: [], energy_sources: [],
device_consumption: [], device_consumption: [],