@@ -523,49 +523,51 @@ export async function findDefinition(device: Zh.Device, generateForUnknown = fal
523
523
}
524
524
}
525
525
526
- if ( ! candidates ) {
527
- if ( ! generateForUnknown || device . type === "Coordinator" ) {
528
- return undefined ;
526
+ if ( candidates ) {
527
+ if ( candidates . length === 1 && candidates [ 0 ] . zigbeeModel ) {
528
+ return candidates [ 0 ] ;
529
529
}
530
530
531
- // Do not add this definition to cache, as device configuration might change.
532
- const { definition} = await generateDefinition ( device ) ;
531
+ logger . debug ( ( ) => `Candidates for ${ device . ieeeAddr } /${ device . modelID } : ${ candidates . map ( ( c ) => `${ c . model } /${ c . vendor } ` ) } ` , NS ) ;
533
532
534
- return definition ;
535
- }
536
- if ( candidates . length === 1 && candidates [ 0 ] . zigbeeModel ) {
537
- return candidates [ 0 ] ;
538
- }
539
- logger . debug ( ( ) => `Candidates for ${ device . ieeeAddr } /${ device . modelID } : ${ candidates . map ( ( c ) => `${ c . model } /${ c . vendor } ` ) } ` , NS ) ;
533
+ // First try to match based on fingerprint, return the first matching one.
534
+ const fingerprintMatch : { priority ?: number ; definition ?: DefinitionWithExtend } = { priority : undefined , definition : undefined } ;
540
535
541
- // First try to match based on fingerprint, return the first matching one.
542
- const fingerprintMatch : { priority ?: number ; definition ?: DefinitionWithExtend } = { priority : undefined , definition : undefined } ;
536
+ for ( const candidate of candidates ) {
537
+ if ( candidate . fingerprint ) {
538
+ for ( const fingerprint of candidate . fingerprint ) {
539
+ const priority = fingerprint . priority ?? 0 ;
543
540
544
- for ( const candidate of candidates ) {
545
- if ( candidate . fingerprint ) {
546
- for ( const fingerprint of candidate . fingerprint ) {
547
- const priority = fingerprint . priority ?? 0 ;
548
-
549
- if ( isFingerprintMatch ( fingerprint , device ) && ( fingerprintMatch . priority === undefined || priority > fingerprintMatch . priority ) ) {
550
- fingerprintMatch . definition = candidate ;
551
- fingerprintMatch . priority = priority ;
541
+ if (
542
+ isFingerprintMatch ( fingerprint , device ) &&
543
+ ( fingerprintMatch . priority === undefined || priority > fingerprintMatch . priority )
544
+ ) {
545
+ fingerprintMatch . definition = candidate ;
546
+ fingerprintMatch . priority = priority ;
547
+ }
552
548
}
553
549
}
554
550
}
555
- }
556
551
557
- if ( fingerprintMatch . definition ) {
558
- return fingerprintMatch . definition ;
559
- }
552
+ if ( fingerprintMatch . definition ) {
553
+ return fingerprintMatch . definition ;
554
+ }
560
555
561
- // Match based on fingerprint failed, return first matching definition based on zigbeeModel
562
- for ( const candidate of candidates ) {
563
- if ( candidate . zigbeeModel && device . modelID && candidate . zigbeeModel . includes ( device . modelID ) ) {
564
- return candidate ;
556
+ // Match based on fingerprint failed, return first matching definition based on zigbeeModel
557
+ for ( const candidate of candidates ) {
558
+ if ( candidate . zigbeeModel && device . modelID && candidate . zigbeeModel . includes ( device . modelID ) ) {
559
+ return candidate ;
560
+ }
565
561
}
566
562
}
567
563
568
- return undefined ;
564
+ if ( ! generateForUnknown || device . type === "Coordinator" ) {
565
+ return undefined ;
566
+ }
567
+
568
+ const { definition} = await generateDefinition ( device ) ;
569
+
570
+ return definition ;
569
571
}
570
572
571
573
export async function generateExternalDefinitionSource ( device : Zh . Device ) : Promise < string > {
0 commit comments