Skip to content

Commit 7563d33

Browse files
committed
Default to old energy view if no water and gas
1 parent c602eef commit 7563d33

File tree

4 files changed

+137
-108
lines changed

4 files changed

+137
-108
lines changed

src/panels/energy/ha-panel-energy.ts

Lines changed: 98 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import type { CSSResultGroup, PropertyValues, TemplateResult } from "lit";
1+
import type { CSSResultGroup, PropertyValues } from "lit";
22
import { LitElement, css, html, nothing } from "lit";
33
import { mdiPencil, mdiDownload } from "@mdi/js";
44
import { customElement, property, state } from "lit/decorators";
55
import "../../components/ha-menu-button";
66
import "../../components/ha-icon-button-arrow-prev";
77
import "../../components/ha-list-item";
88
import "../../components/ha-top-app-bar-fixed";
9+
import "../../components/ha-alert";
910
import type { LovelaceConfig } from "../../data/lovelace/config/types";
1011
import { haStyle } from "../../resources/styles";
1112
import type { HomeAssistant } from "../../types";
@@ -21,6 +22,7 @@ import type {
2122
GasSourceTypeEnergyPreference,
2223
WaterSourceTypeEnergyPreference,
2324
DeviceConsumptionEnergyPreference,
25+
EnergyCollection,
2426
} from "../../data/energy";
2527
import {
2628
computeConsumptionData,
@@ -47,6 +49,11 @@ const ENERGY_LOVELACE_CONFIG: LovelaceConfig = {
4749
},
4850
path: "electricity",
4951
},
52+
{
53+
type: "panel",
54+
path: "setup",
55+
cards: [{ type: "custom:energy-setup-wizard-card" }],
56+
},
5057
],
5158
};
5259

