@@ -39,11 +39,16 @@ class DeclutteringCard extends LitElement {
39
39
throw new Error ( "The object decluttering_templates doesn't exist in your main lovelace config." ) ;
40
40
}
41
41
const templateConfig = ll . config . decluttering_templates [ config . template ] as TemplateConfig ;
42
- if ( ! templateConfig || ! templateConfig . card ) {
42
+ if ( ! templateConfig ) {
43
43
throw new Error ( `The template "${ config . template } " doesn't exist in decluttering_templates` ) ;
44
+ } else if ( ! ( templateConfig . card || templateConfig . element ) ) {
45
+ throw new Error ( 'You shoud define either a card or an element in the template' ) ;
46
+ } else if ( templateConfig . card && templateConfig . element ) {
47
+ throw new Error ( 'You can define a card and an element in the template' ) ;
44
48
}
45
49
this . _config = deepReplace ( config . variables , templateConfig ) ;
46
- this . _createCard ( this . _config ) . then ( card => {
50
+ const type = templateConfig . card ? 'card' : 'element' ;
51
+ this . _createCard ( this . _config , type ) . then ( card => {
47
52
this . _card = card ;
48
53
return this . _card ;
49
54
} ) ;
@@ -57,12 +62,21 @@ class DeclutteringCard extends LitElement {
57
62
` ;
58
63
}
59
64
60
- private async _createCard ( config : LovelaceCardConfig ) : Promise < LovelaceCard > {
65
+ private async _createCard ( config : LovelaceCardConfig , type : 'element' | 'card' ) : Promise < LovelaceCard > {
61
66
let element : LovelaceCard ;
62
67
if ( HELPERS ) {
63
- if ( config . type === 'divider' ) element = ( await HELPERS ) . createRowElement ( config ) ;
64
- else element = ( await HELPERS ) . createCardElement ( config ) ;
65
- // fireEvent(element, 'll-rebuild');
68
+ if ( type === 'card' ) {
69
+ if ( config . type === 'divider' ) element = ( await HELPERS ) . createRowElement ( config ) ;
70
+ else element = ( await HELPERS ) . createCardElement ( config ) ;
71
+ // fireEvent(element, 'll-rebuild');
72
+ } else {
73
+ element = ( await HELPERS ) . createHuiElement ( config ) ;
74
+ if ( config . style ) {
75
+ Object . keys ( config . style ) . forEach ( prop => {
76
+ this . style . setProperty ( prop , config . style [ prop ] ) ;
77
+ } ) ;
78
+ }
79
+ }
66
80
} else {
67
81
element = createThing ( config ) ;
68
82
}
@@ -73,15 +87,19 @@ class DeclutteringCard extends LitElement {
73
87
'll-rebuild' ,
74
88
ev => {
75
89
ev . stopPropagation ( ) ;
76
- this . _rebuildCard ( element , config ) ;
90
+ this . _rebuildCard ( element , config , type ) ;
77
91
} ,
78
92
{ once : true } ,
79
93
) ;
80
94
return element ;
81
95
}
82
96
83
- private async _rebuildCard ( element : LovelaceCard , config : LovelaceCardConfig ) : Promise < void > {
84
- const newCard = await this . _createCard ( config ) ;
97
+ private async _rebuildCard (
98
+ element : LovelaceCard ,
99
+ config : LovelaceCardConfig ,
100
+ type : 'element' | 'card' ,
101
+ ) : Promise < void > {
102
+ const newCard = await this . _createCard ( config , type ) ;
85
103
element . replaceWith ( newCard ) ;
86
104
return ;
87
105
}
0 commit comments