1+ import { mdiDownload , mdiPencil } from "@mdi/js" ;
12import type { CSSResultGroup , PropertyValues } from "lit" ;
23import { LitElement , css , html , nothing } from "lit" ;
3- import { mdiPencil , mdiDownload } from "@mdi/js" ;
44import { customElement , property , state } from "lit/decorators" ;
5- import "../../components/ha-menu-button" ;
5+ import { goBack , navigate } from "../../common/navigate" ;
6+ import "../../components/ha-alert" ;
67import "../../components/ha-icon-button-arrow-prev" ;
78import "../../components/ha-list-item" ;
9+ import "../../components/ha-menu-button" ;
810import "../../components/ha-top-app-bar-fixed" ;
9- import "../../components/ha-alert" ;
10- import type { LovelaceConfig } from "../../data/lovelace/config/types" ;
11- import { haStyle } from "../../resources/styles" ;
12- import type { HomeAssistant } from "../../types" ;
13- import "../lovelace/components/hui-energy-period-selector" ;
14- import type { Lovelace } from "../lovelace/types" ;
15- import "../lovelace/views/hui-view" ;
16- import "../lovelace/views/hui-view-container" ;
17- import { goBack , navigate } from "../../common/navigate" ;
1811import type {
19- GridSourceTypeEnergyPreference ,
20- SolarSourceTypeEnergyPreference ,
2112 BatterySourceTypeEnergyPreference ,
13+ DeviceConsumptionEnergyPreference ,
14+ EnergyPreferences ,
2215 GasSourceTypeEnergyPreference ,
16+ GridSourceTypeEnergyPreference ,
17+ SolarSourceTypeEnergyPreference ,
2318 WaterSourceTypeEnergyPreference ,
24- DeviceConsumptionEnergyPreference ,
25- EnergyCollection ,
2619} from "../../data/energy" ;
2720import {
2821 computeConsumptionData ,
2922 getEnergyDataCollection ,
3023 getSummedData ,
3124} from "../../data/energy" ;
32- import { fileDownload } from "../../util/file_download" ;
25+ import type { LovelaceConfig } from "../../data/lovelace/config/types" ;
26+ import type { LovelaceViewConfig } from "../../data/lovelace/config/view" ;
3327import type { StatisticValue } from "../../data/recorder" ;
28+ import { haStyle } from "../../resources/styles" ;
29+ import type { HomeAssistant } from "../../types" ;
30+ import { fileDownload } from "../../util/file_download" ;
31+ import "../lovelace/components/hui-energy-period-selector" ;
32+ import type { Lovelace } from "../lovelace/types" ;
33+ import "../lovelace/views/hui-view" ;
34+ import "../lovelace/views/hui-view-container" ;
3435
3536export const DEFAULT_ENERGY_COLLECTION_KEY = "energy_dashboard" ;
3637
37- const ENERGY_LOVELACE_CONFIG : LovelaceConfig = {
38- views : [
39- {
40- strategy : {
41- type : "energy-overview" ,
42- collection_key : DEFAULT_ENERGY_COLLECTION_KEY ,
43- } ,
44- } ,
45- {
46- strategy : {
47- type : "energy-electricity" ,
48- collection_key : DEFAULT_ENERGY_COLLECTION_KEY ,
49- } ,
50- path : "electricity" ,
51- } ,
52- {
53- type : "panel" ,
54- path : "setup" ,
55- cards : [ { type : "custom:energy-setup-wizard-card" } ] ,
56- } ,
57- ] ,
38+ const OVERVIEW_VIEW = {
39+ strategy : {
40+ type : "energy-overview" ,
41+ collection_key : DEFAULT_ENERGY_COLLECTION_KEY ,
42+ } ,
43+ } ;
44+
45+ const ELECTRICITY_VIEW = {
46+ back_path : "/energy" ,
47+ path : "electricity" ,
48+ strategy : {
49+ type : "energy-electricity" ,
50+ collection_key : DEFAULT_ENERGY_COLLECTION_KEY ,
51+ } ,
52+ } as LovelaceViewConfig ;
53+
54+ const WIZARD_VIEW = {
55+ type : "panel" ,
56+ path : "setup" ,
57+ cards : [ { type : "custom:energy-setup-wizard-card" } ] ,
5858} ;
5959
6060@customElement ( "ha-panel-energy" )
@@ -74,7 +74,8 @@ class PanelEnergy extends LitElement {
7474 prefix : string ;
7575 } ;
7676
77- private _energyCollection ?: EnergyCollection ;
77+ @state ( )
78+ private _config ?: LovelaceConfig ;
7879
7980 get _viewPath ( ) : string | undefined {
8081 const viewPath : string | undefined = this . route ! . path . split ( "/" ) [ 1 ] ;
@@ -83,7 +84,7 @@ class PanelEnergy extends LitElement {
8384
8485 public connectedCallback ( ) {
8586 super . connectedCallback ( ) ;
86- this . _loadPrefs ( ) ;
87+ this . _loadLovelaceConfig ( ) ;
8788 }
8889
8990 public async willUpdate ( changedProps : PropertyValues ) {
@@ -101,32 +102,15 @@ class PanelEnergy extends LitElement {
101102 }
102103 }
103104
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- }
105+ private async _loadLovelaceConfig ( ) {
106+ try {
107+ this . _config = undefined ;
108+ this . _config = await this . _generateLovelaceConfig ( ) ;
109+ } catch ( err ) {
110+ this . _error = ( err as Error ) . message ;
129111 }
112+
113+ this . _setLovelace ( ) ;
130114 }
131115
132116 private _back ( ev ) {
@@ -135,25 +119,22 @@ class PanelEnergy extends LitElement {
135119 }
136120
137121 protected render ( ) {
138- if ( ! this . _energyCollection ?. prefs ) {
122+ if ( ! this . _config && ! this . _error ) {
139123 // Still loading
140- return html `<div class= "centered" >
141- <ha- spinner size= "large" > </ ha- spinner>
142- </ div> ` ;
143- }
144- const { prefs } = this . _energyCollection ;
145- const isSingleView = prefs . energy_sources . every ( ( source ) =>
146- [ "grid" , "solar" , "battery" ] . includes ( source . type )
147- ) ;
148- let viewPath = this . _viewPath ;
149- if ( isSingleView ) {
150- // if only electricity sources, show electricity view directly
151- viewPath = "electricity" ;
124+ return html `
125+ <div class= "centered" >
126+ <ha- spinner size= "large" > </ ha- spinner>
127+ </ div>
128+ ` ;
152129 }
153- const viewIndex = Math . max (
154- ENERGY_LOVELACE_CONFIG . views . findIndex ( ( view ) => view . path === viewPath ) ,
155- 0
156- ) ;
130+ const isSingleView = this . _config ?. views . length === 1 ;
131+ const viewPath = this . _viewPath ;
132+ const viewIndex = this . _config
133+ ? Math . max (
134+ this . _config . views . findIndex ( ( view ) => view . path === viewPath ) ,
135+ 0
136+ )
137+ : 0 ;
157138 const showBack =
158139 this . _searchParms . has ( "historyBack" ) || ( ! isSingleView && viewIndex > 0 ) ;
159140
@@ -229,10 +210,56 @@ class PanelEnergy extends LitElement {
229210 ` ;
230211 }
231212
213+ private _fetchEnergyPrefs = async ( ) : Promise <
214+ EnergyPreferences | undefined
215+ > => {
216+ const collection = getEnergyDataCollection ( this . hass , {
217+ key : DEFAULT_ENERGY_COLLECTION_KEY ,
218+ } ) ;
219+ try {
220+ await collection . refresh ( ) ;
221+ } catch ( err : any ) {
222+ if ( err . code === "not_found" ) {
223+ return undefined ;
224+ }
225+ throw err ;
226+ }
227+ return collection . prefs ;
228+ } ;
229+
230+ private async _generateLovelaceConfig ( ) : Promise < LovelaceConfig > {
231+ const prefs = await this . _fetchEnergyPrefs ( ) ;
232+ if (
233+ ! prefs ||
234+ ( prefs . device_consumption . length === 0 &&
235+ prefs . energy_sources . length === 0 )
236+ ) {
237+ await import ( "./cards/energy-setup-wizard-card" ) ;
238+ return {
239+ views : [ WIZARD_VIEW ] ,
240+ } ;
241+ }
242+
243+ const isElectricityOnly = prefs . energy_sources . every ( ( source ) =>
244+ [ "grid" , "solar" , "battery" ] . includes ( source . type )
245+ ) ;
246+ if ( isElectricityOnly ) {
247+ return {
248+ views : [ ELECTRICITY_VIEW ] ,
249+ } ;
250+ }
251+ return {
252+ views : [ OVERVIEW_VIEW , ELECTRICITY_VIEW ] ,
253+ } ;
254+ }
255+
232256 private _setLovelace ( ) {
257+ if ( ! this . _config ) {
258+ return ;
259+ }
233260 this . _lovelace = {
234- config : ENERGY_LOVELACE_CONFIG ,
235- rawConfig : ENERGY_LOVELACE_CONFIG ,
261+ config : this . _config ,
262+ rawConfig : this . _config ,
236263 editMode : false ,
237264 urlPath : "energy" ,
238265 mode : "generated" ,
@@ -252,7 +279,9 @@ class PanelEnergy extends LitElement {
252279
253280 private async _dumpCSV ( ev ) {
254281 ev . stopPropagation ( ) ;
255- const energyData = this . _energyCollection ! ;
282+ const energyData = getEnergyDataCollection ( this . hass , {
283+ key : "energy_dashboard" ,
284+ } ) ;
256285
257286 if ( ! energyData . prefs || ! energyData . state . stats ) {
258287 return ;
@@ -549,12 +578,7 @@ class PanelEnergy extends LitElement {
549578 }
550579
551580 private _reloadView ( ) {
552- // Force strategy to be re-run by making a copy of the view
553- const config = this . _lovelace ! . config ;
554- this . _lovelace = {
555- ...this . _lovelace ! ,
556- config : { ...config , views : config . views . map ( ( view ) => ( { ...view } ) ) } ,
557- } ;
581+ this . _loadLovelaceConfig ( ) ;
558582 }
559583
560584 static get styles ( ) : CSSResultGroup {
0 commit comments