@@ -10,16 +10,16 @@ import { modalStyles } from './styles/modal-styles';
1010import { divElement , getVersionRowElement , haCardElement , styleElement , toggleCarCharging , toggleGeneratingPlan } from './utils/html-utils' ;
1111import { PredbatRestApiService } from './services/predbat-service' ;
1212import { PredbatData } from './PredbatData' ;
13- import { PredbatRawDataSchema , RawData } from './schemas/predbat' ;
1413
1514class PredbatTableCard extends HTMLElement {
1615 private _haService = new HomeAssistantService ( ) ;
17- private _predbatService : PredbatRestApiService | null = null ;
16+ private _predbatRestApiService : PredbatRestApiService | null = null ;
1817 private _configManager : ConfigManager = new ConfigManager ( ) ;
1918 private _tableRenderer : TableRenderer = new TableRenderer ( this . _configManager ) ;
2019 private _weatherService = new WeatherService ( ) ;
2120 private _planData : PredbatData | null = null ;
2221 private _restApiAvailable : boolean | null = null ;
22+ private _lastPlanUpdated : string | null = null ;
2323
2424 // reactive data
2525 private _reactiveDataState : { overrides : string | null , generatingPlan : boolean , carCharging : 'on' | 'off' | undefined } = {
@@ -48,16 +48,16 @@ class PredbatTableCard extends HTMLElement {
4848
4949 this . _reactiveDataState . carCharging = this . _configManager . carChargeSwitch ;
5050
51- if ( this . _predbatService ) {
52- this . _predbatService . disconnect ( ) ;
53- this . _predbatService = null ;
51+ if ( this . _predbatRestApiService ) {
52+ this . _predbatRestApiService . disconnect ( ) ;
53+ this . _predbatRestApiService = null ;
5454 }
5555
5656 PredbatRestApiService . restApiAvailable ( restApiPort )
5757 . then ( ( restApiAvailable ) => {
5858 this . _restApiAvailable = restApiAvailable ;
5959
60- if ( restApiAvailable ) this . _predbatService = new PredbatRestApiService ( restApiPort , this . setPlanData ) ;
60+ if ( restApiAvailable ) this . _predbatRestApiService = new PredbatRestApiService ( restApiPort , this . setPlanData ) ;
6161 } )
6262 . catch ( ( ) => this . _restApiAvailable = false ) ;
6363 }
@@ -88,14 +88,21 @@ class PredbatTableCard extends HTMLElement {
8888
8989 // Get data from state if no REST API is available
9090 if ( this . _restApiAvailable === false ) {
91- const validatedHistoricPlanData : RawData = PredbatRawDataSchema . parse ( { } ) ;
92- const validatedPlanData : RawData = PredbatRawDataSchema . parse ( { } ) ;
91+ const planEntity = hass . states [ 'predbat.plan_html' ] ;
9392
94- const newPlanData = new PredbatData ( validatedHistoricPlanData , validatedPlanData ) ;
93+ if ( planEntity !== undefined && planEntity . last_updated !== this . _lastPlanUpdated ) {
94+ const predbatData = this . _haService . predbatData ;
9595
96- this . setPlanData ( newPlanData ) ;
96+ if ( predbatData !== null ) {
97+ this . setPlanData ( predbatData ) ;
98+ this . _lastPlanUpdated = planEntity . last_updated ;
9799
98- this . _render ( ) ;
100+ this . _render ( ) ;
101+ }
102+ }
103+ else {
104+ // skip rendering
105+ }
99106 }
100107 }
101108
@@ -157,11 +164,13 @@ class PredbatTableCard extends HTMLElement {
157164
158165 this . _tableRenderer . container = tableOuterContainer ;
159166 this . _tableRenderer . onUpdate = ( ) : void => {
160- if ( this . _predbatService ) this . _render ( ) ;
167+ if ( this . _predbatRestApiService ) this . _render ( ) ;
161168 } ;
162169 this . _weatherService . onUpdate = ( ) : void => {
163- if ( this . _predbatService ) this . _render ( ) ;
170+ if ( this . _predbatRestApiService ) this . _render ( ) ;
164171 } ;
172+
173+ this . _render ( ) ;
165174 }
166175
167176 // * Lifecycle - Card component removed from DOM
@@ -170,9 +179,9 @@ class PredbatTableCard extends HTMLElement {
170179 await this . _weatherService . unsubscribe ( ) ;
171180 }
172181
173- if ( this . _predbatService ) {
174- this . _predbatService . disconnect ( ) ;
175- this . _predbatService = null ;
182+ if ( this . _predbatRestApiService ) {
183+ this . _predbatRestApiService . disconnect ( ) ;
184+ this . _predbatRestApiService = null ;
176185 }
177186 }
178187
@@ -198,7 +207,10 @@ class PredbatTableCard extends HTMLElement {
198207 } ;
199208
200209 private _render ( ) : void {
201- if ( this . _predbatService === null ) {
210+ if (
211+ this . _restApiAvailable === null ||
212+ ( this . _restApiAvailable && this . _predbatRestApiService === null )
213+ ) {
202214 // Predbat service is initialising
203215 this . _tableRenderer . renderInfo ( 'Initializing Predbat service...' ) ;
204216
0 commit comments