Skip to content

Commit 17ef74d

Browse files
authored
Fix take control of the dashboard (#24800)
1 parent 098c6a2 commit 17ef74d

File tree

5 files changed

+52
-38
lines changed

5 files changed

+52
-38
lines changed

cast/src/receiver/layout/hc-main.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ export class HcMain extends HassElement {
309309
"../../../../src/panels/lovelace/strategies/get-strategy"
310310
);
311311
const config = await generateLovelaceDashboardStrategy(
312-
rawConfig.strategy,
312+
rawConfig,
313313
this.hass!
314314
);
315315
this._handleNewLovelaceConfig(config);
@@ -351,10 +351,7 @@ export class HcMain extends HassElement {
351351
"../../../../src/panels/lovelace/strategies/get-strategy"
352352
);
353353
this._handleNewLovelaceConfig(
354-
await generateLovelaceDashboardStrategy(
355-
DEFAULT_CONFIG.strategy,
356-
this.hass!
357-
)
354+
await generateLovelaceDashboardStrategy(DEFAULT_CONFIG, this.hass!)
358355
);
359356
}
360357

src/panels/lovelace/ha-panel-lovelace.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export class LovelacePanel extends LitElement {
187187

188188
private async _regenerateConfig() {
189189
const conf = await generateLovelaceDashboardStrategy(
190-
DEFAULT_CONFIG.strategy,
190+
DEFAULT_CONFIG,
191191
this.hass!
192192
);
193193
this._setLovelaceConfig(conf, DEFAULT_CONFIG, "generated");
@@ -281,10 +281,7 @@ export class LovelacePanel extends LitElement {
281281
// We need these to generate a dashboard, wait for them
282282
return;
283283
}
284-
conf = await generateLovelaceDashboardStrategy(
285-
rawConf.strategy,
286-
this.hass!
287-
);
284+
conf = await generateLovelaceDashboardStrategy(rawConf, this.hass!);
288285
} else {
289286
conf = rawConf;
290287
}
@@ -301,7 +298,7 @@ export class LovelacePanel extends LitElement {
301298
return;
302299
}
303300
conf = await generateLovelaceDashboardStrategy(
304-
DEFAULT_CONFIG.strategy,
301+
DEFAULT_CONFIG,
305302
this.hass!
306303
);
307304
rawConf = DEFAULT_CONFIG;
@@ -378,10 +375,7 @@ export class LovelacePanel extends LitElement {
378375
let conf: LovelaceConfig;
379376
// If strategy defined, apply it here.
380377
if (isStrategyDashboard(newConfig)) {
381-
conf = await generateLovelaceDashboardStrategy(
382-
newConfig.strategy,
383-
this.hass!
384-
);
378+
conf = await generateLovelaceDashboardStrategy(newConfig, this.hass!);
385379
} else {
386380
conf = newConfig;
387381
}
@@ -415,7 +409,7 @@ export class LovelacePanel extends LitElement {
415409
try {
416410
// Optimistic update
417411
const generatedConf = await generateLovelaceDashboardStrategy(
418-
DEFAULT_CONFIG.strategy,
412+
DEFAULT_CONFIG,
419413
this.hass!
420414
);
421415
this._updateLovelace({

src/panels/lovelace/sections/hui-section.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export class HuiSection extends ReactiveElement {
185185
if (isStrategySection(sectionConfig)) {
186186
isStrategy = true;
187187
sectionConfig = await generateLovelaceSectionStrategy(
188-
sectionConfig.strategy,
188+
sectionConfig,
189189
this.hass!
190190
);
191191
}

src/panels/lovelace/strategies/get-strategy.ts

+43-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
import type {
2+
LovelaceSectionConfig,
3+
LovelaceStrategySectionConfig,
4+
} from "../../../data/lovelace/config/section";
5+
import type { LovelaceStrategyConfig } from "../../../data/lovelace/config/strategy";
16
import type {
27
LovelaceConfig,
8+
LovelaceDashboardStrategyConfig,
39
LovelaceRawConfig,
410
} from "../../../data/lovelace/config/types";
511
import { isStrategyDashboard } from "../../../data/lovelace/config/types";
6-
import type { LovelaceStrategyConfig } from "../../../data/lovelace/config/strategy";
7-
import type { LovelaceViewConfig } from "../../../data/lovelace/config/view";
12+
import type {
13+
LovelaceStrategyViewConfig,
14+
LovelaceViewConfig,
15+
} from "../../../data/lovelace/config/view";
816
import { isStrategyView } from "../../../data/lovelace/config/view";
917
import type { AsyncReturnType, HomeAssistant } from "../../../types";
1018
import { cleanLegacyStrategyConfig, isLegacyStrategy } from "./legacy-strategy";
@@ -133,10 +141,11 @@ const generateStrategy = async <T extends LovelaceStrategyConfigType>(
133141
};
134142

135143
export const generateLovelaceDashboardStrategy = async (
136-
strategyConfig: LovelaceStrategyConfig,
144+
config: LovelaceDashboardStrategyConfig,
137145
hass: HomeAssistant
138-
): Promise<LovelaceConfig> =>
139-
generateStrategy(
146+
): Promise<LovelaceConfig> => {
147+
const { strategy, ...base } = config;
148+
const generated = generateStrategy(
140149
"dashboard",
141150
(err) => ({
142151
views: [
@@ -151,15 +160,21 @@ export const generateLovelaceDashboardStrategy = async (
151160
},
152161
],
153162
}),
154-
strategyConfig,
163+
strategy,
155164
hass
156165
);
166+
return {
167+
...base,
168+
...generated,
169+
};
170+
};
157171

158172
export const generateLovelaceViewStrategy = async (
159-
strategyConfig: LovelaceStrategyConfig,
173+
config: LovelaceStrategyViewConfig,
160174
hass: HomeAssistant
161-
): Promise<LovelaceViewConfig> =>
162-
generateStrategy(
175+
): Promise<LovelaceViewConfig> => {
176+
const { strategy, ...base } = config;
177+
const generated = await generateStrategy(
163178
"view",
164179
(err) => ({
165180
cards: [
@@ -169,15 +184,21 @@ export const generateLovelaceViewStrategy = async (
169184
},
170185
],
171186
}),
172-
strategyConfig,
187+
strategy,
173188
hass
174189
);
190+
return {
191+
...base,
192+
...generated,
193+
};
194+
};
175195

176196
export const generateLovelaceSectionStrategy = async (
177-
strategyConfig: LovelaceStrategyConfig,
197+
config: LovelaceStrategySectionConfig,
178198
hass: HomeAssistant
179-
): Promise<LovelaceViewConfig> =>
180-
generateStrategy(
199+
): Promise<LovelaceSectionConfig> => {
200+
const { strategy, ...base } = config;
201+
const generated = await generateStrategy(
181202
"section",
182203
(err) => ({
183204
cards: [
@@ -187,9 +208,14 @@ export const generateLovelaceSectionStrategy = async (
187208
},
188209
],
189210
}),
190-
strategyConfig,
211+
strategy,
191212
hass
192213
);
214+
return {
215+
...base,
216+
...generated,
217+
};
218+
};
193219

194220
/**
195221
* Find all references to strategies and replaces them with the generated output
@@ -199,20 +225,20 @@ export const expandLovelaceConfigStrategies = async (
199225
hass: HomeAssistant
200226
): Promise<LovelaceConfig> => {
201227
const newConfig = isStrategyDashboard(config)
202-
? await generateLovelaceDashboardStrategy(config.strategy, hass)
228+
? await generateLovelaceDashboardStrategy(config, hass)
203229
: { ...config };
204230

205231
newConfig.views = await Promise.all(
206232
newConfig.views.map(async (view) => {
207233
const newView = isStrategyView(view)
208-
? await generateLovelaceViewStrategy(view.strategy, hass)
234+
? await generateLovelaceViewStrategy(view, hass)
209235
: { ...view };
210236

211237
if (newView.sections) {
212238
newView.sections = await Promise.all(
213239
newView.sections.map(async (section) => {
214240
const newSection = isStrategyView(section)
215-
? await generateLovelaceSectionStrategy(section.strategy, hass)
241+
? await generateLovelaceSectionStrategy(section, hass)
216242
: { ...section };
217243
return newSection;
218244
})

src/panels/lovelace/views/hui-view.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,7 @@ export class HUIView extends ReactiveElement {
233233

234234
if (isStrategyView(viewConfig)) {
235235
isStrategy = true;
236-
viewConfig = await generateLovelaceViewStrategy(
237-
viewConfig.strategy,
238-
this.hass!
239-
);
236+
viewConfig = await generateLovelaceViewStrategy(viewConfig, this.hass!);
240237
}
241238

242239
viewConfig = {

0 commit comments

Comments
 (0)