From f464bcfc14e1ad461f03eca3f5caedd87915776a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 5 Jun 2022 18:45:58 -1000 Subject: [PATCH] Filter entities that will never have entries in the logbook card editor (#12876) --- src/data/logbook.ts | 9 +++++++++ src/panels/logbook/ha-panel-logbook.ts | 16 ++-------------- .../config-elements/hui-logbook-card-editor.ts | 2 ++ 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/data/logbook.ts b/src/data/logbook.ts index eda3aa4ce6..2e56067d76 100644 --- a/src/data/logbook.ts +++ b/src/data/logbook.ts @@ -6,7 +6,9 @@ import { } from "../common/const"; import { computeDomain } from "../common/entity/compute_domain"; import { computeStateDisplay } from "../common/entity/compute_state_display"; +import { computeStateDomain } from "../common/entity/compute_state_domain"; import { LocalizeFunc } from "../common/translations/localize"; +import { HaEntityPickerEntityFilterFunc } from "../components/entity/ha-entity-picker"; import { HomeAssistant } from "../types"; import { UNAVAILABLE_STATES } from "./entity"; @@ -425,3 +427,10 @@ export const localizeStateMessage = ( : state ); }; + +export const filterLogbookCompatibleEntities: HaEntityPickerEntityFilterFunc = ( + entity +) => + computeStateDomain(entity) !== "sensor" || + (entity.attributes.unit_of_measurement === undefined && + entity.attributes.state_class === undefined); diff --git a/src/panels/logbook/ha-panel-logbook.ts b/src/panels/logbook/ha-panel-logbook.ts index bcdcb548f7..b61443442e 100644 --- a/src/panels/logbook/ha-panel-logbook.ts +++ b/src/panels/logbook/ha-panel-logbook.ts @@ -12,7 +12,6 @@ import { } from "date-fns/esm"; import { css, html, LitElement, PropertyValues } from "lit"; import { customElement, property, state } from "lit/decorators"; -import { computeStateDomain } from "../../common/entity/compute_state_domain"; import { navigate } from "../../common/navigate"; import { createSearchParam, @@ -20,11 +19,11 @@ import { } from "../../common/url/search-params"; import { computeRTL } from "../../common/util/compute_rtl"; import "../../components/entity/ha-entity-picker"; -import type { HaEntityPickerEntityFilterFunc } from "../../components/entity/ha-entity-picker"; import "../../components/ha-date-range-picker"; import type { DateRangePickerRanges } from "../../components/ha-date-range-picker"; import "../../components/ha-icon-button"; import "../../components/ha-menu-button"; +import { filterLogbookCompatibleEntities } from "../../data/logbook"; import "../../layouts/ha-app-layout"; import { haStyle } from "../../resources/styles"; import { HomeAssistant } from "../../types"; @@ -89,7 +88,7 @@ export class HaPanelLogbook extends LitElement { .label=${this.hass.localize( "ui.components.entity.entity-picker.entity" )} - .entityFilter=${this._entityFilter} + .entityFilter=${filterLogbookCompatibleEntities} @change=${this._entityPicked} > @@ -242,17 +241,6 @@ export class HaPanelLogbook extends LitElement { this.shadowRoot!.querySelector("ha-logbook")?.refresh(); } - private _entityFilter: HaEntityPickerEntityFilterFunc = (entity) => { - if (computeStateDomain(entity) !== "sensor") { - return true; - } - - return ( - entity.attributes.unit_of_measurement === undefined && - entity.attributes.state_class === undefined - ); - }; - static get styles() { return [ haStyle, diff --git a/src/panels/lovelace/editor/config-elements/hui-logbook-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-logbook-card-editor.ts index f789c09bbe..dcbc6f325b 100644 --- a/src/panels/lovelace/editor/config-elements/hui-logbook-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-logbook-card-editor.ts @@ -17,6 +17,7 @@ import type { HomeAssistant } from "../../../../types"; import type { LogbookCardConfig } from "../../cards/types"; import type { LovelaceCardEditor } from "../../types"; import { baseLovelaceCardConfig } from "../structs/base-card-struct"; +import { filterLogbookCompatibleEntities } from "../../../../data/logbook"; const cardConfigStruct = assign( baseLovelaceCardConfig, @@ -81,6 +82,7 @@ export class HuiLogbookCardEditor