Add floor and label support to history panel (#20388)

This commit is contained in:
Bram Kragten 2024-04-04 00:06:03 +02:00 committed by GitHub
parent 92b7a3b477
commit 5f5ac5419b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,6 +9,7 @@ import { property, query, state } from "lit/decorators";
import memoizeOne from "memoize-one"; import memoizeOne from "memoize-one";
import { ensureArray } from "../../common/array/ensure-array"; import { ensureArray } from "../../common/array/ensure-array";
import { storage } from "../../common/decorators/storage"; import { storage } from "../../common/decorators/storage";
import { computeDomain } from "../../common/entity/compute_domain";
import { navigate } from "../../common/navigate"; import { navigate } from "../../common/navigate";
import { constructUrlCurrentPath } from "../../common/url/construct-url"; import { constructUrlCurrentPath } from "../../common/url/construct-url";
import { import {
@ -27,37 +28,29 @@ import "../../components/ha-menu-button";
import "../../components/ha-target-picker"; import "../../components/ha-target-picker";
import "../../components/ha-top-app-bar-fixed"; import "../../components/ha-top-app-bar-fixed";
import { import {
AreaDeviceLookup,
AreaEntityLookup,
getAreaDeviceLookup,
getAreaEntityLookup,
} from "../../data/area_registry";
import {
DeviceEntityLookup,
getDeviceEntityLookup,
subscribeDeviceRegistry,
} from "../../data/device_registry";
import { subscribeEntityRegistry } from "../../data/entity_registry";
import {
HistoryResult,
computeHistory,
subscribeHistory,
HistoryStates,
EntityHistoryState, EntityHistoryState,
HistoryResult,
HistoryStates,
LineChartState,
LineChartUnit, LineChartUnit,
computeGroupKey, computeGroupKey,
LineChartState, computeHistory,
subscribeHistory,
} from "../../data/history"; } from "../../data/history";
import { fetchStatistics, Statistics } from "../../data/recorder"; import { Statistics, fetchStatistics } from "../../data/recorder";
import {
expandAreaTarget,
expandDeviceTarget,
expandFloorTarget,
expandLabelTarget,
} from "../../data/selector";
import { getSensorNumericDeviceClasses } from "../../data/sensor"; import { getSensorNumericDeviceClasses } from "../../data/sensor";
import { SubscribeMixin } from "../../mixins/subscribe-mixin"; import { showAlertDialog } from "../../dialogs/generic/show-dialog-box";
import { haStyle } from "../../resources/styles"; import { haStyle } from "../../resources/styles";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
import { fileDownload } from "../../util/file_download"; import { fileDownload } from "../../util/file_download";
import { showAlertDialog } from "../../dialogs/generic/show-dialog-box";
import { computeDomain } from "../../common/entity/compute_domain";
class HaPanelHistory extends SubscribeMixin(LitElement) { class HaPanelHistory extends LitElement {
@property({ attribute: false }) hass!: HomeAssistant; @property({ attribute: false }) hass!: HomeAssistant;
@property({ reflect: true, type: Boolean }) public narrow = false; @property({ reflect: true, type: Boolean }) public narrow = false;
@ -83,12 +76,6 @@ class HaPanelHistory extends SubscribeMixin(LitElement) {
@state() private _statisticsHistory?: HistoryResult; @state() private _statisticsHistory?: HistoryResult;
@state() private _deviceEntityLookup?: DeviceEntityLookup;
@state() private _areaEntityLookup?: AreaEntityLookup;
@state() private _areaDeviceLookup?: AreaDeviceLookup;
@state() @state()
private _showBack?: boolean; private _showBack?: boolean;
@ -123,18 +110,6 @@ class HaPanelHistory extends SubscribeMixin(LitElement) {
this._unsubscribeHistory(); this._unsubscribeHistory();
} }
public hassSubscribe(): UnsubscribeFunc[] {
return [
subscribeEntityRegistry(this.hass.connection!, (entities) => {
this._deviceEntityLookup = getDeviceEntityLookup(entities);
this._areaEntityLookup = getAreaEntityLookup(entities);
}),
subscribeDeviceRegistry(this.hass.connection!, (devices) => {
this._areaDeviceLookup = getAreaDeviceLookup(devices);
}),
];
}
private _goBack(): void { private _goBack(): void {
history.back(); history.back();
} }
@ -332,7 +307,9 @@ class HaPanelHistory extends SubscribeMixin(LitElement) {
const entityIds = searchParams.entity_id; const entityIds = searchParams.entity_id;
const deviceIds = searchParams.device_id; const deviceIds = searchParams.device_id;
const areaIds = searchParams.area_id; const areaIds = searchParams.area_id;
if (entityIds || deviceIds || areaIds) { const floorIds = searchParams.floor_id;
const labelsIds = searchParams.label_id;
if (entityIds || deviceIds || areaIds || floorIds || labelsIds) {
this._targetPickerValue = {}; this._targetPickerValue = {};
} }
if (entityIds) { if (entityIds) {
@ -347,6 +324,14 @@ class HaPanelHistory extends SubscribeMixin(LitElement) {
const splitIds = areaIds.split(","); const splitIds = areaIds.split(",");
this._targetPickerValue!.area_id = splitIds; this._targetPickerValue!.area_id = splitIds;
} }
if (floorIds) {
const splitIds = floorIds.split(",");
this._targetPickerValue!.floor_id = splitIds;
}
if (labelsIds) {
const splitIds = labelsIds.split(",");
this._targetPickerValue!.label_id = splitIds;
}
const startDate = searchParams.start_date; const startDate = searchParams.start_date;
if (startDate) { if (startDate) {
@ -522,95 +507,80 @@ class HaPanelHistory extends SubscribeMixin(LitElement) {
private _getEntityIds(): string[] { private _getEntityIds(): string[] {
return this.__getEntityIds( return this.__getEntityIds(
this._targetPickerValue, this._targetPickerValue,
this._deviceEntityLookup, this.hass.entities,
this._areaEntityLookup, this.hass.devices,
this._areaDeviceLookup this.hass.areas
); );
} }
private __getEntityIds = memoizeOne( private __getEntityIds = memoizeOne(
( (
targetPickerValue: HassServiceTarget, targetPickerValue: HassServiceTarget,
deviceEntityLookup: DeviceEntityLookup | undefined, entities: HomeAssistant["entities"],
areaEntityLookup: AreaEntityLookup | undefined, devices: HomeAssistant["devices"],
areaDeviceLookup: AreaDeviceLookup | undefined areas: HomeAssistant["areas"]
): string[] => { ): string[] => {
if ( if (!targetPickerValue) {
!targetPickerValue ||
deviceEntityLookup === undefined ||
areaEntityLookup === undefined ||
areaDeviceLookup === undefined
) {
return []; return [];
} }
const entityIds = new Set<string>(); const targetSelector = { target: {} };
let { const targetEntities =
area_id: searchingAreaId, ensureArray(targetPickerValue.entity_id)?.slice() || [];
device_id: searchingDeviceId, const targetDevices =
entity_id: searchingEntityId, ensureArray(targetPickerValue.device_id)?.slice() || [];
} = targetPickerValue; const targetAreas = ensureArray(targetPickerValue.area_id)?.slice() || [];
const targetFloors = ensureArray(targetPickerValue.floor_id)?.slice();
if (searchingAreaId) { const targetLabels = ensureArray(targetPickerValue.label_id)?.slice();
searchingAreaId = ensureArray(searchingAreaId); if (targetLabels) {
for (const singleSearchingAreaId of searchingAreaId) { targetLabels.forEach((labelId) => {
const foundEntities = areaEntityLookup[singleSearchingAreaId]; const expanded = expandLabelTarget(
if (foundEntities?.length) { this.hass,
for (const foundEntity of foundEntities) { labelId,
if (foundEntity.entity_category === null) { areas,
entityIds.add(foundEntity.entity_id); devices,
} entities,
} targetSelector
} );
targetDevices.push(...expanded.devices);
const foundDevices = areaDeviceLookup[singleSearchingAreaId]; targetEntities.push(...expanded.entities);
if (!foundDevices?.length) { targetAreas.push(...expanded.areas);
continue; });
} }
if (targetFloors) {
for (const foundDevice of foundDevices) { targetFloors.forEach((floorId) => {
const foundDeviceEntities = deviceEntityLookup[foundDevice.id]; const expanded = expandFloorTarget(
if (!foundDeviceEntities?.length) { this.hass,
continue; floorId,
} areas,
targetSelector
for (const foundDeviceEntity of foundDeviceEntities) { );
if ( targetAreas.push(...expanded.areas);
(!foundDeviceEntity.area_id || });
foundDeviceEntity.area_id === singleSearchingAreaId) && }
foundDeviceEntity.entity_category === null if (targetAreas.length) {
) { targetAreas.forEach((areaId) => {
entityIds.add(foundDeviceEntity.entity_id); const expanded = expandAreaTarget(
} this.hass,
} areaId,
} devices,
} entities,
targetSelector
);
targetEntities.push(...expanded.entities);
targetDevices.push(...expanded.devices);
});
}
if (targetDevices.length) {
targetDevices.forEach((deviceId) => {
targetEntities.push(
...expandDeviceTarget(this.hass, deviceId, entities, targetSelector)
.entities
);
});
} }
if (searchingDeviceId) { return targetEntities;
searchingDeviceId = ensureArray(searchingDeviceId);
for (const singleSearchingDeviceId of searchingDeviceId) {
const foundEntities = deviceEntityLookup[singleSearchingDeviceId];
if (!foundEntities?.length) {
continue;
}
for (const foundEntity of foundEntities) {
if (foundEntity.entity_category === null) {
entityIds.add(foundEntity.entity_id);
}
}
}
}
if (searchingEntityId) {
searchingEntityId = ensureArray(searchingEntityId);
for (const singleSearchingEntityId of searchingEntityId) {
entityIds.add(singleSearchingEntityId);
}
}
return [...entityIds];
} }
); );
@ -639,6 +609,12 @@ class HaPanelHistory extends SubscribeMixin(LitElement) {
"," ","
); );
} }
if (this._targetPickerValue.label_id) {
params.label_id = ensureArray(this._targetPickerValue.label_id).join(",");
}
if (this._targetPickerValue.floor_id) {
params.floor_id = ensureArray(this._targetPickerValue.floor_id).join(",");
}
if (this._targetPickerValue.area_id) { if (this._targetPickerValue.area_id) {
params.area_id = ensureArray(this._targetPickerValue.area_id).join(","); params.area_id = ensureArray(this._targetPickerValue.area_id).join(",");
} }