@@ -60,12 +67,26 @@ class PanelEnergy extends LitElement {
6067

6168
@state() private _searchParms = new URLSearchParams(window.location.search);
6269

70+
@state() private _error?: string;
71+
6372
@property({ attribute: false }) public route?: {
6473
path: string;
6574
prefix: string;
6675
};
6776

68-
public willUpdate(changedProps: PropertyValues) {
77+
private _energyCollection?: EnergyCollection;
78+
79+
get _viewPath(): string | undefined {
80+
const viewPath: string | undefined = this.route!.path.split("/")[1];
81+
return viewPath ? decodeURI(viewPath) : undefined;
82+
}
83+
84+
public connectedCallback() {
85+
super.connectedCallback();
86+
this._loadPrefs();
87+
}
88+
89+
public async willUpdate(changedProps: PropertyValues) {
6990
if (!this.hasUpdated) {
7091
this.hass.loadFragmentTranslation("lovelace");
7192
}
@@ -74,22 +95,66 @@ class PanelEnergy extends LitElement {
7495
}
7596
const oldHass = changedProps.get("hass") as this["hass"];
7697
if (oldHass?.locale !== this.hass.locale) {
77-
this._setLovelace();
78-
}
79-
if (oldHass && oldHass.localize !== this.hass.localize) {
98+
await this._setLovelace();
99+
} else if (oldHass && oldHass.localize !== this.hass.localize) {
80100
this._reloadView();
81101
}
82102
}
83103

104+
private async _loadPrefs() {
105+
if (this._viewPath === "setup") {
106+
await import("./cards/energy-setup-wizard-card");
107+
} else {
108+
this._energyCollection = getEnergyDataCollection(this.hass, {
109+
key: DEFAULT_ENERGY_COLLECTION_KEY,
110+
});
111+
try {
112+
// Have to manually refresh here as we don't want to subscribe yet
113+
await this._energyCollection.refresh();
114+
} catch (err: any) {
115+
if (err.code === "not_found") {
116+
navigate("/energy/setup");
117+
}
118+
this._error = err.message;
119+
return;
120+
}
121+
const prefs = this._energyCollection.prefs!;
122+
if (
123+
prefs.device_consumption.length === 0 &&
124+
prefs.energy_sources.length === 0
125+
) {
126+
// No energy sources available, start from scratch
127+
navigate("/energy/setup");
128+
}
129+
}
130+
}
131+
84132
private _back(ev) {
85133
ev.stopPropagation();
86134
goBack();
87135
}
88136

89-
protected render(): TemplateResult {
90-
let viewPath: string | undefined = this.route!.path.split("/")[1];
91-
viewPath = viewPath ? decodeURI(viewPath) : undefined;
92-
const viewIndex = Math.max(ENERGY_LOVELACE_CONFIG.views.findIndex((view) => view.path === viewPath), 0);
137+
protected render() {
138+
if (!this._energyCollection?.prefs) {
139+
// Still loading
140+
return html`<div class="centered">
141+
<ha-spinner size="large"></ha-spinner>
142+
</div>`;
143+
}
144+
let viewPath = this._viewPath;
145+
const { prefs } = this._energyCollection;
146+
if (
147+
prefs.energy_sources.every((source) =>
148+
["grid", "solar", "battery"].includes(source.type)
149+
)
150+
) {
151+
// if only electricity sources, show electricity view directly
152+
viewPath = "electricity";
153+
}
154+
const viewIndex = Math.max(
155+
ENERGY_LOVELACE_CONFIG.views.findIndex((view) => view.path === viewPath),
156+
0
157+
);
93158

94159
return html`
95160
<div class="header">
@@ -144,12 +209,21 @@ class PanelEnergy extends LitElement {
144209
.hass=${this.hass}
145210
@reload-energy-panel=${this._reloadView}
146211
>
147-
<hui-view
148-
.hass=${this.hass}
149-
.narrow=${this.narrow}
150-
.lovelace=${this._lovelace}
151-
.index=${viewIndex}
152-
></hui-view>
212+
${this._error
213+
? html`<div class="centered">
214+
<ha-alert alert-type="error">
215+
An error occurred while fetching your energy preferences:
216+
${this._error}
217+
</ha-alert>
218+
</div>`
219+
: this._lovelace
220+
? html`<hui-view
221+
.hass=${this.hass}
222+
.narrow=${this.narrow}
223+
.lovelace=${this._lovelace}
224+
.index=${viewIndex}
225+
></hui-view>`
226+
: nothing}
153227
</hui-view-container>
154228
`;
155229
}
@@ -177,9 +251,7 @@ class PanelEnergy extends LitElement {
177251

178252
private async _dumpCSV(ev) {
179253
ev.stopPropagation();
180-
const energyData = getEnergyDataCollection(this.hass, {
181-
key: DEFAULT_ENERGY_COLLECTION_KEY,
182-
});
254+
const energyData = this._energyCollection!;
183255

184256
if (!energyData.prefs || !energyData.state.stats) {
185257
return;
@@ -476,7 +548,7 @@ class PanelEnergy extends LitElement {
476548
}
477549

478550
private _reloadView() {
479-
// Force strategy to be re-run by make a copy of the view
551+
// Force strategy to be re-run by making a copy of the view
480552
const config = this._lovelace!.config;
481553
this._lovelace = {
482554
...this._lovelace!,
@@ -582,6 +654,13 @@ class PanelEnergy extends LitElement {
582654
flex: 1 1 100%;
583655
max-width: 100%;
584656
}
657+
.centered {
658+
width: 100%;
659+
height: 100%;
660+
display: flex;
661+
align-items: center;
662+
justify-content: center;
663+
}
585664
`,
586665
];
587666
}

src/panels/energy/strategies/energy-overview-view-strategy.ts

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,14 @@
11
import { ReactiveElement } from "lit";
22
import { customElement } from "lit/decorators";
3-
import type {
4-
EnergyPreferences,
5-
GridSourceTypeEnergyPreference,
6-
} from "../../../data/energy";
7-
import { getEnergyPreferences } from "../../../data/energy";
3+
import type { GridSourceTypeEnergyPreference } from "../../../data/energy";
4+
import { getEnergyDataCollection } from "../../../data/energy";
85
import type { HomeAssistant } from "../../../types";
96
import type { LovelaceViewConfig } from "../../../data/lovelace/config/view";
107
import type { LovelaceStrategyConfig } from "../../../data/lovelace/config/strategy";
118
import type { LovelaceSectionConfig } from "../../../data/lovelace/config/section";
129
import type { LovelaceCardConfig } from "../../../data/lovelace/config/card";
1310
import { DEFAULT_ENERGY_COLLECTION_KEY } from "../ha-panel-energy";
1411

15-
const setupWizard = async (): Promise<LovelaceViewConfig> => {
16-
await import("../cards/energy-setup-wizard-card");
17-
return {
18-
type: "panel",
19-
cards: [
20-
{
21-
type: "custom:energy-setup-wizard-card",
22-
},
23-
],
24-
};
25-
};
26-
27-
const COLUMNS = 2;
28-
2912
@customElement("energy-overview-view-strategy")
3013
export class EnergyViewStrategy extends ReactiveElement {
3114
static async generate(
@@ -34,34 +17,21 @@ export class EnergyViewStrategy extends ReactiveElement {
3417
): Promise<LovelaceViewConfig> {
3518
const view: LovelaceViewConfig = { type: "sections", sections: [] };
3619

37-
let prefs: EnergyPreferences;
3820
const collectionKey =
3921
_config.collection_key || DEFAULT_ENERGY_COLLECTION_KEY;
4022

41-
try {
42-
prefs = await getEnergyPreferences(hass);
43-
} catch (err: any) {
44-
if (err.code === "not_found") {
45-
return setupWizard();
46-
}
47-
view.sections!.push({
48-
column_span: COLUMNS,
49-
cards: [
50-
{
51-
type: "markdown",
52-
content: `An error occurred while fetching your energy preferences: ${err.message}.`,
53-
},
54-
],
55-
});
56-
return view;
57-
}
23+
const energyCollection = getEnergyDataCollection(hass, {
24+
key: collectionKey,
25+
});
26+
const prefs = energyCollection.prefs;
5827

5928
// No energy sources available, start from scratch
6029
if (
61-
prefs!.device_consumption.length === 0 &&
62-
prefs!.energy_sources.length === 0
30+
!prefs ||
31+
(prefs.device_consumption.length === 0 &&
32+
prefs.energy_sources.length === 0)
6333
) {
64-
return setupWizard();
34+
return view;
6535
}
6636

6737
const hasGrid = prefs.energy_sources.find(
@@ -156,56 +126,56 @@ export class EnergyViewStrategy extends ReactiveElement {
156126

157127
view.sections!.push(energySection);
158128

159-
if (hasGas) {
129+
if (hasGrid || hasSolar || hasBattery || hasGas || hasWater) {
160130
view.sections!.push({
161131
type: "grid",
162132
cards: [
163133
{
164134
type: "heading",
165-
heading: hass.localize("ui.panel.energy.summary_list.gas"),
135+
heading: hass.localize(
136+
"ui.panel.energy.cards.energy_sources_table_title"
137+
),
166138
},
167139
{
168-
title: hass.localize(
169-
"ui.panel.energy.cards.energy_gas_graph_title"
170-
),
171-
type: "energy-gas-graph",
140+
type: "energy-sources-table",
172141
collection_key: collectionKey,
173142
},
174143
],
175144
});
176145
}
177146

178-
if (hasWater) {
147+
if (hasGas) {
179148
view.sections!.push({
180149
type: "grid",
181150
cards: [
182151
{
183152
type: "heading",
184-
heading: hass.localize("ui.panel.energy.summary_list.water"),
153+
heading: hass.localize("ui.panel.energy.summary_list.gas"),
185154
},
186155
{
187156
title: hass.localize(
188-
"ui.panel.energy.cards.energy_water_graph_title"
157+
"ui.panel.energy.cards.energy_gas_graph_title"
189158
),
190-
type: "energy-water-graph",
159+
type: "energy-gas-graph",
191160
collection_key: collectionKey,
192161
},
193162
],
194163
});
195164
}
196165

197-
if (hasGrid || hasSolar || hasBattery || hasGas || hasWater) {
166+
if (hasWater) {
198167
view.sections!.push({
199168
type: "grid",
200169
cards: [
201170
{
202171
type: "heading",
203-
heading: hass.localize(
204-
"ui.panel.energy.cards.energy_sources_table_title"
205-
),
172+
heading: hass.localize("ui.panel.energy.summary_list.water"),
206173
},
207174
{
208-
type: "energy-sources-table",
175+
title: hass.localize(
176+
"ui.panel.energy.cards.energy_water_graph_title"
177+
),
178+
type: "energy-water-graph",
209179
collection_key: collectionKey,
210180
},
211181
],

0 commit comments

Comments
 (0)