diff --git a/src/data/script.ts b/src/data/script.ts index c7e08fc12911..b72b08288860 100644 --- a/src/data/script.ts +++ b/src/data/script.ts @@ -40,7 +40,7 @@ export const baseActionStruct = object({ enabled: optional(boolean()), }); -const targetStruct = object({ +export const targetStruct = object({ entity_id: optional(union([string(), array(string())])), device_id: optional(union([string(), array(string())])), area_id: optional(union([string(), array(string())])), diff --git a/src/panels/lovelace/cards/hui-logbook-card.ts b/src/panels/lovelace/cards/hui-logbook-card.ts index fbac8b6d22b4..0bdf74cfb939 100644 --- a/src/panels/lovelace/cards/hui-logbook-card.ts +++ b/src/panels/lovelace/cards/hui-logbook-card.ts @@ -7,6 +7,7 @@ import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_elemen import "../../../components/ha-card"; import type { HomeAssistant } from "../../../types"; import "../../logbook/ha-logbook"; +import type { HassServiceTarget } from "home-assistant-js-websocket"; import type { HaLogbook } from "../../logbook/ha-logbook"; import { findEntities } from "../common/find-entities"; import { processConfigEntities } from "../common/process-config-entities"; @@ -14,6 +15,8 @@ import "../components/hui-warning"; import type { EntityConfig } from "../entity-rows/types"; import type { LovelaceCard, LovelaceCardEditor } from "../types"; import type { LogbookCardConfig } from "./types"; +import { resolveEntityIDs } from "../../../data/selector"; +import memoizeOne from "memoize-one"; export const DEFAULT_HOURS_TO_SHOW = 24; @@ -50,16 +53,16 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard { @state() private _time?: HaLogbook["time"]; - @state() private _entityId?: string[]; + @state() private _targetPickerValue: HassServiceTarget = {}; public getCardSize(): number { return 9 + (this._config?.title ? 1 : 0); } public setConfig(config: LogbookCardConfig): void { - if (!config.entities.length) { + /*if (!config.entities.length) { throw new Error("Entities must be specified"); - } + }*/ this._config = { hours_to_show: DEFAULT_HOURS_TO_SHOW, @@ -68,11 +71,34 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard { this._time = { recent: this._config!.hours_to_show! * 60 * 60, }; - this._entityId = processConfigEntities(config.entities).map( - (entity) => entity.entity + + this._targetPickerValue = this._config?.target; + } + + private _getEntityIds(): string[] | undefined { + console.log("test"); + const entities = this.__getEntityIds( + this._targetPickerValue, + this.hass.entities, + this.hass.devices, + this.hass.areas ); + if (entities.length === 0) { + return undefined; + } + return entities; } + private __getEntityIds = memoizeOne( + ( + targetPickerValue: HassServiceTarget, + entities: HomeAssistant["entities"], + devices: HomeAssistant["devices"], + areas: HomeAssistant["areas"] + ): string[] => + resolveEntityIDs(this.hass, targetPickerValue, entities, devices, areas) + ); + protected updated(changedProperties: PropertyValues) { super.updated(changedProperties); if (!this._config || !this.hass) { @@ -116,7 +142,7 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard { -

- ${`${this.hass!.localize( - "ui.panel.lovelace.editor.card.generic.entities" - )} (${this.hass!.localize( - "ui.panel.lovelace.editor.card.config.required" - )})`} -

- - + > `; } private _entitiesChanged(ev: CustomEvent): void { - this._config = { ...this._config!, entities: ev.detail.value }; + this._config = { ...this._config!, target: ev.detail.value }; fireEvent(this, "config-changed", { config: this._config }); }