mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-19 07:16:39 +00:00
Refactor suggest card function (#19528)
This commit is contained in:
parent
75bbc33fa2
commit
973752b974
@ -27,6 +27,7 @@ import { addEntitiesToLovelaceView } from "../../../lovelace/editor/add-entities
|
|||||||
import type { LovelaceRowConfig } from "../../../lovelace/entity-rows/types";
|
import type { LovelaceRowConfig } from "../../../lovelace/entity-rows/types";
|
||||||
import { LovelaceRow } from "../../../lovelace/entity-rows/types";
|
import { LovelaceRow } from "../../../lovelace/entity-rows/types";
|
||||||
import { EntityRegistryStateEntry } from "../ha-config-device-page";
|
import { EntityRegistryStateEntry } from "../ha-config-device-page";
|
||||||
|
import { computeCards } from "../../../lovelace/common/generate-lovelace-config";
|
||||||
|
|
||||||
@customElement("ha-device-entities-card")
|
@customElement("ha-device-entities-card")
|
||||||
export class HaDeviceEntitiesCard extends LitElement {
|
export class HaDeviceEntitiesCard extends LitElement {
|
||||||
@ -224,13 +225,17 @@ export class HaDeviceEntitiesCard extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _addToLovelaceView(): void {
|
private _addToLovelaceView(): void {
|
||||||
|
const entities = this.entities
|
||||||
|
.filter((entity) => !entity.disabled_by)
|
||||||
|
.map((entity) => entity.entity_id);
|
||||||
|
|
||||||
addEntitiesToLovelaceView(
|
addEntitiesToLovelaceView(
|
||||||
this,
|
this,
|
||||||
this.hass,
|
this.hass,
|
||||||
this.entities
|
computeCards(this.hass.states, entities, {
|
||||||
.filter((entity) => !entity.disabled_by)
|
title: this.deviceName,
|
||||||
.map((entity) => entity.entity_id),
|
}),
|
||||||
this.deviceName
|
entities
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { LovelacePanelConfig } from "../../../data/lovelace";
|
import { LovelacePanelConfig } from "../../../data/lovelace";
|
||||||
|
import { LovelaceCardConfig } from "../../../data/lovelace/config/card";
|
||||||
import {
|
import {
|
||||||
LovelaceConfig,
|
LovelaceConfig,
|
||||||
fetchConfig,
|
fetchConfig,
|
||||||
@ -13,8 +14,8 @@ import { showSelectViewDialog } from "./select-view/show-select-view-dialog";
|
|||||||
export const addEntitiesToLovelaceView = async (
|
export const addEntitiesToLovelaceView = async (
|
||||||
element: HTMLElement,
|
element: HTMLElement,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entities: string[],
|
cardConfig: LovelaceCardConfig[],
|
||||||
cardTitle?: string
|
entities?: string[]
|
||||||
) => {
|
) => {
|
||||||
hass.loadFragmentTranslation("lovelace");
|
hass.loadFragmentTranslation("lovelace");
|
||||||
const dashboards = await fetchDashboards(hass);
|
const dashboards = await fetchDashboards(hass);
|
||||||
@ -30,9 +31,9 @@ export const addEntitiesToLovelaceView = async (
|
|||||||
if (mainLovelaceMode !== "storage" && !storageDashs.length) {
|
if (mainLovelaceMode !== "storage" && !storageDashs.length) {
|
||||||
// no storage dashboards, just show the YAML config
|
// no storage dashboards, just show the YAML config
|
||||||
showSuggestCardDialog(element, {
|
showSuggestCardDialog(element, {
|
||||||
|
cardConfig,
|
||||||
entities,
|
entities,
|
||||||
yaml: true,
|
yaml: true,
|
||||||
cardTitle,
|
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -69,9 +70,9 @@ export const addEntitiesToLovelaceView = async (
|
|||||||
if (dashboards.length > storageDashs.length) {
|
if (dashboards.length > storageDashs.length) {
|
||||||
// all storage dashboards are generated, but we have YAML dashboards just show the YAML config
|
// all storage dashboards are generated, but we have YAML dashboards just show the YAML config
|
||||||
showSuggestCardDialog(element, {
|
showSuggestCardDialog(element, {
|
||||||
|
cardConfig,
|
||||||
entities,
|
entities,
|
||||||
yaml: true,
|
yaml: true,
|
||||||
cardTitle,
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// all storage dashboards are generated
|
// all storage dashboards are generated
|
||||||
@ -91,7 +92,7 @@ export const addEntitiesToLovelaceView = async (
|
|||||||
|
|
||||||
if (!storageDashs.length && lovelaceConfig.views.length === 1) {
|
if (!storageDashs.length && lovelaceConfig.views.length === 1) {
|
||||||
showSuggestCardDialog(element, {
|
showSuggestCardDialog(element, {
|
||||||
cardTitle,
|
cardConfig,
|
||||||
lovelaceConfig: lovelaceConfig!,
|
lovelaceConfig: lovelaceConfig!,
|
||||||
saveConfig: async (newConfig: LovelaceConfig): Promise<void> => {
|
saveConfig: async (newConfig: LovelaceConfig): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
@ -114,7 +115,7 @@ export const addEntitiesToLovelaceView = async (
|
|||||||
dashboards,
|
dashboards,
|
||||||
viewSelectedCallback: (newUrlPath, selectedDashConfig, viewIndex) => {
|
viewSelectedCallback: (newUrlPath, selectedDashConfig, viewIndex) => {
|
||||||
showSuggestCardDialog(element, {
|
showSuggestCardDialog(element, {
|
||||||
cardTitle,
|
cardConfig,
|
||||||
lovelaceConfig: selectedDashConfig,
|
lovelaceConfig: selectedDashConfig,
|
||||||
saveConfig: async (newConfig: LovelaceConfig): Promise<void> => {
|
saveConfig: async (newConfig: LovelaceConfig): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
|
@ -8,7 +8,6 @@ import { LovelaceCardConfig } from "../../../../data/lovelace/config/card";
|
|||||||
import { haStyleDialog } from "../../../../resources/styles";
|
import { haStyleDialog } from "../../../../resources/styles";
|
||||||
import { HomeAssistant } from "../../../../types";
|
import { HomeAssistant } from "../../../../types";
|
||||||
import { showSaveSuccessToast } from "../../../../util/toast-saved-success";
|
import { showSaveSuccessToast } from "../../../../util/toast-saved-success";
|
||||||
import { computeCards } from "../../common/generate-lovelace-config";
|
|
||||||
import { addCards } from "../config-util";
|
import { addCards } from "../config-util";
|
||||||
import "./hui-card-preview";
|
import "./hui-card-preview";
|
||||||
import { showCreateCardDialog } from "./show-create-card-dialog";
|
import { showCreateCardDialog } from "./show-create-card-dialog";
|
||||||
@ -28,11 +27,7 @@ export class HuiDialogSuggestCard extends LitElement {
|
|||||||
|
|
||||||
public showDialog(params: SuggestCardDialogParams): void {
|
public showDialog(params: SuggestCardDialogParams): void {
|
||||||
this._params = params;
|
this._params = params;
|
||||||
this._cardConfig =
|
this._cardConfig = params.cardConfig;
|
||||||
params.cardConfig ||
|
|
||||||
computeCards(this.hass.states, params.entities, {
|
|
||||||
title: params.cardTitle,
|
|
||||||
});
|
|
||||||
if (!Object.isFrozen(this._cardConfig)) {
|
if (!Object.isFrozen(this._cardConfig)) {
|
||||||
this._cardConfig = deepFreeze(this._cardConfig);
|
this._cardConfig = deepFreeze(this._cardConfig);
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,11 @@ import { LovelaceCardConfig } from "../../../../data/lovelace/config/card";
|
|||||||
import { LovelaceConfig } from "../../../../data/lovelace/config/types";
|
import { LovelaceConfig } from "../../../../data/lovelace/config/types";
|
||||||
|
|
||||||
export interface SuggestCardDialogParams {
|
export interface SuggestCardDialogParams {
|
||||||
cardTitle?: string;
|
|
||||||
lovelaceConfig?: LovelaceConfig;
|
lovelaceConfig?: LovelaceConfig;
|
||||||
yaml?: boolean;
|
yaml?: boolean;
|
||||||
saveConfig?: (config: LovelaceConfig) => void;
|
saveConfig?: (config: LovelaceConfig) => void;
|
||||||
path?: [number];
|
path?: [number];
|
||||||
entities: string[]; // We can pass entity id's that will be added to the config when a card is picked
|
entities?: string[]; // Entities used to generate the card config. We pass this to create dialog when user chooses "Pick own"
|
||||||
cardConfig?: LovelaceCardConfig[]; // We can pass a suggested config
|
cardConfig?: LovelaceCardConfig[]; // We can pass a suggested config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user