@@ -30,7 +30,15 @@ import {
30
30
DOMAINS_TOGGLE ,
31
31
LovelaceCard ,
32
32
} 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' ;
34
42
import { actionHandler } from './action-handler' ;
35
43
import {
36
44
computeDomain ,
@@ -83,18 +91,20 @@ class ButtonCard extends LitElement {
83
91
84
92
private _interval ?: number ;
85
93
86
- private _cards : LovelaceCard [ ] = [ ] ;
94
+ private _cards : ButtonCardEmbeddedCards = { } ;
95
+
96
+ private _cardsConfig : ButtonCardEmbeddedCardsConfig = { } ;
87
97
88
98
private _entities : string [ ] = [ ] ;
89
99
90
100
private _initial_setup_complete = false ;
91
101
92
102
public set hass ( hass : HomeAssistant ) {
93
103
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 ] ;
96
106
el . hass = this . _hass ;
97
- }
107
+ } ) ;
98
108
if ( ! this . _initial_setup_complete ) {
99
109
this . _initConnected ( ) ;
100
110
}
@@ -142,7 +152,6 @@ class ButtonCard extends LitElement {
142
152
if ( ! this . _config || ! this . _hass ) return html `` ;
143
153
this . _stateObj = this . _config ! . entity ? this . _hass ! . states [ this . _config ! . entity ] : undefined ;
144
154
try {
145
- this . _cards = [ ] ;
146
155
this . _evaledVariables = this . _config ! . variables
147
156
? this . _objectEvalTemplate ( this . _stateObj , this . _config ! . variables )
148
157
: undefined ;
@@ -552,20 +561,20 @@ class ButtonCard extends LitElement {
552
561
if ( this . _config ! . custom_fields ) {
553
562
Object . keys ( this . _config ! . custom_fields ) . forEach ( key => {
554
563
const value = this . _config ! . custom_fields ! [ key ] ;
555
- if ( ! value . card ) {
564
+ if ( ! ( value as CustomFieldCard ) . card ) {
556
565
fields [ key ] = this . _getTemplateOrValue ( state , value ) ;
557
566
} else {
558
- cards [ key ] = this . _objectEvalTemplate ( state , value . card ) ;
567
+ cards [ key ] = this . _objectEvalTemplate ( state , ( value as CustomFieldCard ) . card ) ;
559
568
}
560
569
} ) ;
561
570
}
562
571
if ( configState && configState . custom_fields ) {
563
572
Object . keys ( configState . custom_fields ) . forEach ( key => {
564
573
const value = configState ! . custom_fields ! [ key ] ;
565
- if ( ! value ! . card ) {
574
+ if ( ! ( value as CustomFieldCard ) ! . card ) {
566
575
fields [ key ] = this . _getTemplateOrValue ( state , value ) ;
567
576
} else {
568
- cards [ key ] = this . _objectEvalTemplate ( state , value . card ) ;
577
+ cards [ key ] = this . _objectEvalTemplate ( state , ( value as CustomFieldCard ) . card ) ;
569
578
}
570
579
} ) ;
571
580
}
@@ -589,8 +598,15 @@ class ButtonCard extends LitElement {
589
598
...this . _buildCustomStyleGeneric ( state , configState , key ) ,
590
599
'grid-area' : key ,
591
600
} ;
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
+ }
594
610
thing . hass = this . _hass ;
595
611
result = html `
596
612
${ result }
@@ -909,6 +925,8 @@ class ButtonCard extends LitElement {
909
925
throw new Error ( 'Invalid configuration' ) ;
910
926
}
911
927
928
+ this . _cards = { } ;
929
+ this . _cardsConfig = { } ;
912
930
const ll = getLovelace ( ) || getLovelaceCast ( ) ;
913
931
let template : ButtonCardConfig = JSON . parse ( JSON . stringify ( config ) ) ;
914
932
template = this . _configFromLLTemplates ( ll , template ) ;
0 commit comments