Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor strategy foundation #17921

Merged
merged 5 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cast/src/receiver/layout/hc-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { HassElement } from "../../../../src/state/hass-element";
import { castContext } from "../cast_context";
import "./hc-launch-screen";

const DEFAULT_STRATEGY = "original-states";

let resourcesLoaded = false;
@customElement("hc-main")
export class HcMain extends HassElement {
Expand Down Expand Up @@ -258,7 +260,7 @@ export class HcMain extends HassElement {
{
strategy: {
type: "energy",
options: { show_date_selection: true },
show_date_selection: true,
},
},
],
Expand Down Expand Up @@ -320,10 +322,10 @@ export class HcMain extends HassElement {
this._handleNewLovelaceConfig(
await generateLovelaceDashboardStrategy(
{
hass: this.hass!,
narrow: false,
type: DEFAULT_STRATEGY,
},
"original-states"
this.hass!,
{ narrow: false }
)
);
}
Expand Down
15 changes: 7 additions & 8 deletions src/data/lovelace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ export interface LovelacePanelConfig {
mode: "yaml" | "storage";
}

export type LovelaceStrategyConfig = {
type: string;
[key: string]: any;
};

export interface LovelaceConfig {
title?: string;
strategy?: {
type: string;
options?: Record<string, unknown>;
};
strategy?: LovelaceStrategyConfig;
views: LovelaceViewConfig[];
background?: string;
}
Expand Down Expand Up @@ -81,10 +83,7 @@ export interface LovelaceViewConfig {
index?: number;
title?: string;
type?: string;
strategy?: {
type: string;
options?: Record<string, unknown>;
};
strategy?: LovelaceStrategyConfig;
badges?: Array<string | LovelaceBadgeConfig>;
cards?: LovelaceCardConfig[];
path?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { ReactiveElement } from "lit";
import { customElement } from "lit/decorators";
import {
EnergyPreferences,
getEnergyPreferences,
GridSourceTypeEnergyPreference,
} from "../../../data/energy";
import { LovelaceViewConfig } from "../../../data/lovelace";
import { LovelaceViewStrategy } from "../../lovelace/strategies/get-strategy";
import {
LovelaceStrategyConfig,
LovelaceViewConfig,
} from "../../../data/lovelace";
import { HomeAssistant } from "../../../types";
import { LovelaceStrategyParams } from "../../lovelace/strategies/types";

const setupWizard = async (): Promise<LovelaceViewConfig> => {
await import("../cards/energy-setup-wizard-card");
Expand All @@ -18,12 +24,17 @@ const setupWizard = async (): Promise<LovelaceViewConfig> => {
};
};

export class EnergyStrategy {
static async generateView(
info: Parameters<LovelaceViewStrategy["generateView"]>[0]
): ReturnType<LovelaceViewStrategy["generateView"]> {
const hass = info.hass;
export interface EnergeryViewStrategyConfig extends LovelaceStrategyConfig {
show_date_selection?: boolean;
}

@customElement("energy-view-strategy")
export class EnergyViewStrategy extends ReactiveElement {
static async generate(
config: EnergeryViewStrategyConfig,
hass: HomeAssistant,
params: LovelaceStrategyParams
): Promise<LovelaceViewConfig> {
const view: LovelaceViewConfig = { cards: [] };

let prefs: EnergyPreferences;
Expand Down Expand Up @@ -56,7 +67,7 @@ export class EnergyStrategy {
(source) => source.type === "water"
);

if (info.narrow || info.view.strategy?.options?.show_date_selection) {
if (params.narrow || config.show_date_selection) {
view.cards!.push({
type: "energy-date-selection",
collection_key: "energy_dashboard",
Expand Down
6 changes: 2 additions & 4 deletions src/panels/lovelace/editor/hui-dialog-save-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,8 @@ export class HuiSaveConfig extends LitElement implements HassDialog {
await lovelace.saveConfig(
this._emptyConfig
? EMPTY_CONFIG
: await expandLovelaceConfigStrategies({
config: lovelace.config,
hass: this.hass!,
narrow: this._params!.narrow,
: await expandLovelaceConfigStrategies(lovelace.config, this.hass, {
narrow: this._params.narrow,
})
);
lovelace.setEditMode(true);
Expand Down
38 changes: 19 additions & 19 deletions src/panels/lovelace/ha-panel-lovelace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ export class LovelacePanel extends LitElement {
private async _regenerateConfig() {
const conf = await generateLovelaceDashboardStrategy(
{
hass: this.hass!,
narrow: this.narrow,
type: DEFAULT_STRATEGY,
},
DEFAULT_STRATEGY
this.hass!,
{ narrow: this.narrow }
);
this._setLovelaceConfig(conf, undefined, "generated");
this._panelState = "loaded";
Expand Down Expand Up @@ -256,11 +256,11 @@ export class LovelacePanel extends LitElement {

// If strategy defined, apply it here.
if (rawConf.strategy) {
conf = await generateLovelaceDashboardStrategy({
config: rawConf,
hass: this.hass!,
narrow: this.narrow,
});
conf = await generateLovelaceDashboardStrategy(
rawConf.strategy,
this.hass!,
{ narrow: this.narrow }
);
} else {
conf = rawConf;
}
Expand All @@ -274,10 +274,10 @@ export class LovelacePanel extends LitElement {
}
conf = await generateLovelaceDashboardStrategy(
{
hass: this.hass!,
narrow: this.narrow,
type: DEFAULT_STRATEGY,
},
DEFAULT_STRATEGY
this.hass!,
{ narrow: this.narrow }
);
confMode = "generated";
} finally {
Expand Down Expand Up @@ -363,11 +363,11 @@ export class LovelacePanel extends LitElement {
let conf: LovelaceConfig;
// If strategy defined, apply it here.
if (newConfig.strategy) {
conf = await generateLovelaceDashboardStrategy({
config: newConfig,
hass: this.hass!,
narrow: this.narrow,
});
conf = await generateLovelaceDashboardStrategy(
newConfig.strategy,
this.hass!,
{ narrow: this.narrow }
);
} else {
conf = newConfig;
}
Expand Down Expand Up @@ -402,10 +402,10 @@ export class LovelacePanel extends LitElement {
// Optimistic update
const generatedConf = await generateLovelaceDashboardStrategy(
{
hass: this.hass!,
narrow: this.narrow,
type: DEFAULT_STRATEGY,
},
DEFAULT_STRATEGY
this.hass!,
{ narrow: this.narrow }
);
this._updateLovelace({
config: generatedConf,
Expand Down
Loading