@@ -3,15 +3,20 @@ import type { PropertyValues } from "lit";
3
3
import { ReactiveElement } from "lit" ;
4
4
import { customElement , property , state } from "lit/decorators" ;
5
5
import { storage } from "../../../common/decorators/storage" ;
6
- import type { HASSDomEvent } from "../../../common/dom/fire_event" ;
6
+ import { fireEvent , type HASSDomEvent } from "../../../common/dom/fire_event" ;
7
+ import { debounce } from "../../../common/util/debounce" ;
8
+ import { deepEqual } from "../../../common/util/deep-equal" ;
7
9
import "../../../components/entity/ha-state-label-badge" ;
8
10
import "../../../components/ha-svg-icon" ;
9
11
import type { LovelaceViewElement } from "../../../data/lovelace" ;
10
12
import type { LovelaceBadgeConfig } from "../../../data/lovelace/config/badge" ;
11
13
import { ensureBadgeConfig } from "../../../data/lovelace/config/badge" ;
12
14
import type { LovelaceCardConfig } from "../../../data/lovelace/config/card" ;
13
15
import type { LovelaceSectionConfig } from "../../../data/lovelace/config/section" ;
14
- import type { LovelaceViewConfig } from "../../../data/lovelace/config/view" ;
16
+ import type {
17
+ LovelaceViewConfig ,
18
+ LovelaceViewRawConfig ,
19
+ } from "../../../data/lovelace/config/view" ;
15
20
import { isStrategyView } from "../../../data/lovelace/config/view" ;
16
21
import type { HomeAssistant } from "../../../types" ;
17
22
import "../badges/hui-badge" ;
@@ -85,6 +90,10 @@ export class HUIView extends ReactiveElement {
85
90
86
91
private _layoutElement ?: LovelaceViewElement ;
87
92
93
+ private _layoutElementConfig ?: LovelaceViewConfig ;
94
+
95
+ private _rendered = false ;
96
+
88
97
@storage ( {
89
98
key : "dashboardCardClipboard" ,
90
99
state : false ,
@@ -145,6 +154,18 @@ export class HUIView extends ReactiveElement {
145
154
return this ;
146
155
}
147
156
157
+ connectedCallback ( ) : void {
158
+ super . connectedCallback ( ) ;
159
+ this . updateComplete . then ( ( ) => {
160
+ this . _rendered = true ;
161
+ } ) ;
162
+ }
163
+
164
+ disconnectedCallback ( ) : void {
165
+ super . disconnectedCallback ( ) ;
166
+ this . _rendered = false ;
167
+ }
168
+
148
169
public willUpdate ( changedProperties : PropertyValues < typeof this > ) : void {
149
170
super . willUpdate ( changedProperties ) ;
150
171
@@ -169,9 +190,62 @@ export class HUIView extends ReactiveElement {
169
190
oldLovelace . config . views [ this . index ] ) )
170
191
) {
171
192
this . _initializeConfig ( ) ;
193
+ return ;
194
+ }
195
+
196
+ if ( ! changedProperties . has ( "hass" ) ) {
197
+ return ;
198
+ }
199
+
200
+ const oldHass = changedProperties . get ( "hass" ) as HomeAssistant | undefined ;
201
+ const viewConfig = this . lovelace . config . views [ this . index ] ;
202
+ if ( oldHass && this . hass && this . lovelace && isStrategyView ( viewConfig ) ) {
203
+ if (
204
+ oldHass . entities !== this . hass . entities ||
205
+ oldHass . devices !== this . hass . devices ||
206
+ oldHass . areas !== this . hass . areas ||
207
+ oldHass . floors !== this . hass . floors
208
+ ) {
209
+ if ( this . hass . config . state === "RUNNING" ) {
210
+ // If the page is not rendered yet, we can force the refresh
211
+ if ( this . _rendered ) {
212
+ this . _debounceRefreshConfig ( false ) ;
213
+ } else {
214
+ this . _refreshConfig ( true ) ;
215
+ }
216
+ }
217
+ }
172
218
}
173
219
}
174
220
221
+ private _debounceRefreshConfig = debounce (
222
+ ( force : boolean ) => this . _refreshConfig ( force ) ,
223
+ 200
224
+ ) ;
225
+
226
+ private _refreshConfig = async ( force : boolean ) => {
227
+ if ( ! this . hass || ! this . lovelace ) {
228
+ return ;
229
+ }
230
+ const viewConfig = this . lovelace . config . views [ this . index ] ;
231
+
232
+ if ( ! isStrategyView ( viewConfig ) ) {
233
+ return ;
234
+ }
235
+
236
+ const oldConfig = this . _layoutElementConfig ;
237
+ const newConfig = await this . _generateConfig ( viewConfig ) ;
238
+
239
+ // Don't ask if the config is the same
240
+ if ( ! deepEqual ( newConfig , oldConfig ) ) {
241
+ if ( force ) {
242
+ this . _setConfig ( newConfig , true ) ;
243
+ } else {
244
+ fireEvent ( this , "strategy-config-changed" ) ;
245
+ }
246
+ }
247
+ } ;
248
+
175
249
protected update ( changedProperties : PropertyValues ) {
176
250
super . update ( changedProperties ) ;
177
251
@@ -227,28 +301,38 @@ export class HUIView extends ReactiveElement {
227
301
}
228
302
}
229
303
230
- private async _initializeConfig ( ) {
231
- let viewConfig = this . lovelace . config . views [ this . index ] ;
232
- let isStrategy = false ;
233
-
234
- if ( isStrategyView ( viewConfig ) ) {
235
- isStrategy = true ;
236
- viewConfig = await generateLovelaceViewStrategy ( viewConfig , this . hass ! ) ;
304
+ private async _generateConfig (
305
+ config : LovelaceViewRawConfig
306
+ ) : Promise < LovelaceViewConfig > {
307
+ if ( isStrategyView ( config ) ) {
308
+ const generatedConfig = await generateLovelaceViewStrategy (
309
+ config ,
310
+ this . hass !
311
+ ) ;
312
+ return {
313
+ ...generatedConfig ,
314
+ type : getViewType ( generatedConfig ) ,
315
+ } ;
237
316
}
238
317
239
- viewConfig = {
240
- ...viewConfig ,
241
- type : getViewType ( viewConfig ) ,
318
+ return {
319
+ ...config ,
320
+ type : getViewType ( config ) ,
242
321
} ;
322
+ }
243
323
324
+ private async _setConfig (
325
+ viewConfig : LovelaceViewConfig ,
326
+ isStrategy : boolean
327
+ ) {
244
328
// Create a new layout element if necessary.
245
329
let addLayoutElement = false ;
246
330
247
331
if ( ! this . _layoutElement || this . _layoutElementType !== viewConfig . type ) {
248
332
addLayoutElement = true ;
249
333
this . _createLayoutElement ( viewConfig ) ;
250
334
}
251
-
335
+ this . _layoutElementConfig = viewConfig ;
252
336
this . _createBadges ( viewConfig ) ;
253
337
this . _createCards ( viewConfig ) ;
254
338
this . _createSections ( viewConfig ) ;
@@ -269,6 +353,15 @@ export class HUIView extends ReactiveElement {
269
353
}
270
354
}
271
355
356
+ private async _initializeConfig ( ) {
357
+ const rawConfig = this . lovelace . config . views [ this . index ] ;
358
+
359
+ const viewConfig = await this . _generateConfig ( rawConfig ) ;
360
+ const isStrategy = isStrategyView ( viewConfig ) ;
361
+
362
+ this . _setConfig ( viewConfig , isStrategy ) ;
363
+ }
364
+
272
365
private _createLayoutElement ( config : LovelaceViewConfig ) : void {
273
366
this . _layoutElement = createViewElement ( config ) as LovelaceViewElement ;
274
367
this . _layoutElementType = config . type ;
0 commit comments