Skip to content

Commit

Permalink
Merge 4a8f4d9 into 66be5f2
Browse files Browse the repository at this point in the history
  • Loading branch information
decompil3d authored Aug 9, 2022
2 parents 66be5f2 + 4a8f4d9 commit b0be76e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cypress/fixtures/harness.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
const now = new Date();
now.setMinutes(0);
h.hass = {
connection: {
subscribeMessage: (/** @type {(msg: string) => void} */ cb, /** @type {{template: string}} */ { template }) => {
cb(template.replace(/\{\{\s*/, '').replace(/\s*\}\}/, ''));
},
},
states: {
'weather.mock': {
attributes: {
Expand Down
17 changes: 17 additions & 0 deletions src/hourly-weather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export class HourlyWeatherCard extends LitElement {

@state() private renderedConfig!: Promise<HourlyWeatherCardConfig>;

private configRenderPending = false;

// https://lit.dev/docs/components/properties/#accessors-custom
public setConfig(config: HourlyWeatherCardConfig): void {
if (!config) {
Expand Down Expand Up @@ -97,6 +99,11 @@ export class HourlyWeatherCard extends LitElement {
}

private triggerConfigRender(): void {
if (!this.hass?.connection) {
// HASS connection not ready yet, so wait until it is
this.configRenderPending = true;
return;
}
this.renderedConfig = this.renderConfig();
}

Expand Down Expand Up @@ -151,6 +158,11 @@ export class HourlyWeatherCard extends LitElement {
// Don't mess with `selectedLanguage` since that might have unintended consequences
window.localStorage.setItem('haServerLanguage', this.hass.locale.language);
}

if (this.hass?.connection && this.configRenderPending) {
this.configRenderPending = false;
this.triggerConfigRender();
}
}

// https://lit.dev/docs/components/rendering/
Expand All @@ -161,6 +173,11 @@ export class HourlyWeatherCard extends LitElement {

private async renderCore(): Promise<TemplateResult | void> {
const config = await this.renderedConfig;

if (!config) {
return;
}

const entityId: string = config.entity;
const state = this.hass.states[entityId];
const { forecast } = state.attributes as { forecast: ForecastSegment[] };
Expand Down

0 comments on commit b0be76e

Please sign in to comment.