Skip to content

Commit c602eef

Browse files
committed
Refactor energy panel strategies to use DEFAULT_ENERGY_COLLECTION_KEY and remove gas/water strategies
1 parent 6836a81 commit c602eef

File tree

7 files changed

+87
-217
lines changed

7 files changed

+87
-217
lines changed

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,23 @@ import {
3030
import { fileDownload } from "../../util/file_download";
3131
import type { StatisticValue } from "../../data/recorder";
3232

33+
export const DEFAULT_ENERGY_COLLECTION_KEY = "energy_dashboard";
34+
3335
const ENERGY_LOVELACE_CONFIG: LovelaceConfig = {
3436
views: [
3537
{
3638
strategy: {
3739
type: "energy-overview",
40+
collection_key: DEFAULT_ENERGY_COLLECTION_KEY,
3841
},
3942
},
4043
{
4144
strategy: {
4245
type: "energy",
46+
collection_key: DEFAULT_ENERGY_COLLECTION_KEY,
4347
},
4448
path: "electricity",
4549
},
46-
{
47-
strategy: {
48-
type: "water",
49-
},
50-
path: "water",
51-
},
52-
{
53-
strategy: {
54-
type: "gas",
55-
},
56-
path: "gas",
57-
},
5850
],
5951
};
6052

@@ -124,7 +116,7 @@ class PanelEnergy extends LitElement {
124116
125117
<hui-energy-period-selector
126118
.hass=${this.hass}
127-
collection-key="energy_dashboard"
119+
.collectionKey=${DEFAULT_ENERGY_COLLECTION_KEY}
128120
>
129121
${this.hass.user?.is_admin
130122
? html` <ha-list-item
@@ -186,7 +178,7 @@ class PanelEnergy extends LitElement {
186178
private async _dumpCSV(ev) {
187179
ev.stopPropagation();
188180
const energyData = getEnergyDataCollection(this.hass, {
189-
key: "energy_dashboard",
181+
key: DEFAULT_ENERGY_COLLECTION_KEY,
190182
});
191183

192184
if (!energyData.prefs || !energyData.state.stats) {

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

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { LovelaceViewConfig } from "../../../data/lovelace/config/view";
1010
import type { LovelaceStrategyConfig } from "../../../data/lovelace/config/strategy";
1111
import type { LovelaceSectionConfig } from "../../../data/lovelace/config/section";
1212
import type { LovelaceCardConfig } from "../../../data/lovelace/config/card";
13+
import { DEFAULT_ENERGY_COLLECTION_KEY } from "../ha-panel-energy";
1314

1415
const setupWizard = async (): Promise<LovelaceViewConfig> => {
1516
await import("../cards/energy-setup-wizard-card");
@@ -34,6 +35,8 @@ export class EnergyViewStrategy extends ReactiveElement {
3435
const view: LovelaceViewConfig = { type: "sections", sections: [] };
3536

3637
let prefs: EnergyPreferences;
38+
const collectionKey =
39+
_config.collection_key || DEFAULT_ENERGY_COLLECTION_KEY;
3740

3841
try {
3942
prefs = await getEnergyPreferences(hass);
@@ -80,11 +83,10 @@ export class EnergyViewStrategy extends ReactiveElement {
8083

8184
const energySection: LovelaceSectionConfig = {
8285
type: "grid",
83-
column_span: COLUMNS,
8486
cards: [
8587
{
8688
type: "heading",
87-
heading: hass.localize("ui.panel.energy.electricity_overview_title"),
89+
heading: hass.localize("ui.panel.energy.summary_list.energy"),
8890
tap_action: {
8991
action: "navigate",
9092
navigation_path: "/energy/electricity",
@@ -98,48 +100,52 @@ export class EnergyViewStrategy extends ReactiveElement {
98100
title: hass.localize("ui.panel.energy.cards.energy_distribution_title"),
99101
type: "energy-distribution",
100102
view_layout: { position: "sidebar" },
101-
collection_key: "energy_dashboard",
103+
collection_key: collectionKey,
102104
});
103105
}
104106

105-
const gauges: LovelaceCardConfig[] = [];
106-
// Only include if we have a grid source & return.
107-
if (hasReturn) {
108-
gauges.push({
109-
type: "energy-grid-neutrality-gauge",
110-
view_layout: { position: "sidebar" },
111-
collection_key: "energy_dashboard",
107+
if (prefs!.device_consumption.length > 0) {
108+
energySection.cards!.push({
109+
title: hass.localize(
110+
"ui.panel.energy.cards.energy_top_consumers_title"
111+
),
112+
type: "energy-devices-graph",
113+
collection_key: collectionKey,
114+
max_devices: 5,
112115
});
113-
}
116+
} else if (hasGrid) {
117+
const gauges: LovelaceCardConfig[] = [];
118+
// Only include if we have a grid source & return.
119+
if (hasReturn) {
120+
gauges.push({
121+
type: "energy-grid-neutrality-gauge",
122+
view_layout: { position: "sidebar" },
123+
collection_key: collectionKey,
124+
});
125+
}
114126

115-
// Only include if we have a grid
116-
if (hasGrid) {
117127
gauges.push({
118128
type: "energy-carbon-consumed-gauge",
119129
view_layout: { position: "sidebar" },
120-
collection_key: "energy_dashboard",
130+
collection_key: collectionKey,
121131
});
122-
}
123132

124-
// Only include if we have a solar source.
125-
if (hasSolar) {
126-
if (hasReturn) {
127-
gauges.push({
128-
type: "energy-solar-consumed-gauge",
129-
view_layout: { position: "sidebar" },
130-
collection_key: "energy_dashboard",
131-
});
132-
}
133-
if (hasGrid) {
133+
// Only include if we have a solar source.
134+
if (hasSolar) {
135+
if (hasReturn) {
136+
gauges.push({
137+
type: "energy-solar-consumed-gauge",
138+
view_layout: { position: "sidebar" },
139+
collection_key: collectionKey,
140+
});
141+
}
134142
gauges.push({
135143
type: "energy-self-sufficiency-gauge",
136144
view_layout: { position: "sidebar" },
137-
collection_key: "energy_dashboard",
145+
collection_key: collectionKey,
138146
});
139147
}
140-
}
141148

142-
if (gauges.length) {
143149
energySection.cards!.push({
144150
type: "grid",
145151
columns: 2,
@@ -153,18 +159,55 @@ export class EnergyViewStrategy extends ReactiveElement {
153159
if (hasGas) {
154160
view.sections!.push({
155161
type: "grid",
156-
column_span: 1,
157162
cards: [
158163
{
159164
type: "heading",
160-
heading: hass.localize("ui.panel.energy.gas_overview_title"),
161-
tap_action: { action: "navigate", navigation_path: "/energy/gas" },
165+
heading: hass.localize("ui.panel.energy.summary_list.gas"),
162166
},
163167
{
164-
title: hass.localize("ui.panel.energy.cards.energy_gas_graph_title"),
168+
title: hass.localize(
169+
"ui.panel.energy.cards.energy_gas_graph_title"
170+
),
165171
type: "energy-gas-graph",
166-
collection_key: "energy_dashboard",
167-
}
172+
collection_key: collectionKey,
173+
},
174+
],
175+
});
176+
}
177+
178+
if (hasWater) {
179+
view.sections!.push({
180+
type: "grid",
181+
cards: [
182+
{
183+
type: "heading",
184+
heading: hass.localize("ui.panel.energy.summary_list.water"),
185+
},
186+
{
187+
title: hass.localize(
188+
"ui.panel.energy.cards.energy_water_graph_title"
189+
),
190+
type: "energy-water-graph",
191+
collection_key: collectionKey,
192+
},
193+
],
194+
});
195+
}
196+
197+
if (hasGrid || hasSolar || hasBattery || hasGas || hasWater) {
198+
view.sections!.push({
199+
type: "grid",
200+
cards: [
201+
{
202+
type: "heading",
203+
heading: hass.localize(
204+
"ui.panel.energy.cards.energy_sources_table_title"
205+
),
206+
},
207+
{
208+
type: "energy-sources-table",
209+
collection_key: collectionKey,
210+
},
168211
],
169212
});
170213
}

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

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,9 @@ export class EnergyViewStrategy extends ReactiveElement {
6363
const hasSolar = prefs.energy_sources.some(
6464
(source) => source.type === "solar"
6565
);
66-
const hasGas = prefs.energy_sources.some((source) => source.type === "gas");
6766
const hasBattery = prefs.energy_sources.some(
6867
(source) => source.type === "battery"
6968
);
70-
const hasWater = prefs.energy_sources.some(
71-
(source) => source.type === "water"
72-
);
7369

7470
view.cards!.push({
7571
type: "energy-compare",
@@ -94,24 +90,6 @@ export class EnergyViewStrategy extends ReactiveElement {
9490
});
9591
}
9692

97-
// Only include if we have a gas source.
98-
if (hasGas) {
99-
view.cards!.push({
100-
title: hass.localize("ui.panel.energy.cards.energy_gas_graph_title"),
101-
type: "energy-gas-graph",
102-
collection_key: "energy_dashboard",
103-
});
104-
}
105-
106-
// Only include if we have a water source.
107-
if (hasWater) {
108-
view.cards!.push({
109-
title: hass.localize("ui.panel.energy.cards.energy_water_graph_title"),
110-
type: "energy-water-graph",
111-
collection_key: "energy_dashboard",
112-
});
113-
}
114-
11593
// Only include if we have a grid or battery.
11694
if (hasGrid || hasBattery) {
11795
view.cards!.push({
@@ -122,13 +100,14 @@ export class EnergyViewStrategy extends ReactiveElement {
122100
});
123101
}
124102

125-
if (hasGrid || hasSolar || hasGas || hasWater || hasBattery) {
103+
if (hasGrid || hasSolar || hasBattery) {
126104
view.cards!.push({
127105
title: hass.localize(
128106
"ui.panel.energy.cards.energy_sources_table_title"
129107
),
130108
type: "energy-sources-table",
131109
collection_key: "energy_dashboard",
110+
types: ["grid", "solar", "battery"],
132111
});
133112
}
134113

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

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)