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