@@ -9,16 +9,28 @@ type EntityCategory = "none" | "config" | "diagnostic";
99export interface EntityFilter {
1010 domain ?: string | string [ ] ;
1111 device_class ?: string | string [ ] ;
12- device ?: string | string [ ] ;
13- area ?: string | string [ ] ;
14- floor ?: string | string [ ] ;
12+ device ?: string | null | ( string | null ) [ ] ;
13+ area ?: string | null | ( string | null ) [ ] ;
14+ floor ?: string | null | ( string | null ) [ ] ;
1515 label ?: string | string [ ] ;
1616 entity_category ?: EntityCategory | EntityCategory [ ] ;
1717 hidden_platform ?: string | string [ ] ;
1818}
1919
2020export type EntityFilterFunc = ( entityId : string ) => boolean ;
2121
22+ const normalizeFilterArray = < T > (
23+ value : T | null | T [ ] | ( T | null ) [ ] | undefined
24+ ) : Set < T | null > | undefined => {
25+ if ( value === undefined ) {
26+ return undefined ;
27+ }
28+ if ( value === null ) {
29+ return new Set ( [ null ] ) ;
30+ }
31+ return new Set ( ensureArray ( value ) ) ;
32+ } ;
33+
2234export const generateEntityFilter = (
2335 hass : HomeAssistant ,
2436 filter : EntityFilter
@@ -29,11 +41,9 @@ export const generateEntityFilter = (
2941 const deviceClasses = filter . device_class
3042 ? new Set ( ensureArray ( filter . device_class ) )
3143 : undefined ;
32- const floors = filter . floor ? new Set ( ensureArray ( filter . floor ) ) : undefined ;
33- const areas = filter . area ? new Set ( ensureArray ( filter . area ) ) : undefined ;
34- const devices = filter . device
35- ? new Set ( ensureArray ( filter . device ) )
36- : undefined ;
44+ const floors = normalizeFilterArray ( filter . floor ) ;
45+ const areas = normalizeFilterArray ( filter . area ) ;
46+ const devices = normalizeFilterArray ( filter . device ) ;
3747 const entityCategories = filter . entity_category
3848 ? new Set ( ensureArray ( filter . entity_category ) )
3949 : undefined ;
@@ -73,23 +83,20 @@ export const generateEntityFilter = (
7383 }
7484
7585 if ( floors ) {
76- if ( ! floor || ! floors . has ( floor . floor_id ) ) {
86+ const floorId = floor ?. floor_id ?? null ;
87+ if ( ! floors . has ( floorId ) ) {
7788 return false ;
7889 }
7990 }
8091 if ( areas ) {
81- if ( ! area ) {
82- return false ;
83- }
84- if ( ! areas . has ( area . area_id ) ) {
92+ const areaId = area ?. area_id ?? null ;
93+ if ( ! areas . has ( areaId ) ) {
8594 return false ;
8695 }
8796 }
8897 if ( devices ) {
89- if ( ! device ) {
90- return false ;
91- }
92- if ( ! devices . has ( device . id ) ) {
98+ const deviceId = device ?. id ?? null ;
99+ if ( ! devices . has ( deviceId ) ) {
93100 return false ;
94101 }
95102 }
0 commit comments