Skip to content

Commit

Permalink
Refactor suggest card function (#19528)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Jan 25, 2024
1 parent 75bbc33 commit 973752b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { addEntitiesToLovelaceView } from "../../../lovelace/editor/add-entities
import type { LovelaceRowConfig } from "../../../lovelace/entity-rows/types";
import { LovelaceRow } from "../../../lovelace/entity-rows/types";
import { EntityRegistryStateEntry } from "../ha-config-device-page";
import { computeCards } from "../../../lovelace/common/generate-lovelace-config";

@customElement("ha-device-entities-card")
export class HaDeviceEntitiesCard extends LitElement {
Expand Down Expand Up @@ -224,13 +225,17 @@ export class HaDeviceEntitiesCard extends LitElement {
}

private _addToLovelaceView(): void {
const entities = this.entities
.filter((entity) => !entity.disabled_by)
.map((entity) => entity.entity_id);

addEntitiesToLovelaceView(
this,
this.hass,
this.entities
.filter((entity) => !entity.disabled_by)
.map((entity) => entity.entity_id),
this.deviceName
computeCards(this.hass.states, entities, {
title: this.deviceName,
}),
entities
);
}

Expand Down
13 changes: 7 additions & 6 deletions src/panels/lovelace/editor/add-entities-to-view.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LovelacePanelConfig } from "../../../data/lovelace";
import { LovelaceCardConfig } from "../../../data/lovelace/config/card";
import {
LovelaceConfig,
fetchConfig,
Expand All @@ -13,8 +14,8 @@ import { showSelectViewDialog } from "./select-view/show-select-view-dialog";
export const addEntitiesToLovelaceView = async (
element: HTMLElement,
hass: HomeAssistant,
entities: string[],
cardTitle?: string
cardConfig: LovelaceCardConfig[],
entities?: string[]
) => {
hass.loadFragmentTranslation("lovelace");
const dashboards = await fetchDashboards(hass);
Expand All @@ -30,9 +31,9 @@ export const addEntitiesToLovelaceView = async (
if (mainLovelaceMode !== "storage" && !storageDashs.length) {
// no storage dashboards, just show the YAML config
showSuggestCardDialog(element, {
cardConfig,
entities,
yaml: true,
cardTitle,
});
return;
}
Expand Down Expand Up @@ -69,9 +70,9 @@ export const addEntitiesToLovelaceView = async (
if (dashboards.length > storageDashs.length) {
// all storage dashboards are generated, but we have YAML dashboards just show the YAML config
showSuggestCardDialog(element, {
cardConfig,
entities,
yaml: true,
cardTitle,
});
} else {
// all storage dashboards are generated
Expand All @@ -91,7 +92,7 @@ export const addEntitiesToLovelaceView = async (

if (!storageDashs.length && lovelaceConfig.views.length === 1) {
showSuggestCardDialog(element, {
cardTitle,
cardConfig,
lovelaceConfig: lovelaceConfig!,
saveConfig: async (newConfig: LovelaceConfig): Promise<void> => {
try {
Expand All @@ -114,7 +115,7 @@ export const addEntitiesToLovelaceView = async (
dashboards,
viewSelectedCallback: (newUrlPath, selectedDashConfig, viewIndex) => {
showSuggestCardDialog(element, {
cardTitle,
cardConfig,
lovelaceConfig: selectedDashConfig,
saveConfig: async (newConfig: LovelaceConfig): Promise<void> => {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { LovelaceCardConfig } from "../../../../data/lovelace/config/card";
import { haStyleDialog } from "../../../../resources/styles";
import { HomeAssistant } from "../../../../types";
import { showSaveSuccessToast } from "../../../../util/toast-saved-success";
import { computeCards } from "../../common/generate-lovelace-config";
import { addCards } from "../config-util";
import "./hui-card-preview";
import { showCreateCardDialog } from "./show-create-card-dialog";
Expand All @@ -28,11 +27,7 @@ export class HuiDialogSuggestCard extends LitElement {

public showDialog(params: SuggestCardDialogParams): void {
this._params = params;
this._cardConfig =
params.cardConfig ||
computeCards(this.hass.states, params.entities, {
title: params.cardTitle,
});
this._cardConfig = params.cardConfig;
if (!Object.isFrozen(this._cardConfig)) {
this._cardConfig = deepFreeze(this._cardConfig);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { LovelaceCardConfig } from "../../../../data/lovelace/config/card";
import { LovelaceConfig } from "../../../../data/lovelace/config/types";

export interface SuggestCardDialogParams {
cardTitle?: string;
lovelaceConfig?: LovelaceConfig;
yaml?: boolean;
saveConfig?: (config: LovelaceConfig) => void;
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
}

Expand Down

0 comments on commit 973752b

Please sign in to comment.