Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a target picker for the logbook card #23007

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/data/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())])),
Expand Down
38 changes: 32 additions & 6 deletions src/panels/lovelace/cards/hui-logbook-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import "../../../components/ha-card";
import type { HomeAssistant } from "../../../types";
import "../../logbook/ha-logbook";
import type { HassServiceTarget } from "home-assistant-js-websocket";

Check failure on line 10 in src/panels/lovelace/cards/hui-logbook-card.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

`home-assistant-js-websocket` type import should occur before import of `../../../common/config/is_component_loaded`
import type { HaLogbook } from "../../logbook/ha-logbook";
import { findEntities } from "../common/find-entities";
import { processConfigEntities } from "../common/process-config-entities";

Check failure on line 13 in src/panels/lovelace/cards/hui-logbook-card.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

'processConfigEntities' is defined but never used
import "../components/hui-warning";
import type { EntityConfig } from "../entity-rows/types";

Check failure on line 15 in src/panels/lovelace/cards/hui-logbook-card.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

'EntityConfig' is defined but never used
import type { LovelaceCard, LovelaceCardEditor } from "../types";
import type { LogbookCardConfig } from "./types";
import { resolveEntityIDs } from "../../../data/selector";
import memoizeOne from "memoize-one";

Check failure on line 19 in src/panels/lovelace/cards/hui-logbook-card.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

`memoize-one` import should occur before import of `../../../common/config/is_component_loaded`

export const DEFAULT_HOURS_TO_SHOW = 24;

Expand Down Expand Up @@ -50,16 +53,16 @@

@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) {

Check failure on line 63 in src/panels/lovelace/cards/hui-logbook-card.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

Expected exception block, space or tab after '/*' in comment

Check failure on line 63 in src/panels/lovelace/cards/hui-logbook-card.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

Expected space or tab before '*/' in comment
throw new Error("Entities must be specified");
}
}*/

this._config = {
hours_to_show: DEFAULT_HOURS_TO_SHOW,
Expand All @@ -68,11 +71,34 @@
this._time = {
recent: this._config!.hours_to_show! * 60 * 60,
};
this._entityId = processConfigEntities<EntityConfig>(config.entities).map(
(entity) => entity.entity

this._targetPickerValue = this._config?.target;
}

private _getEntityIds(): string[] | undefined {
console.log("test");

Check failure on line 79 in src/panels/lovelace/cards/hui-logbook-card.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

Unexpected console statement
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) {
Expand Down Expand Up @@ -116,7 +142,7 @@
<ha-logbook
.hass=${this.hass}
.time=${this._time}
.entityIds=${this._entityId}
.entityIds=${this._getEntityIds()}
narrow
relative-time
virtualize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
optional,
string,
} from "superstruct";
import type { HassServiceTarget } from "home-assistant-js-websocket";
import { fireEvent } from "../../../../common/dom/fire_event";
import "../../../../components/entity/ha-entities-picker";
import "../../../../components/ha-target-picker";
import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types";
import { filterLogbookCompatibleEntities } from "../../../../data/logbook";
Expand All @@ -19,6 +21,7 @@ import type { LogbookCardConfig } from "../../cards/types";
import type { LovelaceCardEditor } from "../../types";
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
import { DEFAULT_HOURS_TO_SHOW } from "../../cards/hui-logbook-card";
import { targetStruct } from "../../../../data/script";

const cardConfigStruct = assign(
baseLovelaceCardConfig,
Expand All @@ -27,6 +30,7 @@ const cardConfigStruct = assign(
title: optional(string()),
hours_to_show: optional(number()),
theme: optional(string()),
target: optional(targetStruct),
})
);

Expand Down Expand Up @@ -64,6 +68,10 @@ export class HuiLogbookCardEditor
return this._config!.entities || [];
}

get _targetPicker(): HassServiceTarget {
return this._config!.target || {};
}

protected render() {
if (!this.hass || !this._config) {
return nothing;
Expand All @@ -77,25 +85,18 @@ export class HuiLogbookCardEditor
.computeLabel=${this._computeLabelCallback}
@value-changed=${this._valueChanged}
></ha-form>
<h3>
${`${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.entities"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.required"
)})`}
</h3>
<ha-entities-picker

<ha-target-picker
.hass=${this.hass}
.value=${this._entities}
.value=${this._targetPicker}
.entityFilter=${filterLogbookCompatibleEntities}
@value-changed=${this._entitiesChanged}
>
</ha-entities-picker>
></ha-target-picker>
`;
}

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 });
}

Expand Down
Loading