1- import type { CSSResultGroup , PropertyValues , TemplateResult } from "lit" ;
1+ import type { CSSResultGroup , PropertyValues } from "lit" ;
22import { LitElement , css , html , nothing } from "lit" ;
33import { mdiPencil , mdiDownload } from "@mdi/js" ;
44import { customElement , property , state } from "lit/decorators" ;
55import "../../components/ha-menu-button" ;
66import "../../components/ha-icon-button-arrow-prev" ;
77import "../../components/ha-list-item" ;
88import "../../components/ha-top-app-bar-fixed" ;
9+ import "../../components/ha-alert" ;
910import type { LovelaceConfig } from "../../data/lovelace/config/types" ;
1011import { haStyle } from "../../resources/styles" ;
1112import type { HomeAssistant } from "../../types" ;
@@ -21,6 +22,7 @@ import type {
2122 GasSourceTypeEnergyPreference ,
2223 WaterSourceTypeEnergyPreference ,
2324 DeviceConsumptionEnergyPreference ,
25+ EnergyCollection ,
2426} from "../../data/energy" ;
2527import {
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 }
0 commit comments