Skip to content
Merged
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
16 changes: 14 additions & 2 deletions src/panels/logbook/ha-logbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export class HaLogbook extends LitElement {

@property({ attribute: false }) public deviceIds?: string[];

@property({ attribute: false }) public stateFilter?: string[];

@property({ type: Boolean }) public narrow = false;

@property({ type: Boolean, reflect: true }) public virtualize = false;
Expand Down Expand Up @@ -165,7 +167,7 @@ export class HaLogbook extends LitElement {
protected willUpdate(changedProps: PropertyValues): void {
let changed = changedProps.has("time");

for (const key of ["entityIds", "deviceIds"]) {
for (const key of ["entityIds", "deviceIds", "stateFilter"]) {
if (!changedProps.has(key)) {
continue;
}
Expand Down Expand Up @@ -352,9 +354,19 @@ export class HaLogbook extends LitElement {
"recent" in this.time
? findStartOfRecentTime(new Date(), this.time.recent)
: undefined;

let eventsFiltered: LogbookEntry[] | undefined;
if (this.stateFilter && this.stateFilter.length > 0) {
eventsFiltered = streamMessage.events.filter(
(e) => e.state && this.stateFilter?.includes(e.state)
);
} else {
eventsFiltered = [...streamMessage.events];
}

// Put newest ones on top. Reverse works in-place so
// make a copy first.
const newEntries = [...streamMessage.events].reverse();
const newEntries = eventsFiltered.reverse();
if (!this._logbookEntries || !this._logbookEntries.length) {
this._logbookEntries = newEntries;
return;
Expand Down
5 changes: 5 additions & 0 deletions src/panels/lovelace/cards/hui-logbook-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard {

@state() private _targetPickerValue: HassServiceTarget = {};

@state() private _stateFilter?: string[];

public getCardSize(): number {
return 9 + (this._config?.title ? 1 : 0);
}
Expand Down Expand Up @@ -129,6 +131,8 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard {
};

this._targetPickerValue = target;

this._stateFilter = ensureArray(config.state_filter);
}

private _getEntityIds(): string[] | undefined {
Expand Down Expand Up @@ -209,6 +213,7 @@ export class HuiLogbookCard extends LitElement implements LovelaceCard {
.hass=${this.hass}
.time=${this._time}
.entityIds=${this._getEntityIds()}
.stateFilter=${this._stateFilter}
narrow
relative-time
virtualize
Expand Down
1 change: 1 addition & 0 deletions src/panels/lovelace/cards/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export interface LogbookCardConfig extends LovelaceCardConfig {
title?: string;
hours_to_show?: number;
theme?: string;
state_filter?: string[];
}

interface GeoLocationSourceConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
string,
} from "superstruct";
import type { HassServiceTarget } from "home-assistant-js-websocket";
import memoizeOne from "memoize-one";
import { fireEvent } from "../../../../common/dom/fire_event";
import "../../../../components/entity/ha-entities-picker";
import "../../../../components/ha-target-picker";
Expand All @@ -24,6 +25,7 @@ import { DEFAULT_HOURS_TO_SHOW } from "../../cards/hui-logbook-card";
import { targetStruct } from "../../../../data/script";
import { getSensorNumericDeviceClasses } from "../../../../data/sensor";
import type { HaEntityPickerEntityFilterFunc } from "../../../../data/entity";
import { resolveEntityIDs } from "../../../../data/selector";

const cardConfigStruct = assign(
baseLovelaceCardConfig,
Expand All @@ -33,6 +35,7 @@ const cardConfigStruct = assign(
hours_to_show: optional(number()),
theme: optional(string()),
target: optional(targetStruct),
state_filter: optional(array(string())),
})
);

Expand All @@ -50,6 +53,13 @@ const SCHEMA = [
},
],
},
{
name: "state_filter",
context: {
filter_entity: "context_entities",
},
selector: { state: { multiple: true } },
},
] as const;

@customElement("hui-logbook-card-editor")
Expand Down Expand Up @@ -106,7 +116,13 @@ export class HuiLogbookCardEditor
return html`
<ha-form
.hass=${this.hass}
.data=${this._config}
.data=${this._data(
this._config,
this._targetPicker,
this.hass.entities,
this.hass.devices,
this.hass.areas
)}
.schema=${SCHEMA}
.computeLabel=${this._computeLabelCallback}
@value-changed=${this._valueChanged}
Expand All @@ -122,6 +138,25 @@ export class HuiLogbookCardEditor
`;
}

private _data = memoizeOne(
(
config: LogbookCardConfig,
target: HassServiceTarget,
entities: HomeAssistant["entities"],
devices: HomeAssistant["devices"],
areas: HomeAssistant["areas"]
) => ({
...config,
context_entities: resolveEntityIDs(
this.hass!,
target,
entities,
devices,
areas
),
})
);

private _filterFunc: HaEntityPickerEntityFilterFunc = (entity) =>
filterLogbookCompatibleEntities(entity, this._sensorNumericDeviceClasses);

Expand All @@ -131,7 +166,9 @@ export class HuiLogbookCardEditor
}

private _valueChanged(ev: CustomEvent): void {
fireEvent(this, "config-changed", { config: ev.detail.value });
const newConfig = { ...ev.detail.value };
delete newConfig.context_entities;
fireEvent(this, "config-changed", { config: newConfig });
}

private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) => {
Expand All @@ -142,6 +179,10 @@ export class HuiLogbookCardEditor
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.optional"
)})`;
case "state_filter":
return this.hass!.localize(
"ui.panel.lovelace.editor.card.logbook.state_filter"
);
default:
return this.hass!.localize(
`ui.panel.lovelace.editor.card.generic.${schema.name}`
Expand Down
3 changes: 2 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7651,7 +7651,8 @@
},
"logbook": {
"name": "Activity",
"description": "The Activity card shows a list of events for entities."
"description": "The Activity card shows a list of events for entities.",
"state_filter": "State filter"
},
"history-graph": {
"name": "History graph",
Expand Down
Loading