Fix take control of the dashboard (#24800)

This commit is contained in:
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"
);
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!)
);
}

View File

@ -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({

View File

@ -185,7 +185,7 @@ export class HuiSection extends ReactiveElement {
if (isStrategySection(sectionConfig)) {
isStrategy = true;
sectionConfig = await generateLovelaceSectionStrategy(
sectionConfig.strategy,
sectionConfig,
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 {
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 <T extends LovelaceStrategyConfigType>(
};
export const generateLovelaceDashboardStrategy = async (
strategyConfig: LovelaceStrategyConfig,
config: LovelaceDashboardStrategyConfig,
hass: HomeAssistant
): Promise<LovelaceConfig> =>
generateStrategy(
): Promise<LovelaceConfig> => {
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<LovelaceViewConfig> =>
generateStrategy(
): Promise<LovelaceViewConfig> => {
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<LovelaceViewConfig> =>
generateStrategy(
): Promise<LovelaceSectionConfig> => {
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<LovelaceConfig> => {
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;
})

View File

@ -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 = {