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 { computeStateDomain } from "../common/entity/compute_state_domain";
|
||||||
import { autoCaseNoun } from "../common/translations/auto_case_noun";
|
import { autoCaseNoun } from "../common/translations/auto_case_noun";
|
||||||
import type { LocalizeFunc } from "../common/translations/localize";
|
import type { LocalizeFunc } from "../common/translations/localize";
|
||||||
import type { HaEntityPickerEntityFilterFunc } from "../components/entity/ha-entity-picker";
|
|
||||||
import type { HomeAssistant } from "../types";
|
import type { HomeAssistant } from "../types";
|
||||||
import { UNAVAILABLE, UNKNOWN } from "./entity";
|
import { UNAVAILABLE, UNKNOWN } from "./entity";
|
||||||
|
import { isNumericEntity } from "./history";
|
||||||
|
|
||||||
const LOGBOOK_LOCALIZE_PATH = "ui.components.logbook.messages";
|
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 {
|
export interface LogbookStreamMessage {
|
||||||
events: LogbookEntry[];
|
events: LogbookEntry[];
|
||||||
@@ -326,9 +326,14 @@ export const localizeStateMessage = (
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const filterLogbookCompatibleEntities: HaEntityPickerEntityFilterFunc = (
|
export const filterLogbookCompatibleEntities = (
|
||||||
entity
|
entity,
|
||||||
) =>
|
sensorNumericDeviceClasses: string[] = []
|
||||||
computeStateDomain(entity) !== "sensor" ||
|
) => {
|
||||||
(entity.attributes.unit_of_measurement === undefined &&
|
const domain = computeStateDomain(entity);
|
||||||
entity.attributes.state_class === undefined);
|
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);
|
const domain = computeDomain(entityId);
|
||||||
if (
|
if (
|
||||||
(CONTINUOUS_DOMAINS.includes(domain) &&
|
CONTINUOUS_DOMAINS.includes(domain) ||
|
||||||
|
(domain === "sensor" &&
|
||||||
isNumericEntity(
|
isNumericEntity(
|
||||||
domain,
|
domain,
|
||||||
stateObj,
|
stateObj,
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ import "./ha-logbook";
|
|||||||
import { storage } from "../../common/decorators/storage";
|
import { storage } from "../../common/decorators/storage";
|
||||||
import { ensureArray } from "../../common/array/ensure-array";
|
import { ensureArray } from "../../common/array/ensure-array";
|
||||||
import { resolveEntityIDs } from "../../data/selector";
|
import { resolveEntityIDs } from "../../data/selector";
|
||||||
|
import { getSensorNumericDeviceClasses } from "../../data/sensor";
|
||||||
|
import type { HaEntityPickerEntityFilterFunc } from "../../components/entity/ha-entity-picker";
|
||||||
|
|
||||||
@customElement("ha-panel-logbook")
|
@customElement("ha-panel-logbook")
|
||||||
export class HaPanelLogbook extends LitElement {
|
export class HaPanelLogbook extends LitElement {
|
||||||
@@ -47,6 +49,8 @@ export class HaPanelLogbook extends LitElement {
|
|||||||
})
|
})
|
||||||
private _targetPickerValue: HassServiceTarget = {};
|
private _targetPickerValue: HassServiceTarget = {};
|
||||||
|
|
||||||
|
@state() private _sensorNumericDeviceClasses?: string[] = [];
|
||||||
|
|
||||||
public constructor() {
|
public constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
@@ -100,7 +104,7 @@ export class HaPanelLogbook extends LitElement {
|
|||||||
|
|
||||||
<ha-target-picker
|
<ha-target-picker
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.entityFilter=${filterLogbookCompatibleEntities}
|
.entityFilter=${this._filterFunc}
|
||||||
.value=${this._targetPickerValue}
|
.value=${this._targetPickerValue}
|
||||||
add-on-top
|
add-on-top
|
||||||
@value-changed=${this._targetsChanged}
|
@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) {
|
protected willUpdate(changedProps: PropertyValues) {
|
||||||
super.willUpdate(changedProps);
|
super.willUpdate(changedProps);
|
||||||
|
|
||||||
@@ -128,9 +135,15 @@ export class HaPanelLogbook extends LitElement {
|
|||||||
this._applyURLParams();
|
this._applyURLParams();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async _loadNumericDeviceClasses() {
|
||||||
|
const deviceClasses = await getSensorNumericDeviceClasses(this.hass);
|
||||||
|
this._sensorNumericDeviceClasses = deviceClasses.numeric_device_classes;
|
||||||
|
}
|
||||||
|
|
||||||
protected firstUpdated(changedProps: PropertyValues) {
|
protected firstUpdated(changedProps: PropertyValues) {
|
||||||
super.firstUpdated(changedProps);
|
super.firstUpdated(changedProps);
|
||||||
this.hass.loadBackendTranslation("title");
|
this.hass.loadBackendTranslation("title");
|
||||||
|
this._loadNumericDeviceClasses();
|
||||||
|
|
||||||
const searchParams = extractSearchParamsObject();
|
const searchParams = extractSearchParamsObject();
|
||||||
if (searchParams.back === "1" && history.length > 1) {
|
if (searchParams.back === "1" && history.length > 1) {
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ import type { LovelaceCardEditor } from "../../types";
|
|||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
import { DEFAULT_HOURS_TO_SHOW } from "../../cards/hui-logbook-card";
|
import { DEFAULT_HOURS_TO_SHOW } from "../../cards/hui-logbook-card";
|
||||||
import { targetStruct } from "../../../../data/script";
|
import { targetStruct } from "../../../../data/script";
|
||||||
|
import type { HaEntityPickerEntityFilterFunc } from "../../../../components/entity/ha-entity-picker";
|
||||||
|
import { getSensorNumericDeviceClasses } from "../../../../data/sensor";
|
||||||
|
|
||||||
const cardConfigStruct = assign(
|
const cardConfigStruct = assign(
|
||||||
baseLovelaceCardConfig,
|
baseLovelaceCardConfig,
|
||||||
@@ -59,6 +61,8 @@ export class HuiLogbookCardEditor
|
|||||||
|
|
||||||
@state() private _config?: LogbookCardConfig;
|
@state() private _config?: LogbookCardConfig;
|
||||||
|
|
||||||
|
@state() private _sensorNumericDeviceClasses?: string[];
|
||||||
|
|
||||||
public setConfig(config: LogbookCardConfig): void {
|
public setConfig(config: LogbookCardConfig): void {
|
||||||
assert(config, cardConfigStruct);
|
assert(config, cardConfigStruct);
|
||||||
this._config = config;
|
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() {
|
protected render() {
|
||||||
if (!this.hass || !this._config) {
|
if (!this.hass || !this._config) {
|
||||||
return nothing;
|
return nothing;
|
||||||
@@ -96,7 +114,7 @@ export class HuiLogbookCardEditor
|
|||||||
|
|
||||||
<ha-target-picker
|
<ha-target-picker
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.entityFilter=${filterLogbookCompatibleEntities}
|
.entityFilter=${this._filterFunc}
|
||||||
.value=${this._targetPicker}
|
.value=${this._targetPicker}
|
||||||
add-on-top
|
add-on-top
|
||||||
@value-changed=${this._entitiesChanged}
|
@value-changed=${this._entitiesChanged}
|
||||||
@@ -104,6 +122,9 @@ export class HuiLogbookCardEditor
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _filterFunc: HaEntityPickerEntityFilterFunc = (entity) =>
|
||||||
|
filterLogbookCompatibleEntities(entity, this._sensorNumericDeviceClasses);
|
||||||
|
|
||||||
private _entitiesChanged(ev: CustomEvent): void {
|
private _entitiesChanged(ev: CustomEvent): void {
|
||||||
this._config = { ...this._config!, target: ev.detail.value };
|
this._config = { ...this._config!, target: ev.detail.value };
|
||||||
fireEvent(this, "config-changed", { config: this._config });
|
fireEvent(this, "config-changed", { config: this._config });
|
||||||
|
|||||||
Reference in New Issue
Block a user