@@ -2,7 +2,7 @@ import assert from 'node:assert';
2
2
3
3
import { logger } from '../../utils/logger' ;
4
4
import * as Zcl from '../../zspec/zcl' ;
5
- import { CustomClusters } from '../../zspec/zcl/definition/tstype' ;
5
+ import { Cluster , CustomClusters } from '../../zspec/zcl/definition/tstype' ;
6
6
import ZclTransactionSequenceNumber from '../helpers/zclTransactionSequenceNumber' ;
7
7
import { DatabaseEntry , KeyValue } from '../tstype' ;
8
8
import Device from './device' ;
@@ -186,7 +186,7 @@ export class Group extends Entity {
186
186
187
187
public async write ( clusterKey : number | string , attributes : KeyValue , options ?: Options ) : Promise < void > {
188
188
const optionsWithDefaults = this . getOptionsWithDefaults ( options , Zcl . Direction . CLIENT_TO_SERVER ) ;
189
- const cluster = Zcl . Utils . getCluster ( clusterKey , undefined , this . customClusters ) ;
189
+ const cluster = this . getCluster ( clusterKey ) ;
190
190
const payload : { attrId : number ; dataType : number ; attrData : number | string | boolean } [ ] = [ ] ;
191
191
192
192
for ( const [ nameOrID , value ] of Object . entries ( attributes ) ) {
@@ -214,7 +214,7 @@ export class Group extends Entity {
214
214
'write' ,
215
215
cluster . ID ,
216
216
payload ,
217
- this . customClusters ,
217
+ this . _customClusters ?? { } ,
218
218
optionsWithDefaults . reservedBits ,
219
219
) ;
220
220
@@ -230,7 +230,7 @@ export class Group extends Entity {
230
230
231
231
public async read ( clusterKey : number | string , attributes : ( string | number ) [ ] , options ?: Options ) : Promise < void > {
232
232
const optionsWithDefaults = this . getOptionsWithDefaults ( options , Zcl . Direction . CLIENT_TO_SERVER ) ;
233
- const cluster = Zcl . Utils . getCluster ( clusterKey , undefined , this . customClusters ) ;
233
+ const cluster = this . getCluster ( clusterKey ) ;
234
234
const payload : { attrId : number } [ ] = [ ] ;
235
235
236
236
for ( const attribute of attributes ) {
@@ -246,7 +246,7 @@ export class Group extends Entity {
246
246
'read' ,
247
247
cluster . ID ,
248
248
payload ,
249
- this . customClusters ,
249
+ this . _customClusters ?? { } ,
250
250
optionsWithDefaults . reservedBits ,
251
251
) ;
252
252
@@ -267,7 +267,7 @@ export class Group extends Entity {
267
267
268
268
public async command ( clusterKey : number | string , commandKey : number | string , payload : KeyValue , options ?: Options ) : Promise < void > {
269
269
const optionsWithDefaults = this . getOptionsWithDefaults ( options , Zcl . Direction . CLIENT_TO_SERVER ) ;
270
- const cluster = Zcl . Utils . getCluster ( clusterKey , undefined , this . customClusters ) ;
270
+ const cluster = this . getCluster ( clusterKey ) ;
271
271
const command = cluster . getCommand ( commandKey ) ;
272
272
273
273
const createLogMessage = ( ) : string => `Command ${ this . groupID } ${ cluster . name } .${ command . name } (${ JSON . stringify ( payload ) } )` ;
@@ -283,7 +283,7 @@ export class Group extends Entity {
283
283
command . ID ,
284
284
cluster . ID ,
285
285
payload ,
286
- this . customClusters ,
286
+ this . _customClusters ?? { } ,
287
287
optionsWithDefaults . reservedBits ,
288
288
) ;
289
289
@@ -309,15 +309,11 @@ export class Group extends Entity {
309
309
}
310
310
311
311
/**
312
- * Get custom clusters that all members share.
312
+ * Calculate, store, and return custom clusters that all members share.
313
313
*/
314
- get customClusters ( ) : CustomClusters {
315
- if ( this . _customClusters ) {
316
- return this . _customClusters ;
317
- }
318
-
314
+ private calculateCustomClusters ( ) : CustomClusters {
319
315
if ( this . _members . size === 0 ) {
320
- return { } ;
316
+ return ( this . _customClusters = { } ) ;
321
317
}
322
318
323
319
const membersArray = Array . from ( this . _members ) ;
@@ -331,8 +327,24 @@ export class Group extends Entity {
331
327
}
332
328
}
333
329
334
- this . _customClusters = commonClusters ;
335
- return this . _customClusters ;
330
+ return ( this . _customClusters = commonClusters ) ;
331
+ }
332
+
333
+ private getCluster ( key : string | number ) : Cluster {
334
+ if ( this . _customClusters ) {
335
+ return Zcl . Utils . getCluster ( key , undefined , this . _customClusters ) ;
336
+ }
337
+
338
+ // At first, don't fully calculate custom clusters
339
+ const cluster = Zcl . Utils . findCluster ( key , undefined , { } ) ;
340
+
341
+ // If no cluster was found, and we haven't calculated custom clusters,
342
+ // do so now, and then retry
343
+ if ( ! cluster ) {
344
+ return Zcl . Utils . getCluster ( key , undefined , this . calculateCustomClusters ( ) ) ;
345
+ }
346
+
347
+ return cluster ;
336
348
}
337
349
}
338
350
0 commit comments