From 6f81e7da65923ee720dda3ced12adce1007067db Mon Sep 17 00:00:00 2001 From: DigiLive Date: Fri, 16 May 2025 08:13:44 +0200 Subject: [PATCH] Fix showing entities twice Entities with an explicit area id where shown twice. Once because of the explicit area and once because of its device's area. Closes #207. --- src/utilities/RegistryFilter.ts | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/utilities/RegistryFilter.ts b/src/utilities/RegistryFilter.ts index 0041a51..87a8933 100644 --- a/src/utilities/RegistryFilter.ts +++ b/src/utilities/RegistryFilter.ts @@ -67,40 +67,33 @@ class RegistryFilter { } /** - * Filters entries **strictly** by their `area_id`. - * - * - Entries with a matching `area_id` are kept. - * - If `expandToDevice` is `true`, the device's `area_id` is evaluated if the entry's area_id doesn't match. - * - If `areaId` is `undefined` (or omitted), entries without an `area_id` property are kept. + * Filters entries by their `area_id`. * * @param {string | undefined} areaId - The area id to match. - * @param {boolean} [expandToDevice=true] - Whether to use the device's `area_id` if the entry's doesn't match. + * @param {boolean} [expandToDevice=true] - Whether to evaluate the device's `area_id` (see remarks). * * @remarks - * For area id `undisclosed`, the `area_id` of the entry's device may be `undisclosed` or `undefined`. + * For entries with area id `undisclosed` or `undefined`, the device's `area_id` must also match if `expandToDevice` + * is `true`. */ whereAreaId(areaId?: string, expandToDevice: boolean = true): this { const predicate = (entry: T) => { let deviceAreaId: string | null | undefined = undefined; const entryObject = entry as EntityRegistryEntry; - // Retrieve the device area ID only if expandToDevice is true if (expandToDevice && entryObject.device_id) { deviceAreaId = Registry.devices.find((device) => device.id === entryObject.device_id)?.area_id; } - // Logic for 'undisclosed' areaId - if (areaId === 'undisclosed') { - return entry.area_id === areaId && (deviceAreaId === areaId || deviceAreaId === undefined); - } - - // Logic for undefined areaId if (areaId === undefined) { - return entry.area_id === undefined && (!expandToDevice || deviceAreaId === undefined); + return entry.area_id === undefined && deviceAreaId === undefined; } - // Logic for any other areaId - return entry.area_id === areaId || (expandToDevice && deviceAreaId === areaId); + if (entry.area_id === 'undisclosed' || !entry.area_id) { + return deviceAreaId === areaId; + } + + return entry.area_id === areaId; }; this.filters.push(this.checkInversion(predicate));