mirror of
https://github.com/home-assistant/frontend.git
synced 2025-04-26 14:27:20 +00:00
Fix grid onboarding (#9621)
This commit is contained in:
parent
21a29ed3a5
commit
0c0091375c
@ -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]);
|
||||
|
||||
|
@ -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);
|
||||
},
|
||||
});
|
||||
|
@ -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: [],
|
||||
|
Loading…
x
Reference in New Issue
Block a user