Skip to content

Commit

Permalink
Filter unrecorded entities from history panel (#19621)
Browse files Browse the repository at this point in the history
* Filter unrecorded entities from history panel

* cache result

* Cache excluded entities instead of recorded entities
  • Loading branch information
karwosts authored May 29, 2024
1 parent 5fab196 commit d886700
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/data/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export interface RecorderInfo {
thread_running: boolean;
}

export interface RecordedEntities {
entity_ids: string[];
}
export interface RecordedExcludedEntities {
recorded_ids: string[];
excluded_ids: string[];
}

export type StatisticType = "change" | "state" | "sum" | "min" | "max" | "mean";

export interface Statistics {
Expand Down Expand Up @@ -324,3 +332,25 @@ export const getDisplayUnit = (

export const isExternalStatistic = (statisticsId: string): boolean =>
statisticsId.includes(":");

let recordedExcludedEntitiesCache: RecordedExcludedEntities | undefined;

export const getRecordedExcludedEntities = async (
hass: HomeAssistant
): Promise<RecordedExcludedEntities> => {
if (recordedExcludedEntitiesCache) {
return recordedExcludedEntitiesCache;
}
const recordedEntities = await hass.callWS<RecordedEntities>({
type: "recorder/recorded_entities",
});

recordedExcludedEntitiesCache = {
recorded_ids: recordedEntities.entity_ids,
excluded_ids: Object.keys(hass.states).filter(
(id) => !recordedEntities.entity_ids.includes(id)
),
};

return recordedExcludedEntitiesCache;
};
23 changes: 22 additions & 1 deletion src/panels/history/ha-panel-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@mdi/js";
import { ActionDetail } from "@material/mwc-list";
import { differenceInHours } from "date-fns";
import { HassEntity } from "home-assistant-js-websocket";
import {
HassServiceTarget,
UnsubscribeFunc,
Expand Down Expand Up @@ -45,7 +46,11 @@ import {
computeHistory,
subscribeHistory,
} from "../../data/history";
import { Statistics, fetchStatistics } from "../../data/recorder";
import {
fetchStatistics,
Statistics,
getRecordedExcludedEntities,
} from "../../data/recorder";
import {
expandAreaTarget,
expandDeviceTarget,
Expand Down Expand Up @@ -95,6 +100,8 @@ class HaPanelHistory extends LitElement {

private _interval?: number;

private _excludedEntities?: string[];

public constructor() {
super();

Expand Down Expand Up @@ -185,6 +192,7 @@ class HaPanelHistory extends LitElement {
.hass=${this.hass}
.value=${this._targetPickerValue}
.disabled=${this._isLoading}
.entityFilter=${this._entityFilter}
addOnTop
@value-changed=${this._targetsChanged}
></ha-target-picker>
Expand All @@ -211,6 +219,10 @@ class HaPanelHistory extends LitElement {
`;
}

private _entityFilter = (entity: HassEntity): boolean =>
!this._excludedEntities ||
!this._excludedEntities.includes(entity.entity_id);

private mergeHistoryResults(
ltsResult: HistoryResult,
historyResult: HistoryResult
Expand Down Expand Up @@ -362,8 +374,17 @@ class HaPanelHistory extends LitElement {
}
}

private async _getRecordedExcludedEntities() {
const { recorded_ids: _recordedIds, excluded_ids: excludedIds } =
await getRecordedExcludedEntities(this.hass);
this._excludedEntities = excludedIds;
}

protected firstUpdated(changedProps: PropertyValues) {
super.firstUpdated(changedProps);

this._getRecordedExcludedEntities();

const searchParams = extractSearchParamsObject();
if (searchParams.back === "1" && history.length > 1) {
this._showBack = true;
Expand Down

0 comments on commit d886700

Please sign in to comment.