Fix take control of the dashboard (#24800)

pull/24805/head
Paul Bottein 2025-03-27 13:33:52 +01:00 committed by GitHub
parent 098c6a2567
commit 17ef74d680
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 52 additions and 38 deletions

View File

@ -309,7 +309,7 @@ export class HcMain extends HassElement {
"../../../../src/panels/lovelace/strategies/get-strategy" "../../../../src/panels/lovelace/strategies/get-strategy"
); );
const config = await generateLovelaceDashboardStrategy( const config = await generateLovelaceDashboardStrategy(
rawConfig.strategy, rawConfig,
this.hass! this.hass!
); );
this._handleNewLovelaceConfig(config); this._handleNewLovelaceConfig(config);
@ -351,10 +351,7 @@ export class HcMain extends HassElement {
"../../../../src/panels/lovelace/strategies/get-strategy" "../../../../src/panels/lovelace/strategies/get-strategy"
); );
this._handleNewLovelaceConfig( this._handleNewLovelaceConfig(
await generateLovelaceDashboardStrategy( await generateLovelaceDashboardStrategy(DEFAULT_CONFIG, this.hass!)
DEFAULT_CONFIG.strategy,
this.hass!
)
); );
} }

View File

@ -187,7 +187,7 @@ export class LovelacePanel extends LitElement {
private async _regenerateConfig() { private async _regenerateConfig() {
const conf = await generateLovelaceDashboardStrategy( const conf = await generateLovelaceDashboardStrategy(
DEFAULT_CONFIG.strategy, DEFAULT_CONFIG,
this.hass! this.hass!
); );
this._setLovelaceConfig(conf, DEFAULT_CONFIG, "generated"); 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 // We need these to generate a dashboard, wait for them
return; return;
} }
conf = await generateLovelaceDashboardStrategy( conf = await generateLovelaceDashboardStrategy(rawConf, this.hass!);
rawConf.strategy,
this.hass!
);
} else { } else {
conf = rawConf; conf = rawConf;
} }
@ -301,7 +298,7 @@ export class LovelacePanel extends LitElement {
return; return;
} }
conf = await generateLovelaceDashboardStrategy( conf = await generateLovelaceDashboardStrategy(
DEFAULT_CONFIG.strategy, DEFAULT_CONFIG,
this.hass! this.hass!
); );
rawConf = DEFAULT_CONFIG; rawConf = DEFAULT_CONFIG;
@ -378,10 +375,7 @@ export class LovelacePanel extends LitElement {
let conf: LovelaceConfig; let conf: LovelaceConfig;
// If strategy defined, apply it here. // If strategy defined, apply it here.
if (isStrategyDashboard(newConfig)) { if (isStrategyDashboard(newConfig)) {
conf = await generateLovelaceDashboardStrategy( conf = await generateLovelaceDashboardStrategy(newConfig, this.hass!);
newConfig.strategy,
this.hass!
);
} else { } else {
conf = newConfig; conf = newConfig;
} }
@ -415,7 +409,7 @@ export class LovelacePanel extends LitElement {
try { try {
// Optimistic update // Optimistic update
const generatedConf = await generateLovelaceDashboardStrategy( const generatedConf = await generateLovelaceDashboardStrategy(
DEFAULT_CONFIG.strategy, DEFAULT_CONFIG,
this.hass! this.hass!
); );
this._updateLovelace({ this._updateLovelace({

View File

@ -185,7 +185,7 @@ export class HuiSection extends ReactiveElement {
if (isStrategySection(sectionConfig)) { if (isStrategySection(sectionConfig)) {
isStrategy = true; isStrategy = true;
sectionConfig = await generateLovelaceSectionStrategy( sectionConfig = await generateLovelaceSectionStrategy(
sectionConfig.strategy, sectionConfig,
this.hass! this.hass!
); );
} }

View File

@ -1,10 +1,18 @@
import type {
LovelaceSectionConfig,
LovelaceStrategySectionConfig,
} from "../../../data/lovelace/config/section";
import type { LovelaceStrategyConfig } from "../../../data/lovelace/config/strategy";
import type { import type {
LovelaceConfig, LovelaceConfig,
LovelaceDashboardStrategyConfig,
LovelaceRawConfig, LovelaceRawConfig,
} from "../../../data/lovelace/config/types"; } from "../../../data/lovelace/config/types";
import { isStrategyDashboard } from "../../../data/lovelace/config/types"; import { isStrategyDashboard } from "../../../data/lovelace/config/types";
import type { LovelaceStrategyConfig } from "../../../data/lovelace/config/strategy"; import type {
import type { LovelaceViewConfig } from "../../../data/lovelace/config/view"; LovelaceStrategyViewConfig,
LovelaceViewConfig,
} from "../../../data/lovelace/config/view";
import { isStrategyView } from "../../../data/lovelace/config/view"; import { isStrategyView } from "../../../data/lovelace/config/view";
import type { AsyncReturnType, HomeAssistant } from "../../../types"; import type { AsyncReturnType, HomeAssistant } from "../../../types";
import { cleanLegacyStrategyConfig, isLegacyStrategy } from "./legacy-strategy"; import { cleanLegacyStrategyConfig, isLegacyStrategy } from "./legacy-strategy";
@ -133,10 +141,11 @@ const generateStrategy = async <T extends LovelaceStrategyConfigType>(
}; };
export const generateLovelaceDashboardStrategy = async ( export const generateLovelaceDashboardStrategy = async (
strategyConfig: LovelaceStrategyConfig, config: LovelaceDashboardStrategyConfig,
hass: HomeAssistant hass: HomeAssistant
): Promise<LovelaceConfig> => ): Promise<LovelaceConfig> => {
generateStrategy( const { strategy, ...base } = config;
const generated = generateStrategy(
"dashboard", "dashboard",
(err) => ({ (err) => ({
views: [ views: [
@ -151,15 +160,21 @@ export const generateLovelaceDashboardStrategy = async (
}, },
], ],
}), }),
strategyConfig, strategy,
hass hass
); );
return {
...base,
...generated,
};
};
export const generateLovelaceViewStrategy = async ( export const generateLovelaceViewStrategy = async (
strategyConfig: LovelaceStrategyConfig, config: LovelaceStrategyViewConfig,
hass: HomeAssistant hass: HomeAssistant
): Promise<LovelaceViewConfig> => ): Promise<LovelaceViewConfig> => {
generateStrategy( const { strategy, ...base } = config;
const generated = await generateStrategy(
"view", "view",
(err) => ({ (err) => ({
cards: [ cards: [
@ -169,15 +184,21 @@ export const generateLovelaceViewStrategy = async (
}, },
], ],
}), }),
strategyConfig, strategy,
hass hass
); );
return {
...base,
...generated,
};
};
export const generateLovelaceSectionStrategy = async ( export const generateLovelaceSectionStrategy = async (
strategyConfig: LovelaceStrategyConfig, config: LovelaceStrategySectionConfig,
hass: HomeAssistant hass: HomeAssistant
): Promise<LovelaceViewConfig> => ): Promise<LovelaceSectionConfig> => {
generateStrategy( const { strategy, ...base } = config;
const generated = await generateStrategy(
"section", "section",
(err) => ({ (err) => ({
cards: [ cards: [
@ -187,9 +208,14 @@ export const generateLovelaceSectionStrategy = async (
}, },
], ],
}), }),
strategyConfig, strategy,
hass hass
); );
return {
...base,
...generated,
};
};
/** /**
* Find all references to strategies and replaces them with the generated output * Find all references to strategies and replaces them with the generated output
@ -199,20 +225,20 @@ export const expandLovelaceConfigStrategies = async (
hass: HomeAssistant hass: HomeAssistant
): Promise<LovelaceConfig> => { ): Promise<LovelaceConfig> => {
const newConfig = isStrategyDashboard(config) const newConfig = isStrategyDashboard(config)
? await generateLovelaceDashboardStrategy(config.strategy, hass) ? await generateLovelaceDashboardStrategy(config, hass)
: { ...config }; : { ...config };
newConfig.views = await Promise.all( newConfig.views = await Promise.all(
newConfig.views.map(async (view) => { newConfig.views.map(async (view) => {
const newView = isStrategyView(view) const newView = isStrategyView(view)
? await generateLovelaceViewStrategy(view.strategy, hass) ? await generateLovelaceViewStrategy(view, hass)
: { ...view }; : { ...view };
if (newView.sections) { if (newView.sections) {
newView.sections = await Promise.all( newView.sections = await Promise.all(
newView.sections.map(async (section) => { newView.sections.map(async (section) => {
const newSection = isStrategyView(section) const newSection = isStrategyView(section)
? await generateLovelaceSectionStrategy(section.strategy, hass) ? await generateLovelaceSectionStrategy(section, hass)
: { ...section }; : { ...section };
return newSection; return newSection;
}) })

View File

@ -233,10 +233,7 @@ export class HUIView extends ReactiveElement {
if (isStrategyView(viewConfig)) { if (isStrategyView(viewConfig)) {
isStrategy = true; isStrategy = true;
viewConfig = await generateLovelaceViewStrategy( viewConfig = await generateLovelaceViewStrategy(viewConfig, this.hass!);
viewConfig.strategy,
this.hass!
);
} }
viewConfig = { viewConfig = {