Skip to content

Commit f5af69d

Browse files
committed
Get data from state as backup
1 parent 0e6b868 commit f5af69d

3 files changed

Lines changed: 90 additions & 50 deletions

File tree

‎dist/Predbat-Table-Card-Next.js‎

Lines changed: 46 additions & 28 deletions
Large diffs are not rendered by default.

‎src/predbat-card.ts‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import { modalStyles } from './styles/modal-styles';
1010
import { divElement, getVersionRowElement, haCardElement, styleElement, toggleCarCharging, toggleGeneratingPlan } from './utils/html-utils';
1111
import { PredbatRestApiService } from './services/predbat-service';
1212
import { PredbatData } from './PredbatData';
13-
import { PredbatRawDataSchema, RawData } from './schemas/predbat';
1413

1514
class 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

‎src/services/home-assistant-service.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ export declare const process: {
55
};
66
};
77

8+
import { PredbatData } from '../PredbatData';
89
import { HassEntity, HistoryStateArraySchema } from '../schemas/home-assistant';
10+
import { PredbatRawDataSchema } from '../schemas/predbat';
911
import type { EntityObject, HistoryState, HomeAssistant, SoftwareVersion, Sun } from '../types/home-assistant';
1012
import { cleanAndValidateVersion } from '../utils/general-utils';
11-
import { PredbatData } from '../PredbatData';
1213

1314
export class HomeAssistantService {
1415
private _hass: HomeAssistant | null = null;
15-
private _predbatData: PredbatData | null = null;
1616
private _dayLimit: number | null = null;
17+
private _lastPlanUpdated: string | null = null;
1718

1819
// Accept new HomeAssistant data
1920
public set hass(newHass: HomeAssistant) {
@@ -68,10 +69,19 @@ export class HomeAssistantService {
6869
return this._hass.states;
6970
}
7071

71-
public get predbatData(): PredbatData {
72-
if (this._predbatData === null) throw new Error('this._predbatData is null');
72+
public get predbatData(): PredbatData | null {
73+
const planHtml = this._hass?.states['predbat.plan_html'];
74+
const historicEntity = this._hass?.states['predbat.cost_yesterday'];
75+
76+
if (planHtml === undefined) throw new Error('predbat.plan_html from HASS is undefined');
77+
if (historicEntity === undefined) throw new Error('predbat.cost_yesterday from HASS is undefined');
78+
79+
if (planHtml.last_updated === this._lastPlanUpdated) return null;
80+
81+
const validatedPlanData = PredbatRawDataSchema.parse(planHtml.attributes.raw);
82+
const validatedHistoricPlanData = PredbatRawDataSchema.parse(historicEntity.attributes.json);
7383

74-
return this._predbatData;
84+
return new PredbatData(validatedHistoricPlanData, validatedPlanData);
7585
}
7686

7787
private async _callService(domain: string, service: string, entity_id: string, option: string): Promise<void> {

0 commit comments

Comments
 (0)