Skip to content

Commit 0d001bd

Browse files
committed
Avoid rebuilding embedded cards if not needed
Fix #334
1 parent 9ad07fd commit 0d001bd

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

src/button-card.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ import {
3030
DOMAINS_TOGGLE,
3131
LovelaceCard,
3232
} from 'custom-card-helpers';
33-
import { ButtonCardConfig, StateConfig, ExemptionUserConfig, ExemptionUsernameConfig } from './types';
33+
import {
34+
ButtonCardConfig,
35+
StateConfig,
36+
ExemptionUserConfig,
37+
ExemptionUsernameConfig,
38+
CustomFieldCard,
39+
ButtonCardEmbeddedCards,
40+
ButtonCardEmbeddedCardsConfig,
41+
} from './types';
3442
import { actionHandler } from './action-handler';
3543
import {
3644
computeDomain,
@@ -83,18 +91,20 @@ class ButtonCard extends LitElement {
8391

8492
private _interval?: number;
8593

86-
private _cards: LovelaceCard[] = [];
94+
private _cards: ButtonCardEmbeddedCards = {};
95+
96+
private _cardsConfig: ButtonCardEmbeddedCardsConfig = {};
8797

8898
private _entities: string[] = [];
8999

90100
private _initial_setup_complete = false;
91101

92102
public set hass(hass: HomeAssistant) {
93103
this._hass = hass;
94-
for (const element of this._cards) {
95-
const el = element as any;
104+
Object.keys(this._cards).forEach(element => {
105+
const el = this._cards[element];
96106
el.hass = this._hass;
97-
}
107+
});
98108
if (!this._initial_setup_complete) {
99109
this._initConnected();
100110
}
@@ -142,7 +152,6 @@ class ButtonCard extends LitElement {
142152
if (!this._config || !this._hass) return html``;
143153
this._stateObj = this._config!.entity ? this._hass!.states[this._config!.entity] : undefined;
144154
try {
145-
this._cards = [];
146155
this._evaledVariables = this._config!.variables
147156
? this._objectEvalTemplate(this._stateObj, this._config!.variables)
148157
: undefined;
@@ -552,20 +561,20 @@ class ButtonCard extends LitElement {
552561
if (this._config!.custom_fields) {
553562
Object.keys(this._config!.custom_fields).forEach(key => {
554563
const value = this._config!.custom_fields![key];
555-
if (!value.card) {
564+
if (!(value as CustomFieldCard).card) {
556565
fields[key] = this._getTemplateOrValue(state, value);
557566
} else {
558-
cards[key] = this._objectEvalTemplate(state, value.card);
567+
cards[key] = this._objectEvalTemplate(state, (value as CustomFieldCard).card);
559568
}
560569
});
561570
}
562571
if (configState && configState.custom_fields) {
563572
Object.keys(configState.custom_fields).forEach(key => {
564573
const value = configState!.custom_fields![key];
565-
if (!value!.card) {
574+
if (!(value as CustomFieldCard)!.card) {
566575
fields[key] = this._getTemplateOrValue(state, value);
567576
} else {
568-
cards[key] = this._objectEvalTemplate(state, value.card);
577+
cards[key] = this._objectEvalTemplate(state, (value as CustomFieldCard).card);
569578
}
570579
});
571580
}
@@ -589,8 +598,15 @@ class ButtonCard extends LitElement {
589598
...this._buildCustomStyleGeneric(state, configState, key),
590599
'grid-area': key,
591600
};
592-
const thing = this._createCard(cards[key]);
593-
this._cards.push(thing);
601+
let thing;
602+
const stringConfig = JSON.stringify(cards[key]);
603+
if (this._cardsConfig[key] !== stringConfig) {
604+
thing = this._createCard(cards[key]);
605+
this._cards[key] = thing;
606+
this._cardsConfig[key] = stringConfig;
607+
} else {
608+
thing = this._cards[key];
609+
}
594610
thing.hass = this._hass;
595611
result = html`
596612
${result}
@@ -909,6 +925,8 @@ class ButtonCard extends LitElement {
909925
throw new Error('Invalid configuration');
910926
}
911927

928+
this._cards = {};
929+
this._cardsConfig = {};
912930
const ll = getLovelace() || getLovelaceCast();
913931
let template: ButtonCardConfig = JSON.parse(JSON.stringify(config));
914932
template = this._configFromLLTemplates(ll, template);

src/types.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ActionConfig } from 'custom-card-helpers';
1+
import { ActionConfig, LovelaceCardConfig, LovelaceCard } from 'custom-card-helpers';
22

33
export interface ButtonCardConfig {
44
template?: string | string[];
@@ -102,9 +102,21 @@ export interface CssStyleConfig {
102102
}
103103

104104
export interface CustomFields {
105-
[key: string]: any;
105+
[key: string]: string | CustomFieldCard;
106+
}
107+
108+
export interface CustomFieldCard {
109+
card: LovelaceCardConfig;
106110
}
107111

108112
export interface Variables {
109113
[key: string]: any;
110114
}
115+
116+
export interface ButtonCardEmbeddedCards {
117+
[key: string]: LovelaceCard;
118+
}
119+
120+
export interface ButtonCardEmbeddedCardsConfig {
121+
[key: string]: string | undefined;
122+
}

0 commit comments

Comments
 (0)