mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-31 13:07:49 +00:00
Use responsive condition in energy dashboard
This commit is contained in:
parent
12cb94cdc0
commit
5270ca4db6
@ -324,8 +324,7 @@ export class HcMain extends HassElement {
|
|||||||
{
|
{
|
||||||
type: DEFAULT_STRATEGY,
|
type: DEFAULT_STRATEGY,
|
||||||
},
|
},
|
||||||
this.hass!,
|
this.hass!
|
||||||
{ narrow: false }
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
1
src/common/util/media_query.ts
Normal file
1
src/common/util/media_query.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export const NARROW_MEDIA_QUERY = `(max-width: 870px)`;
|
@ -16,6 +16,7 @@ import "../components/ha-drawer";
|
|||||||
import { showNotificationDrawer } from "../dialogs/notifications/show-notification-drawer";
|
import { showNotificationDrawer } from "../dialogs/notifications/show-notification-drawer";
|
||||||
import type { HomeAssistant, Route } from "../types";
|
import type { HomeAssistant, Route } from "../types";
|
||||||
import "./partial-panel-resolver";
|
import "./partial-panel-resolver";
|
||||||
|
import { NARROW_MEDIA_QUERY } from "../common/util/media_query";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
// for fire event
|
// for fire event
|
||||||
@ -50,7 +51,7 @@ export class HomeAssistantMain extends LitElement {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
listenMediaQuery("(max-width: 870px)", (matches) => {
|
listenMediaQuery(NARROW_MEDIA_QUERY, (matches) => {
|
||||||
this.narrow = matches;
|
this.narrow = matches;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { ReactiveElement } from "lit";
|
import { ReactiveElement } from "lit";
|
||||||
import { customElement } from "lit/decorators";
|
import { customElement } from "lit/decorators";
|
||||||
|
import { NARROW_MEDIA_QUERY } from "../../../common/util/media_query";
|
||||||
import {
|
import {
|
||||||
EnergyPreferences,
|
EnergyPreferences,
|
||||||
getEnergyPreferences,
|
getEnergyPreferences,
|
||||||
@ -10,7 +11,6 @@ import {
|
|||||||
LovelaceViewConfig,
|
LovelaceViewConfig,
|
||||||
} from "../../../data/lovelace";
|
} from "../../../data/lovelace";
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import { LovelaceStrategyParams } from "../../lovelace/strategies/types";
|
|
||||||
|
|
||||||
const setupWizard = async (): Promise<LovelaceViewConfig> => {
|
const setupWizard = async (): Promise<LovelaceViewConfig> => {
|
||||||
await import("../cards/energy-setup-wizard-card");
|
await import("../cards/energy-setup-wizard-card");
|
||||||
@ -32,8 +32,7 @@ export interface EnergeryViewStrategyConfig extends LovelaceStrategyConfig {
|
|||||||
export class EnergyViewStrategy extends ReactiveElement {
|
export class EnergyViewStrategy extends ReactiveElement {
|
||||||
static async generate(
|
static async generate(
|
||||||
config: EnergeryViewStrategyConfig,
|
config: EnergeryViewStrategyConfig,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant
|
||||||
params: LovelaceStrategyParams
|
|
||||||
): Promise<LovelaceViewConfig> {
|
): Promise<LovelaceViewConfig> {
|
||||||
const view: LovelaceViewConfig = { cards: [] };
|
const view: LovelaceViewConfig = { cards: [] };
|
||||||
|
|
||||||
@ -67,11 +66,24 @@ export class EnergyViewStrategy extends ReactiveElement {
|
|||||||
(source) => source.type === "water"
|
(source) => source.type === "water"
|
||||||
);
|
);
|
||||||
|
|
||||||
if (params.narrow || config.show_date_selection) {
|
const dateSelectionCard = {
|
||||||
|
type: "energy-date-selection",
|
||||||
|
collection_key: "energy_dashboard",
|
||||||
|
view_layout: { position: "sidebar" },
|
||||||
|
};
|
||||||
|
|
||||||
|
if (config.show_date_selection) {
|
||||||
|
view.cards!.push(dateSelectionCard);
|
||||||
|
} else {
|
||||||
view.cards!.push({
|
view.cards!.push({
|
||||||
type: "energy-date-selection",
|
type: "conditional",
|
||||||
collection_key: "energy_dashboard",
|
conditions: [
|
||||||
view_layout: { position: "sidebar" },
|
{
|
||||||
|
condition: "responsive",
|
||||||
|
media_query: NARROW_MEDIA_QUERY,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
card: dateSelectionCard,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,9 +174,7 @@ export class HuiSaveConfig extends LitElement implements HassDialog {
|
|||||||
await lovelace.saveConfig(
|
await lovelace.saveConfig(
|
||||||
this._emptyConfig
|
this._emptyConfig
|
||||||
? EMPTY_CONFIG
|
? EMPTY_CONFIG
|
||||||
: await expandLovelaceConfigStrategies(lovelace.config, this.hass, {
|
: await expandLovelaceConfigStrategies(lovelace.config, this.hass)
|
||||||
narrow: this._params.narrow,
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
lovelace.setEditMode(true);
|
lovelace.setEditMode(true);
|
||||||
this._saving = false;
|
this._saving = false;
|
||||||
|
@ -167,8 +167,7 @@ export class LovelacePanel extends LitElement {
|
|||||||
{
|
{
|
||||||
type: DEFAULT_STRATEGY,
|
type: DEFAULT_STRATEGY,
|
||||||
},
|
},
|
||||||
this.hass!,
|
this.hass!
|
||||||
{ narrow: this.narrow }
|
|
||||||
);
|
);
|
||||||
this._setLovelaceConfig(conf, undefined, "generated");
|
this._setLovelaceConfig(conf, undefined, "generated");
|
||||||
this._panelState = "loaded";
|
this._panelState = "loaded";
|
||||||
@ -258,8 +257,7 @@ export class LovelacePanel extends LitElement {
|
|||||||
if (rawConf.strategy) {
|
if (rawConf.strategy) {
|
||||||
conf = await generateLovelaceDashboardStrategy(
|
conf = await generateLovelaceDashboardStrategy(
|
||||||
rawConf.strategy,
|
rawConf.strategy,
|
||||||
this.hass!,
|
this.hass!
|
||||||
{ narrow: this.narrow }
|
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
conf = rawConf;
|
conf = rawConf;
|
||||||
@ -276,8 +274,7 @@ export class LovelacePanel extends LitElement {
|
|||||||
{
|
{
|
||||||
type: DEFAULT_STRATEGY,
|
type: DEFAULT_STRATEGY,
|
||||||
},
|
},
|
||||||
this.hass!,
|
this.hass!
|
||||||
{ narrow: this.narrow }
|
|
||||||
);
|
);
|
||||||
confMode = "generated";
|
confMode = "generated";
|
||||||
} finally {
|
} finally {
|
||||||
@ -365,8 +362,7 @@ export class LovelacePanel extends LitElement {
|
|||||||
if (newConfig.strategy) {
|
if (newConfig.strategy) {
|
||||||
conf = await generateLovelaceDashboardStrategy(
|
conf = await generateLovelaceDashboardStrategy(
|
||||||
newConfig.strategy,
|
newConfig.strategy,
|
||||||
this.hass!,
|
this.hass!
|
||||||
{ narrow: this.narrow }
|
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
conf = newConfig;
|
conf = newConfig;
|
||||||
@ -404,8 +400,7 @@ export class LovelacePanel extends LitElement {
|
|||||||
{
|
{
|
||||||
type: DEFAULT_STRATEGY,
|
type: DEFAULT_STRATEGY,
|
||||||
},
|
},
|
||||||
this.hass!,
|
this.hass!
|
||||||
{ narrow: this.narrow }
|
|
||||||
);
|
);
|
||||||
this._updateLovelace({
|
this._updateLovelace({
|
||||||
config: generatedConf,
|
config: generatedConf,
|
||||||
|
@ -8,7 +8,6 @@ import { isLegacyStrategy } from "./legacy-strategy";
|
|||||||
import {
|
import {
|
||||||
LovelaceDashboardStrategy,
|
LovelaceDashboardStrategy,
|
||||||
LovelaceStrategy,
|
LovelaceStrategy,
|
||||||
LovelaceStrategyParams,
|
|
||||||
LovelaceViewStrategy,
|
LovelaceViewStrategy,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
@ -77,8 +76,7 @@ const generateStrategy = async <T extends LovelaceStrategyConfigType>(
|
|||||||
configType: T,
|
configType: T,
|
||||||
renderError: (err: string | Error) => StrategyConfig<T>,
|
renderError: (err: string | Error) => StrategyConfig<T>,
|
||||||
strategyConfig: LovelaceStrategyConfig,
|
strategyConfig: LovelaceStrategyConfig,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant
|
||||||
params: LovelaceStrategyParams
|
|
||||||
): Promise<StrategyConfig<T>> => {
|
): Promise<StrategyConfig<T>> => {
|
||||||
const strategyType = strategyConfig.type;
|
const strategyType = strategyConfig.type;
|
||||||
if (!strategyType) {
|
if (!strategyType) {
|
||||||
@ -95,7 +93,7 @@ const generateStrategy = async <T extends LovelaceStrategyConfigType>(
|
|||||||
return (await strategy.generateDashboard({
|
return (await strategy.generateDashboard({
|
||||||
config: { strategy: strategyConfig, views: [] },
|
config: { strategy: strategyConfig, views: [] },
|
||||||
hass,
|
hass,
|
||||||
narrow: params.narrow,
|
narrow: undefined,
|
||||||
})) as StrategyConfig<T>;
|
})) as StrategyConfig<T>;
|
||||||
}
|
}
|
||||||
if (configType === "view" && "generateView" in strategy) {
|
if (configType === "view" && "generateView" in strategy) {
|
||||||
@ -103,7 +101,7 @@ const generateStrategy = async <T extends LovelaceStrategyConfigType>(
|
|||||||
config: { views: [] },
|
config: { views: [] },
|
||||||
view: { strategy: strategyConfig },
|
view: { strategy: strategyConfig },
|
||||||
hass,
|
hass,
|
||||||
narrow: params.narrow,
|
narrow: undefined,
|
||||||
})) as StrategyConfig<T>;
|
})) as StrategyConfig<T>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -115,7 +113,7 @@ const generateStrategy = async <T extends LovelaceStrategyConfigType>(
|
|||||||
|
|
||||||
delete config.options;
|
delete config.options;
|
||||||
|
|
||||||
return await strategy.generate(config, hass, params);
|
return await strategy.generate(config, hass);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
if (err.message !== "timeout") {
|
if (err.message !== "timeout") {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
@ -128,8 +126,7 @@ const generateStrategy = async <T extends LovelaceStrategyConfigType>(
|
|||||||
|
|
||||||
export const generateLovelaceDashboardStrategy = async (
|
export const generateLovelaceDashboardStrategy = async (
|
||||||
strategyConfig: LovelaceStrategyConfig,
|
strategyConfig: LovelaceStrategyConfig,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant
|
||||||
params: LovelaceStrategyParams
|
|
||||||
): Promise<LovelaceConfig> =>
|
): Promise<LovelaceConfig> =>
|
||||||
generateStrategy(
|
generateStrategy(
|
||||||
"dashboard",
|
"dashboard",
|
||||||
@ -147,14 +144,12 @@ export const generateLovelaceDashboardStrategy = async (
|
|||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
strategyConfig,
|
strategyConfig,
|
||||||
hass,
|
hass
|
||||||
params
|
|
||||||
);
|
);
|
||||||
|
|
||||||
export const generateLovelaceViewStrategy = async (
|
export const generateLovelaceViewStrategy = async (
|
||||||
strategyConfig: LovelaceStrategyConfig,
|
strategyConfig: LovelaceStrategyConfig,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant
|
||||||
params: LovelaceStrategyParams
|
|
||||||
): Promise<LovelaceViewConfig> =>
|
): Promise<LovelaceViewConfig> =>
|
||||||
generateStrategy(
|
generateStrategy(
|
||||||
"view",
|
"view",
|
||||||
@ -167,8 +162,7 @@ export const generateLovelaceViewStrategy = async (
|
|||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
strategyConfig,
|
strategyConfig,
|
||||||
hass,
|
hass
|
||||||
params
|
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -176,18 +170,15 @@ export const generateLovelaceViewStrategy = async (
|
|||||||
*/
|
*/
|
||||||
export const expandLovelaceConfigStrategies = async (
|
export const expandLovelaceConfigStrategies = async (
|
||||||
config: LovelaceConfig,
|
config: LovelaceConfig,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant
|
||||||
params: LovelaceStrategyParams
|
|
||||||
): Promise<LovelaceConfig> => {
|
): Promise<LovelaceConfig> => {
|
||||||
const newConfig = config.strategy
|
const newConfig = config.strategy
|
||||||
? await generateLovelaceDashboardStrategy(config.strategy, hass, params)
|
? await generateLovelaceDashboardStrategy(config.strategy, hass)
|
||||||
: { ...config };
|
: { ...config };
|
||||||
|
|
||||||
newConfig.views = await Promise.all(
|
newConfig.views = await Promise.all(
|
||||||
newConfig.views.map((view) =>
|
newConfig.views.map((view) =>
|
||||||
view.strategy
|
view.strategy ? generateLovelaceViewStrategy(view.strategy, hass) : view
|
||||||
? generateLovelaceViewStrategy(view.strategy, hass, params)
|
|
||||||
: view
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -2,14 +2,12 @@ import { ReactiveElement } from "lit";
|
|||||||
import { customElement } from "lit/decorators";
|
import { customElement } from "lit/decorators";
|
||||||
import { LovelaceConfig, LovelaceStrategyConfig } from "../../../data/lovelace";
|
import { LovelaceConfig, LovelaceStrategyConfig } from "../../../data/lovelace";
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import { LovelaceStrategyParams } from "./types";
|
|
||||||
|
|
||||||
@customElement("original-states-dashboard-strategy")
|
@customElement("original-states-dashboard-strategy")
|
||||||
export class OriginalStatesDashboardStrategy extends ReactiveElement {
|
export class OriginalStatesDashboardStrategy extends ReactiveElement {
|
||||||
static async generate(
|
static async generate(
|
||||||
_config: LovelaceStrategyConfig,
|
_config: LovelaceStrategyConfig,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant
|
||||||
_params?: LovelaceStrategyParams
|
|
||||||
): Promise<LovelaceConfig> {
|
): Promise<LovelaceConfig> {
|
||||||
return {
|
return {
|
||||||
title: hass.config.location_name,
|
title: hass.config.location_name,
|
||||||
|
@ -9,14 +9,12 @@ import {
|
|||||||
} from "../../../data/lovelace";
|
} from "../../../data/lovelace";
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import { generateDefaultViewConfig } from "../common/generate-lovelace-config";
|
import { generateDefaultViewConfig } from "../common/generate-lovelace-config";
|
||||||
import { LovelaceStrategyParams } from "./types";
|
|
||||||
|
|
||||||
@customElement("original-states-view-strategy")
|
@customElement("original-states-view-strategy")
|
||||||
export class OriginalStatesViewStrategy extends ReactiveElement {
|
export class OriginalStatesViewStrategy extends ReactiveElement {
|
||||||
static async generate(
|
static async generate(
|
||||||
_config: LovelaceStrategyConfig,
|
_config: LovelaceStrategyConfig,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant
|
||||||
_params?: LovelaceStrategyParams
|
|
||||||
): Promise<LovelaceViewConfig> {
|
): Promise<LovelaceViewConfig> {
|
||||||
if (hass.config.state === STATE_NOT_RUNNING) {
|
if (hass.config.state === STATE_NOT_RUNNING) {
|
||||||
return {
|
return {
|
||||||
|
@ -5,16 +5,8 @@ import {
|
|||||||
} from "../../../data/lovelace";
|
} from "../../../data/lovelace";
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
|
|
||||||
export type LovelaceStrategyParams = {
|
|
||||||
narrow?: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type LovelaceStrategy<T = any> = {
|
export type LovelaceStrategy<T = any> = {
|
||||||
generate(
|
generate(config: LovelaceStrategyConfig, hass: HomeAssistant): Promise<T>;
|
||||||
config: LovelaceStrategyConfig,
|
|
||||||
hass: HomeAssistant,
|
|
||||||
params?: LovelaceStrategyParams
|
|
||||||
): Promise<T>;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface LovelaceDashboardStrategy
|
export interface LovelaceDashboardStrategy
|
||||||
|
@ -192,8 +192,7 @@ export class HUIView extends ReactiveElement {
|
|||||||
isStrategy = true;
|
isStrategy = true;
|
||||||
viewConfig = await generateLovelaceViewStrategy(
|
viewConfig = await generateLovelaceViewStrategy(
|
||||||
viewConfig.strategy,
|
viewConfig.strategy,
|
||||||
this.hass!,
|
this.hass!
|
||||||
{ narrow: this.narrow }
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user