From 17ef74d680aa1cb1c118d5dbbc6b39b5c62d7e5b Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Thu, 27 Mar 2025 13:33:52 +0100 Subject: [PATCH] Fix take control of the dashboard (#24800) --- cast/src/receiver/layout/hc-main.ts | 7 +-- src/panels/lovelace/ha-panel-lovelace.ts | 16 ++--- src/panels/lovelace/sections/hui-section.ts | 2 +- .../lovelace/strategies/get-strategy.ts | 60 +++++++++++++------ src/panels/lovelace/views/hui-view.ts | 5 +- 5 files changed, 52 insertions(+), 38 deletions(-) diff --git a/cast/src/receiver/layout/hc-main.ts b/cast/src/receiver/layout/hc-main.ts index 958d94cccc..46267f715a 100644 --- a/cast/src/receiver/layout/hc-main.ts +++ b/cast/src/receiver/layout/hc-main.ts @@ -309,7 +309,7 @@ export class HcMain extends HassElement { "../../../../src/panels/lovelace/strategies/get-strategy" ); const config = await generateLovelaceDashboardStrategy( - rawConfig.strategy, + rawConfig, this.hass! ); this._handleNewLovelaceConfig(config); @@ -351,10 +351,7 @@ export class HcMain extends HassElement { "../../../../src/panels/lovelace/strategies/get-strategy" ); this._handleNewLovelaceConfig( - await generateLovelaceDashboardStrategy( - DEFAULT_CONFIG.strategy, - this.hass! - ) + await generateLovelaceDashboardStrategy(DEFAULT_CONFIG, this.hass!) ); } diff --git a/src/panels/lovelace/ha-panel-lovelace.ts b/src/panels/lovelace/ha-panel-lovelace.ts index eaf0d05ba5..bc7471d9c3 100644 --- a/src/panels/lovelace/ha-panel-lovelace.ts +++ b/src/panels/lovelace/ha-panel-lovelace.ts @@ -187,7 +187,7 @@ export class LovelacePanel extends LitElement { private async _regenerateConfig() { const conf = await generateLovelaceDashboardStrategy( - DEFAULT_CONFIG.strategy, + DEFAULT_CONFIG, this.hass! ); this._setLovelaceConfig(conf, DEFAULT_CONFIG, "generated"); @@ -281,10 +281,7 @@ export class LovelacePanel extends LitElement { // We need these to generate a dashboard, wait for them return; } - conf = await generateLovelaceDashboardStrategy( - rawConf.strategy, - this.hass! - ); + conf = await generateLovelaceDashboardStrategy(rawConf, this.hass!); } else { conf = rawConf; } @@ -301,7 +298,7 @@ export class LovelacePanel extends LitElement { return; } conf = await generateLovelaceDashboardStrategy( - DEFAULT_CONFIG.strategy, + DEFAULT_CONFIG, this.hass! ); rawConf = DEFAULT_CONFIG; @@ -378,10 +375,7 @@ export class LovelacePanel extends LitElement { let conf: LovelaceConfig; // If strategy defined, apply it here. if (isStrategyDashboard(newConfig)) { - conf = await generateLovelaceDashboardStrategy( - newConfig.strategy, - this.hass! - ); + conf = await generateLovelaceDashboardStrategy(newConfig, this.hass!); } else { conf = newConfig; } @@ -415,7 +409,7 @@ export class LovelacePanel extends LitElement { try { // Optimistic update const generatedConf = await generateLovelaceDashboardStrategy( - DEFAULT_CONFIG.strategy, + DEFAULT_CONFIG, this.hass! ); this._updateLovelace({ diff --git a/src/panels/lovelace/sections/hui-section.ts b/src/panels/lovelace/sections/hui-section.ts index 5bcd11b56a..1db1de751e 100644 --- a/src/panels/lovelace/sections/hui-section.ts +++ b/src/panels/lovelace/sections/hui-section.ts @@ -185,7 +185,7 @@ export class HuiSection extends ReactiveElement { if (isStrategySection(sectionConfig)) { isStrategy = true; sectionConfig = await generateLovelaceSectionStrategy( - sectionConfig.strategy, + sectionConfig, this.hass! ); } diff --git a/src/panels/lovelace/strategies/get-strategy.ts b/src/panels/lovelace/strategies/get-strategy.ts index 107bc6e918..ad55a027ce 100644 --- a/src/panels/lovelace/strategies/get-strategy.ts +++ b/src/panels/lovelace/strategies/get-strategy.ts @@ -1,10 +1,18 @@ +import type { + LovelaceSectionConfig, + LovelaceStrategySectionConfig, +} from "../../../data/lovelace/config/section"; +import type { LovelaceStrategyConfig } from "../../../data/lovelace/config/strategy"; import type { LovelaceConfig, + LovelaceDashboardStrategyConfig, LovelaceRawConfig, } from "../../../data/lovelace/config/types"; import { isStrategyDashboard } from "../../../data/lovelace/config/types"; -import type { LovelaceStrategyConfig } from "../../../data/lovelace/config/strategy"; -import type { LovelaceViewConfig } from "../../../data/lovelace/config/view"; +import type { + LovelaceStrategyViewConfig, + LovelaceViewConfig, +} from "../../../data/lovelace/config/view"; import { isStrategyView } from "../../../data/lovelace/config/view"; import type { AsyncReturnType, HomeAssistant } from "../../../types"; import { cleanLegacyStrategyConfig, isLegacyStrategy } from "./legacy-strategy"; @@ -133,10 +141,11 @@ const generateStrategy = async ( }; export const generateLovelaceDashboardStrategy = async ( - strategyConfig: LovelaceStrategyConfig, + config: LovelaceDashboardStrategyConfig, hass: HomeAssistant -): Promise => - generateStrategy( +): Promise => { + const { strategy, ...base } = config; + const generated = generateStrategy( "dashboard", (err) => ({ views: [ @@ -151,15 +160,21 @@ export const generateLovelaceDashboardStrategy = async ( }, ], }), - strategyConfig, + strategy, hass ); + return { + ...base, + ...generated, + }; +}; export const generateLovelaceViewStrategy = async ( - strategyConfig: LovelaceStrategyConfig, + config: LovelaceStrategyViewConfig, hass: HomeAssistant -): Promise => - generateStrategy( +): Promise => { + const { strategy, ...base } = config; + const generated = await generateStrategy( "view", (err) => ({ cards: [ @@ -169,15 +184,21 @@ export const generateLovelaceViewStrategy = async ( }, ], }), - strategyConfig, + strategy, hass ); + return { + ...base, + ...generated, + }; +}; export const generateLovelaceSectionStrategy = async ( - strategyConfig: LovelaceStrategyConfig, + config: LovelaceStrategySectionConfig, hass: HomeAssistant -): Promise => - generateStrategy( +): Promise => { + const { strategy, ...base } = config; + const generated = await generateStrategy( "section", (err) => ({ cards: [ @@ -187,9 +208,14 @@ export const generateLovelaceSectionStrategy = async ( }, ], }), - strategyConfig, + strategy, hass ); + return { + ...base, + ...generated, + }; +}; /** * Find all references to strategies and replaces them with the generated output @@ -199,20 +225,20 @@ export const expandLovelaceConfigStrategies = async ( hass: HomeAssistant ): Promise => { const newConfig = isStrategyDashboard(config) - ? await generateLovelaceDashboardStrategy(config.strategy, hass) + ? await generateLovelaceDashboardStrategy(config, hass) : { ...config }; newConfig.views = await Promise.all( newConfig.views.map(async (view) => { const newView = isStrategyView(view) - ? await generateLovelaceViewStrategy(view.strategy, hass) + ? await generateLovelaceViewStrategy(view, hass) : { ...view }; if (newView.sections) { newView.sections = await Promise.all( newView.sections.map(async (section) => { const newSection = isStrategyView(section) - ? await generateLovelaceSectionStrategy(section.strategy, hass) + ? await generateLovelaceSectionStrategy(section, hass) : { ...section }; return newSection; }) diff --git a/src/panels/lovelace/views/hui-view.ts b/src/panels/lovelace/views/hui-view.ts index 6b2f19739b..12d10e3214 100644 --- a/src/panels/lovelace/views/hui-view.ts +++ b/src/panels/lovelace/views/hui-view.ts @@ -233,10 +233,7 @@ export class HUIView extends ReactiveElement { if (isStrategyView(viewConfig)) { isStrategy = true; - viewConfig = await generateLovelaceViewStrategy( - viewConfig.strategy, - this.hass! - ); + viewConfig = await generateLovelaceViewStrategy(viewConfig, this.hass!); } viewConfig = {