Skip to content

Commit

Permalink
Migrate entities to items
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Sep 26, 2024
1 parent 7df4ebb commit 89b0d84
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
19 changes: 15 additions & 4 deletions src/panels/lovelace/cards/hui-heading-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ import type {
} from "../types";
import type { HeadingCardConfig } from "./types";

export const migrateHeadingCardConfig = (
config: HeadingCardConfig
): HeadingCardConfig => {
const newConfig = { ...config };
if (newConfig.entities) {
newConfig.items = [...(newConfig.items || []), ...newConfig.entities];
delete newConfig.entities;
}
return newConfig;
};

@customElement("hui-heading-card")
export class HuiHeadingCard extends LitElement implements LovelaceCard {
public static async getConfigElement(): Promise<LovelaceCardEditor> {
Expand All @@ -47,7 +58,7 @@ export class HuiHeadingCard extends LitElement implements LovelaceCard {
tap_action: {
action: "none",
},
...config,
...migrateHeadingCardConfig(config),
};
}

Expand All @@ -66,8 +77,8 @@ export class HuiHeadingCard extends LitElement implements LovelaceCard {
handleAction(this, this.hass!, this._config!, ev.detail.action!);
}

private _items = memoizeOne((entities: HeadingCardConfig["entities"]) =>
processEditorEntities(entities || [])
private _items = memoizeOne((items: HeadingCardConfig["items"]) =>
processEditorEntities(items || [])
);

protected render() {
Expand All @@ -79,7 +90,7 @@ export class HuiHeadingCard extends LitElement implements LovelaceCard {

const style = this._config.heading_style || "title";

const items = this._items(this._config.entities);
const items = this._items(this._config.items);

return html`
<ha-card>
Expand Down
2 changes: 2 additions & 0 deletions src/panels/lovelace/cards/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,5 +509,7 @@ export interface HeadingCardConfig extends LovelaceCardConfig {
heading?: string;
icon?: string;
tap_action?: ActionConfig;
items?: (LovelaceHeadingItemConfig | string)[];
/** @deprecated Use `triggers` instead */
entities?: (LovelaceHeadingItemConfig | string)[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { EditSubElementEvent } from "../types";
import { configElementStyle } from "./config-elements-style";
import "./hui-heading-items-editor";
import { migrateHeadingCardConfig } from "../../cards/hui-heading-card";

const actions: UiAction[] = ["navigate", "url", "perform-action", "none"];

Expand All @@ -45,6 +46,8 @@ const cardConfigStruct = assign(
heading: optional(string()),
icon: optional(string()),
tap_action: optional(actionConfigStruct),
items: optional(array(any())),
// deprecated
entities: optional(array(any())),
})
);
Expand All @@ -60,7 +63,7 @@ export class HuiHeadingCardEditor

public setConfig(config: HeadingCardConfig): void {
assert(config, cardConfigStruct);
this._config = config;
this._config = migrateHeadingCardConfig(config);
}

private _schema = memoizeOne(
Expand Down Expand Up @@ -108,8 +111,8 @@ export class HuiHeadingCardEditor
);

private _items = memoizeOne(
(entities: HeadingCardConfig["entities"]): LovelaceHeadingItemConfig[] =>
processEditorEntities(entities || [])
(items: HeadingCardConfig["items"]): LovelaceHeadingItemConfig[] =>
processEditorEntities(items || [])
);

protected render() {
Expand Down Expand Up @@ -145,7 +148,7 @@ export class HuiHeadingCardEditor
<div class="content">
<hui-heading-items-editor
.hass=${this.hass}
.items=${this._items(this._config!.entities)}
.items=${this._items(this._config!.items)}
@heading-items-changed=${this._itemsChanged}
@edit-heading-item=${this._editItem}
>
Expand All @@ -163,7 +166,7 @@ export class HuiHeadingCardEditor

const config = {
...this._config,
entities: ev.detail.items as LovelaceHeadingItemConfig[],
items: ev.detail.items as LovelaceHeadingItemConfig[],
};

fireEvent(this, "config-changed", { config });
Expand All @@ -183,7 +186,7 @@ export class HuiHeadingCardEditor
private _editItem(ev: HASSDomEvent<{ index: number }>): void {
ev.stopPropagation();
const index = ev.detail.index;
const config = this._items(this._config!.entities)[index];
const config = this._items(this._config!.items)[index];

fireEvent(this, "edit-sub-element", {
config: config,
Expand All @@ -193,9 +196,9 @@ export class HuiHeadingCardEditor
}

private _updateEntity(index: number, entity: EntityHeadingItemConfig) {
const entities = this._config!.entities!.concat();
entities[index] = entity;
const config = { ...this._config!, entities };
const items = this._config!.items!.concat();
items[index] = entity;
const config = { ...this._config!, items };
fireEvent(this, "config-changed", {
config: config,
});
Expand Down

0 comments on commit 89b0d84

Please sign in to comment.