@@ -725,8 +725,12 @@ export default class Bridge extends Extension {
725
725
scenes : Scene [ ] ;
726
726
}
727
727
728
- const devices = this . zigbee . devices ( ) . map ( ( device ) => {
728
+ // XXX: definition<>DefinitionPayload don't match to use `Device[]` type here
729
+ const devices : KeyValue [ ] = [ ] ;
730
+
731
+ for ( const device of this . zigbee . devicesIterator ( ) ) {
729
732
const endpoints : { [ s : number ] : Data } = { } ;
733
+
730
734
for ( const endpoint of device . zh . endpoints ) {
731
735
const data : Data = {
732
736
scenes : utils . getScenes ( endpoint ) ,
@@ -758,7 +762,7 @@ export default class Bridge extends Extension {
758
762
endpoints [ endpoint . ID ] = data ;
759
763
}
760
764
761
- return {
765
+ devices . push ( {
762
766
ieee_address : device . ieeeAddr ,
763
767
type : device . zh . type ,
764
768
network_address : device . zh . networkAddress ,
@@ -775,24 +779,32 @@ export default class Bridge extends Extension {
775
779
interview_completed : device . zh . interviewCompleted ,
776
780
manufacturer : device . zh . manufacturerName ,
777
781
endpoints,
778
- } ;
779
- } ) ;
782
+ } ) ;
783
+ }
780
784
781
785
await this . mqtt . publish ( 'bridge/devices' , stringify ( devices ) , { retain : true , qos : 0 } , settings . get ( ) . mqtt . base_topic , true ) ;
782
786
}
783
787
784
788
async publishGroups ( ) : Promise < void > {
785
- const groups = this . zigbee . groups ( ) . map ( ( g ) => {
786
- return {
787
- id : g . ID ,
788
- friendly_name : g . ID === 901 ? 'default_bind_group' : g . name ,
789
- description : g . options . description ,
790
- scenes : utils . getScenes ( g . zh ) ,
791
- members : g . zh . members . map ( ( e ) => {
792
- return { ieee_address : e . getDevice ( ) . ieeeAddr , endpoint : e . ID } ;
793
- } ) ,
794
- } ;
795
- } ) ;
789
+ // XXX: id<>ID can't use `Group[]` type
790
+ const groups : KeyValue [ ] = [ ] ;
791
+
792
+ for ( const group of this . zigbee . groupsIterator ( ) ) {
793
+ const members = [ ] ;
794
+
795
+ for ( const member of group . zh . members ) {
796
+ members . push ( { ieee_address : member . getDevice ( ) . ieeeAddr , endpoint : member . ID } ) ;
797
+ }
798
+
799
+ groups . push ( {
800
+ id : group . ID ,
801
+ friendly_name : group . ID === 901 ? 'default_bind_group' : group . name ,
802
+ description : group . options . description ,
803
+ scenes : utils . getScenes ( group . zh ) ,
804
+ members,
805
+ } ) ;
806
+ }
807
+
796
808
await this . mqtt . publish ( 'bridge/groups' , stringify ( groups ) , { retain : true , qos : 0 } , settings . get ( ) . mqtt . base_topic , true ) ;
797
809
}
798
810
@@ -807,20 +819,23 @@ export default class Bridge extends Extension {
807
819
custom_clusters : { } ,
808
820
} ;
809
821
810
- for ( const device of this . zigbee . devices ( ) ) {
811
- if ( Object . keys ( device . customClusters ) . length !== 0 ) {
812
- data . custom_clusters [ device . ieeeAddr ] = device . customClusters ;
813
- }
822
+ for ( const device of this . zigbee . devicesIterator ( ( d ) => ! utils . objectIsEmpty ( d . customClusters ) ) ) {
823
+ data . custom_clusters [ device . ieeeAddr ] = device . customClusters ;
814
824
}
815
825
816
826
await this . mqtt . publish ( 'bridge/definitions' , stringify ( data ) , { retain : true , qos : 0 } , settings . get ( ) . mqtt . base_topic , true ) ;
817
827
}
818
828
819
- getDefinitionPayload ( device : Device ) : DefinitionPayload {
820
- if ( ! device . definition ) return null ;
829
+ getDefinitionPayload ( device : Device ) : DefinitionPayload | null {
830
+ if ( ! device . definition ) {
831
+ return null ;
832
+ }
833
+
834
+ // TODO: better typing to avoid @ts -expect-error
821
835
// @ts -expect-error icon is valid for external definitions
822
836
const definitionIcon = device . definition . icon ;
823
837
let icon = device . options . icon ?? definitionIcon ;
838
+
824
839
if ( icon ) {
825
840
icon = icon . replace ( '${zigbeeModel}' , utils . sanitizeImageParameter ( device . zh . modelID ) ) ;
826
841
icon = icon . replace ( '${model}' , utils . sanitizeImageParameter ( device . definition . model ) ) ;
0 commit comments