Energy: Wait with subscribe for _config to be set

This commit is contained in:
Bram Kragten 2022-02-28 18:21:55 +01:00
parent 437de42c55
commit 2df3041b49
No known key found for this signature in database
GPG Key ID: FBE2DFDB363EF55B
10 changed files with 34 additions and 1 deletions

View File

@ -13,6 +13,8 @@ export const SubscribeMixin = <T extends Constructor<ReactiveElement>>(
class SubscribeClass extends superClass {
@property({ attribute: false }) public hass?: HomeAssistant;
protected hassSubscribeNeedsProperties?: string[];
private __unsubs?: Array<UnsubscribeFunc | Promise<UnsubscribeFunc>>;
public connectedCallback() {
@ -39,6 +41,16 @@ export const SubscribeMixin = <T extends Constructor<ReactiveElement>>(
super.updated(changedProps);
if (changedProps.has("hass")) {
this.__checkSubscribed();
return;
}
if (!this.hassSubscribeNeedsProperties) {
return;
}
for (const key of changedProps.keys()) {
if (this.hassSubscribeNeedsProperties.includes(key as string)) {
this.__checkSubscribed();
return;
}
}
}
@ -52,7 +64,10 @@ export const SubscribeMixin = <T extends Constructor<ReactiveElement>>(
if (
this.__unsubs !== undefined ||
!(this as unknown as Element).isConnected ||
this.hass === undefined
this.hass === undefined ||
this.hassSubscribeNeedsProperties?.some(
(prop) => this[prop] === undefined
)
) {
return;
}

View File

@ -32,6 +32,8 @@ class HuiEnergyCarbonGaugeCard
@state() private _data?: EnergyData;
protected hassSubscribeNeedsProperties = ["_config"];
public getCardSize(): number {
return 4;
}

View File

@ -49,6 +49,8 @@ export class HuiEnergyDevicesGraphCard
@query("ha-chart-base") private _chart?: HaChartBase;
protected hassSubscribeNeedsProperties = ["_config"];
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass, {

View File

@ -43,6 +43,8 @@ class HuiEnergyDistrubutionCard
@state() private _data?: EnergyData;
protected hassSubscribeNeedsProperties = ["_config"];
public setConfig(config: EnergyDistributionCardConfig): void {
this._config = config;
}

View File

@ -62,6 +62,8 @@ export class HuiEnergyGasGraphCard
@state() private _unit?: string;
protected hassSubscribeNeedsProperties = ["_config"];
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass, {

View File

@ -35,6 +35,8 @@ class HuiEnergyGridGaugeCard
@state() private _data?: EnergyData;
protected hassSubscribeNeedsProperties = ["_config"];
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass!, {

View File

@ -30,6 +30,8 @@ class HuiEnergySolarGaugeCard
@state() private _data?: EnergyData;
protected hassSubscribeNeedsProperties = ["_config"];
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass!, {

View File

@ -61,6 +61,8 @@ export class HuiEnergySolarGraphCard
@state() private _end = endOfToday();
protected hassSubscribeNeedsProperties = ["_config"];
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass, {

View File

@ -45,6 +45,8 @@ export class HuiEnergySourcesTableCard
@state() private _data?: EnergyData;
protected hassSubscribeNeedsProperties = ["_config"];
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass, {

View File

@ -50,6 +50,8 @@ export class HuiEnergyUsageGraphCard
@state() private _end = endOfToday();
protected hassSubscribeNeedsProperties = ["_config"];
public hassSubscribe(): UnsubscribeFunc[] {
return [
getEnergyDataCollection(this.hass, {