Skip to content

Commit

Permalink
Remove narrow from strategies (#18201)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkragten authored Oct 16, 2023
1 parent 02d9786 commit 4b885cb
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 54 deletions.
3 changes: 1 addition & 2 deletions cast/src/receiver/layout/hc-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,7 @@ export class HcMain extends HassElement {
{
type: DEFAULT_STRATEGY,
},
this.hass!,
{ narrow: false }
this.hass!
)
);
}
Expand Down
4 changes: 1 addition & 3 deletions src/panels/lovelace/editor/hui-dialog-save-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ export class HuiSaveConfig extends LitElement implements HassDialog {
await lovelace.saveConfig(
this._emptyConfig
? EMPTY_CONFIG
: await expandLovelaceConfigStrategies(lovelace.config, this.hass, {
narrow: this._params.narrow,
})
: await expandLovelaceConfigStrategies(lovelace.config, this.hass)
);
lovelace.setEditMode(true);
this._saving = false;
Expand Down
15 changes: 5 additions & 10 deletions src/panels/lovelace/ha-panel-lovelace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ export class LovelacePanel extends LitElement {
{
type: DEFAULT_STRATEGY,
},
this.hass!,
{ narrow: this.narrow }
this.hass!
);
this._setLovelaceConfig(conf, undefined, "generated");
this._panelState = "loaded";
Expand Down Expand Up @@ -258,8 +257,7 @@ export class LovelacePanel extends LitElement {
if (rawConf.strategy) {
conf = await generateLovelaceDashboardStrategy(
rawConf.strategy,
this.hass!,
{ narrow: this.narrow }
this.hass!
);
} else {
conf = rawConf;
Expand All @@ -276,8 +274,7 @@ export class LovelacePanel extends LitElement {
{
type: DEFAULT_STRATEGY,
},
this.hass!,
{ narrow: this.narrow }
this.hass!
);
confMode = "generated";
} finally {
Expand Down Expand Up @@ -365,8 +362,7 @@ export class LovelacePanel extends LitElement {
if (newConfig.strategy) {
conf = await generateLovelaceDashboardStrategy(
newConfig.strategy,
this.hass!,
{ narrow: this.narrow }
this.hass!
);
} else {
conf = newConfig;
Expand Down Expand Up @@ -404,8 +400,7 @@ export class LovelacePanel extends LitElement {
{
type: DEFAULT_STRATEGY,
},
this.hass!,
{ narrow: this.narrow }
this.hass!
);
this._updateLovelace({
config: generatedConf,
Expand Down
29 changes: 9 additions & 20 deletions src/panels/lovelace/strategies/get-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { isLegacyStrategy } from "./legacy-strategy";
import {
LovelaceDashboardStrategy,
LovelaceStrategy,
LovelaceStrategyParams,
LovelaceViewStrategy,
} from "./types";

Expand Down Expand Up @@ -77,8 +76,7 @@ const generateStrategy = async <T extends LovelaceStrategyConfigType>(
configType: T,
renderError: (err: string | Error) => StrategyConfig<T>,
strategyConfig: LovelaceStrategyConfig,
hass: HomeAssistant,
params: LovelaceStrategyParams
hass: HomeAssistant
): Promise<StrategyConfig<T>> => {
const strategyType = strategyConfig.type;
if (!strategyType) {
Expand All @@ -95,15 +93,13 @@ const generateStrategy = async <T extends LovelaceStrategyConfigType>(
return (await strategy.generateDashboard({
config: { strategy: strategyConfig, views: [] },
hass,
narrow: params.narrow,
})) as StrategyConfig<T>;
}
if (configType === "view" && "generateView" in strategy) {
return (await strategy.generateView({
config: { views: [] },
view: { strategy: strategyConfig },
hass,
narrow: params.narrow,
})) as StrategyConfig<T>;
}
}
Expand All @@ -115,7 +111,7 @@ const generateStrategy = async <T extends LovelaceStrategyConfigType>(

delete config.options;

return await strategy.generate(config, hass, params);
return await strategy.generate(config, hass);
} catch (err: any) {
if (err.message !== "timeout") {
// eslint-disable-next-line
Expand All @@ -128,8 +124,7 @@ const generateStrategy = async <T extends LovelaceStrategyConfigType>(

export const generateLovelaceDashboardStrategy = async (
strategyConfig: LovelaceStrategyConfig,
hass: HomeAssistant,
params: LovelaceStrategyParams
hass: HomeAssistant
): Promise<LovelaceConfig> =>
generateStrategy(
"dashboard",
Expand All @@ -147,14 +142,12 @@ export const generateLovelaceDashboardStrategy = async (
],
}),
strategyConfig,
hass,
params
hass
);

export const generateLovelaceViewStrategy = async (
strategyConfig: LovelaceStrategyConfig,
hass: HomeAssistant,
params: LovelaceStrategyParams
hass: HomeAssistant
): Promise<LovelaceViewConfig> =>
generateStrategy(
"view",
Expand All @@ -167,27 +160,23 @@ export const generateLovelaceViewStrategy = async (
],
}),
strategyConfig,
hass,
params
hass
);

/**
* Find all references to strategies and replaces them with the generated output
*/
export const expandLovelaceConfigStrategies = async (
config: LovelaceConfig,
hass: HomeAssistant,
params: LovelaceStrategyParams
hass: HomeAssistant
): Promise<LovelaceConfig> => {
const newConfig = config.strategy
? await generateLovelaceDashboardStrategy(config.strategy, hass, params)
? await generateLovelaceDashboardStrategy(config.strategy, hass)
: { ...config };

newConfig.views = await Promise.all(
newConfig.views.map((view) =>
view.strategy
? generateLovelaceViewStrategy(view.strategy, hass, params)
: view
view.strategy ? generateLovelaceViewStrategy(view.strategy, hass) : view
)
);

Expand Down
2 changes: 0 additions & 2 deletions src/panels/lovelace/strategies/legacy-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export interface LovelaceDashboardStrategy {
generateDashboard(info: {
config?: LovelaceConfig;
hass: HomeAssistant;
narrow: boolean | undefined;
}): Promise<LovelaceConfig>;
}

Expand All @@ -19,6 +18,5 @@ export interface LovelaceViewStrategy {
view: LovelaceViewConfig;
config: LovelaceConfig;
hass: HomeAssistant;
narrow: boolean | undefined;
}): Promise<LovelaceViewConfig>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import { ReactiveElement } from "lit";
import { customElement } from "lit/decorators";
import { LovelaceConfig, LovelaceStrategyConfig } from "../../../data/lovelace";
import { HomeAssistant } from "../../../types";
import { LovelaceStrategyParams } from "./types";

@customElement("original-states-dashboard-strategy")
export class OriginalStatesDashboardStrategy extends ReactiveElement {
static async generate(
_config: LovelaceStrategyConfig,
hass: HomeAssistant,
_params?: LovelaceStrategyParams
hass: HomeAssistant
): Promise<LovelaceConfig> {
return {
title: hass.config.location_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import {
} from "../../../data/lovelace";
import { HomeAssistant } from "../../../types";
import { generateDefaultViewConfig } from "../common/generate-lovelace-config";
import { LovelaceStrategyParams } from "./types";

@customElement("original-states-view-strategy")
export class OriginalStatesViewStrategy extends ReactiveElement {
static async generate(
_config: LovelaceStrategyConfig,
hass: HomeAssistant,
_params?: LovelaceStrategyParams
hass: HomeAssistant
): Promise<LovelaceViewConfig> {
if (hass.config.state === STATE_NOT_RUNNING) {
return {
Expand Down
10 changes: 1 addition & 9 deletions src/panels/lovelace/strategies/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@ import {
} from "../../../data/lovelace";
import { HomeAssistant } from "../../../types";

export type LovelaceStrategyParams = {
narrow?: boolean;
};

export type LovelaceStrategy<T = any> = {
generate(
config: LovelaceStrategyConfig,
hass: HomeAssistant,
params?: LovelaceStrategyParams
): Promise<T>;
generate(config: LovelaceStrategyConfig, hass: HomeAssistant): Promise<T>;
};

export interface LovelaceDashboardStrategy
Expand Down
3 changes: 1 addition & 2 deletions src/panels/lovelace/views/hui-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ export class HUIView extends ReactiveElement {
isStrategy = true;
viewConfig = await generateLovelaceViewStrategy(
viewConfig.strategy,
this.hass!,
{ narrow: this.narrow }
this.hass!
);
}

Expand Down

0 comments on commit 4b885cb

Please sign in to comment.