@@ -516,6 +516,86 @@ function zapTypeToPythonClusterObjectType(type, options)
516
516
return _zapTypeToPythonClusterObjectType . call ( this , type , options )
517
517
}
518
518
519
+ async function _getPythonFieldDefault ( type , options )
520
+ {
521
+ async function fn ( pkgId )
522
+ {
523
+ const ns = options . hash . ns ;
524
+ const typeChecker = async ( method ) => zclHelper [ method ] ( this . global . db , type , pkgId ) . then ( zclType => zclType != 'unknown' ) ;
525
+
526
+ if ( await typeChecker ( 'isEnum' ) ) {
527
+ return '0' ;
528
+ }
529
+
530
+ if ( await typeChecker ( 'isBitmap' ) ) {
531
+ return '0' ;
532
+ }
533
+
534
+ if ( await typeChecker ( 'isStruct' ) ) {
535
+ return 'field(default_factory=lambda: ' + ns + '.Structs.' + type + '())' ;
536
+ }
537
+
538
+ if ( StringHelper . isCharString ( type ) ) {
539
+ return '""' ;
540
+ }
541
+
542
+ if ( StringHelper . isOctetString ( type ) ) {
543
+ return 'b""' ;
544
+ }
545
+
546
+ if ( [ 'single' , 'double' ] . includes ( type . toLowerCase ( ) ) ) {
547
+ return '0.0' ;
548
+ }
549
+
550
+ if ( type . toLowerCase ( ) == 'boolean' ) {
551
+ return 'False'
552
+ }
553
+
554
+ // #10748: asUnderlyingZclType will emit wrong types for int{48|56|64}(u), so we process all int values here.
555
+ if ( type . toLowerCase ( ) . match ( / ^ i n t \d + $ / ) ) {
556
+ return '0'
557
+ }
558
+
559
+ if ( type . toLowerCase ( ) . match ( / ^ i n t \d + u $ / ) ) {
560
+ return '0'
561
+ }
562
+
563
+ resolvedType = await zclHelper . asUnderlyingZclType . call ( { global : this . global } , type , options ) ;
564
+ {
565
+ basicType = ChipTypesHelper . asBasicType ( resolvedType ) ;
566
+ if ( basicType . match ( / ^ i n t \d + _ t $ / ) ) {
567
+ return '0'
568
+ }
569
+ if ( basicType . match ( / ^ u i n t \d + _ t $ / ) ) {
570
+ return '0'
571
+ }
572
+ }
573
+ }
574
+
575
+ let promise = templateUtil . ensureZclPackageId ( this ) . then ( fn . bind ( this ) ) ;
576
+ if ( ( this . isList || this . isArray || this . entryType ) && ! options . hash . forceNotList ) {
577
+ promise = promise . then ( typeStr => `field(default_factory=lambda: [])` ) ;
578
+ }
579
+
580
+ const isNull = ( this . isNullable && ! options . hash . forceNotNullable ) ;
581
+ const isOptional = ( this . isOptional && ! options . hash . forceNotOptional ) ;
582
+
583
+ if ( isNull && isOptional ) {
584
+ promise = promise . then ( typeStr => `None` ) ;
585
+ } else if ( isNull ) {
586
+ promise = promise . then ( typeStr => `NullValue` ) ;
587
+ } else if ( isOptional ) {
588
+ promise = promise . then ( typeStr => `None` ) ;
589
+ }
590
+
591
+ return templateUtil . templatePromise ( this . global , promise )
592
+ }
593
+
594
+ function getPythonFieldDefault ( type , options )
595
+ {
596
+ return _getPythonFieldDefault . call ( this , type , options )
597
+ }
598
+
519
599
async function getResponseCommandName ( responseRef , options )
520
600
{
521
601
let pkgId = await templateUtil . ensureZclPackageId ( this ) ;
@@ -620,3 +700,4 @@ exports.zapTypeToDecodableClusterObjectType = zapTypeToDecodableClusterObjectTyp
620
700
exports . zapTypeToPythonClusterObjectType = zapTypeToPythonClusterObjectType ;
621
701
exports . getResponseCommandName = getResponseCommandName ;
622
702
exports . isWeaklyTypedEnum = isWeaklyTypedEnum ;
703
+ exports . getPythonFieldDefault = getPythonFieldDefault ;
0 commit comments