@@ -169,19 +169,36 @@ export const computeSettingsDiff = (before: object, after: object) => {
169169
170170export const sanitizeZ2MDeviceName = ( deviceName ?: string ) : string => deviceName ? deviceName . replace ( / : | \s | \/ / g, "-" ) : "NA" ;
171171
172- export const getEndpoints = ( obj : Device | Group ) : Endpoint [ ] => {
173- let eps : Endpoint [ ] = [ ] ;
174- if ( ! obj ) {
175- return eps ;
176- } else if ( ( obj as Device ) . endpoints ) {
177- eps = eps . concat ( Object . keys ( ( obj as Device ) . endpoints ) as Endpoint [ ] ) ;
178- } else if ( ( obj as Group ) . members ) {
179- eps = eps . concat ( ( obj as Group ) . members . map ( g => g . endpoint ) ) ;
172+ export function isDevice ( entity : Device | Group ) : entity is Device {
173+ return ! ( "members" in entity ) ;
174+ }
175+
176+ export const getEndpoints = ( entity ?: Device | Group ) : Endpoint [ ] => {
177+ const endpoints = new Set < string | number > ( ) ;
178+
179+ if ( ! entity ) {
180+ return [ ...endpoints ] ;
180181 }
181- if ( ( obj as Device ) . definition ?. exposes ) {
182- eps = eps . concat ( ( obj as Device ) . definition ?. exposes ?. map ( e => e . endpoint ) . filter ( Boolean ) as Endpoint [ ] ) ;
182+
183+ if ( isDevice ( entity ) ) {
184+ for ( const key in entity . endpoints ) {
185+ endpoints . add ( Number . parseInt ( key , 10 ) ) ;
186+ }
187+
188+ if ( entity . definition ?. exposes ) {
189+ for ( const expose of entity . definition . exposes ) {
190+ if ( expose . endpoint ) {
191+ endpoints . add ( expose . endpoint ) ;
192+ }
193+ }
194+ }
195+ } else {
196+ for ( const member of entity . members ) {
197+ endpoints . add ( member . endpoint ) ;
198+ }
183199 }
184- return eps ;
200+
201+ return [ ...endpoints ] ;
185202}
186203
187204export const isDeviceDisabled = ( device : Device , config : Z2MConfig ) : boolean => ! ! ( config . device_options ?. disabled || config . devices [ device . ieee_address ] ?. disabled ) ;
0 commit comments