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

View File

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

View File

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