mirror of
https://github.com/home-assistant/frontend.git
synced 2025-12-20 02:38:53 +00:00
Fix incorrect logbook entity filters (#27037)
* Fix incorrect logbook filters * Update src/panels/lovelace/editor/config-elements/hui-logbook-card-editor.ts --------- Co-authored-by: Petar Petrov <MindFreeze@users.noreply.github.com>
This commit is contained in:
@@ -8,12 +8,12 @@ import { computeDomain } from "../common/entity/compute_domain";
|
||||
import { computeStateDomain } from "../common/entity/compute_state_domain";
|
||||
import { autoCaseNoun } from "../common/translations/auto_case_noun";
|
||||
import type { LocalizeFunc } from "../common/translations/localize";
|
||||
import type { HaEntityPickerEntityFilterFunc } from "../components/entity/ha-entity-picker";
|
||||
import type { HomeAssistant } from "../types";
|
||||
import { UNAVAILABLE, UNKNOWN } from "./entity";
|
||||
import { isNumericEntity } from "./history";
|
||||
|
||||
const LOGBOOK_LOCALIZE_PATH = "ui.components.logbook.messages";
|
||||
export const CONTINUOUS_DOMAINS = ["counter", "proximity", "sensor", "zone"];
|
||||
export const CONTINUOUS_DOMAINS = ["counter", "proximity"];
|
||||
|
||||
export interface LogbookStreamMessage {
|
||||
events: LogbookEntry[];
|
||||
@@ -326,9 +326,14 @@ export const localizeStateMessage = (
|
||||
});
|
||||
};
|
||||
|
||||
export const filterLogbookCompatibleEntities: HaEntityPickerEntityFilterFunc = (
|
||||
entity
|
||||
) =>
|
||||
computeStateDomain(entity) !== "sensor" ||
|
||||
(entity.attributes.unit_of_measurement === undefined &&
|
||||
entity.attributes.state_class === undefined);
|
||||
export const filterLogbookCompatibleEntities = (
|
||||
entity,
|
||||
sensorNumericDeviceClasses: string[] = []
|
||||
) => {
|
||||
const domain = computeStateDomain(entity);
|
||||
const continuous =
|
||||
CONTINUOUS_DOMAINS.includes(domain) ||
|
||||
(domain === "sensor" &&
|
||||
isNumericEntity(domain, entity, undefined, sensorNumericDeviceClasses));
|
||||
return !continuous;
|
||||
};
|
||||
|
||||
@@ -116,7 +116,8 @@ export const computeShowLogBookComponent = (
|
||||
|
||||
const domain = computeDomain(entityId);
|
||||
if (
|
||||
(CONTINUOUS_DOMAINS.includes(domain) &&
|
||||
CONTINUOUS_DOMAINS.includes(domain) ||
|
||||
(domain === "sensor" &&
|
||||
isNumericEntity(
|
||||
domain,
|
||||
stateObj,
|
||||
|
||||
@@ -25,6 +25,8 @@ import "./ha-logbook";
|
||||
import { storage } from "../../common/decorators/storage";
|
||||
import { ensureArray } from "../../common/array/ensure-array";
|
||||
import { resolveEntityIDs } from "../../data/selector";
|
||||
import { getSensorNumericDeviceClasses } from "../../data/sensor";
|
||||
import type { HaEntityPickerEntityFilterFunc } from "../../components/entity/ha-entity-picker";
|
||||
|
||||
@customElement("ha-panel-logbook")
|
||||
export class HaPanelLogbook extends LitElement {
|
||||
@@ -47,6 +49,8 @@ export class HaPanelLogbook extends LitElement {
|
||||
})
|
||||
private _targetPickerValue: HassServiceTarget = {};
|
||||
|
||||
@state() private _sensorNumericDeviceClasses?: string[] = [];
|
||||
|
||||
public constructor() {
|
||||
super();
|
||||
|
||||
@@ -100,7 +104,7 @@ export class HaPanelLogbook extends LitElement {
|
||||
|
||||
<ha-target-picker
|
||||
.hass=${this.hass}
|
||||
.entityFilter=${filterLogbookCompatibleEntities}
|
||||
.entityFilter=${this._filterFunc}
|
||||
.value=${this._targetPickerValue}
|
||||
add-on-top
|
||||
@value-changed=${this._targetsChanged}
|
||||
@@ -118,6 +122,9 @@ export class HaPanelLogbook extends LitElement {
|
||||
`;
|
||||
}
|
||||
|
||||
private _filterFunc: HaEntityPickerEntityFilterFunc = (entity) =>
|
||||
filterLogbookCompatibleEntities(entity, this._sensorNumericDeviceClasses);
|
||||
|
||||
protected willUpdate(changedProps: PropertyValues) {
|
||||
super.willUpdate(changedProps);
|
||||
|
||||
@@ -128,9 +135,15 @@ export class HaPanelLogbook extends LitElement {
|
||||
this._applyURLParams();
|
||||
}
|
||||
|
||||
private async _loadNumericDeviceClasses() {
|
||||
const deviceClasses = await getSensorNumericDeviceClasses(this.hass);
|
||||
this._sensorNumericDeviceClasses = deviceClasses.numeric_device_classes;
|
||||
}
|
||||
|
||||
protected firstUpdated(changedProps: PropertyValues) {
|
||||
super.firstUpdated(changedProps);
|
||||
this.hass.loadBackendTranslation("title");
|
||||
this._loadNumericDeviceClasses();
|
||||
|
||||
const searchParams = extractSearchParamsObject();
|
||||
if (searchParams.back === "1" && history.length > 1) {
|
||||
|
||||
@@ -22,6 +22,8 @@ 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";
|
||||
import type { HaEntityPickerEntityFilterFunc } from "../../../../components/entity/ha-entity-picker";
|
||||
import { getSensorNumericDeviceClasses } from "../../../../data/sensor";
|
||||
|
||||
const cardConfigStruct = assign(
|
||||
baseLovelaceCardConfig,
|
||||
@@ -59,6 +61,8 @@ export class HuiLogbookCardEditor
|
||||
|
||||
@state() private _config?: LogbookCardConfig;
|
||||
|
||||
@state() private _sensorNumericDeviceClasses?: string[];
|
||||
|
||||
public setConfig(config: LogbookCardConfig): void {
|
||||
assert(config, cardConfigStruct);
|
||||
this._config = config;
|
||||
@@ -80,6 +84,20 @@ export class HuiLogbookCardEditor
|
||||
);
|
||||
}
|
||||
|
||||
private async _loadNumericDeviceClasses(hass: HomeAssistant) {
|
||||
// ensures that the _load function is not called a second time
|
||||
// if another updated occurs before the async function returns
|
||||
this._sensorNumericDeviceClasses = [];
|
||||
const deviceClasses = await getSensorNumericDeviceClasses(hass);
|
||||
this._sensorNumericDeviceClasses = deviceClasses.numeric_device_classes;
|
||||
}
|
||||
|
||||
protected updated() {
|
||||
if (this.hass && !this._sensorNumericDeviceClasses) {
|
||||
this._loadNumericDeviceClasses(this.hass);
|
||||
}
|
||||
}
|
||||
|
||||
protected render() {
|
||||
if (!this.hass || !this._config) {
|
||||
return nothing;
|
||||
@@ -96,7 +114,7 @@ export class HuiLogbookCardEditor
|
||||
|
||||
<ha-target-picker
|
||||
.hass=${this.hass}
|
||||
.entityFilter=${filterLogbookCompatibleEntities}
|
||||
.entityFilter=${this._filterFunc}
|
||||
.value=${this._targetPicker}
|
||||
add-on-top
|
||||
@value-changed=${this._entitiesChanged}
|
||||
@@ -104,6 +122,9 @@ export class HuiLogbookCardEditor
|
||||
`;
|
||||
}
|
||||
|
||||
private _filterFunc: HaEntityPickerEntityFilterFunc = (entity) =>
|
||||
filterLogbookCompatibleEntities(entity, this._sensorNumericDeviceClasses);
|
||||
|
||||
private _entitiesChanged(ev: CustomEvent): void {
|
||||
this._config = { ...this._config!, target: ev.detail.value };
|
||||
fireEvent(this, "config-changed", { config: this._config });
|
||||
|
||||
Reference in New Issue
Block a user