@@ -18,7 +18,6 @@ import {
1818 Zigbee2MqttProperty ,
1919 WRITE_BIT ,
2020 parseType ,
21- isReadable ,
2221 parseUnit ,
2322 HeatingCoolingProperty ,
2423} from './zigbee2mqtt-property' ;
@@ -29,7 +28,7 @@ function debug(): boolean {
2928 return DEBUG_FLAG . DEBUG_zigbee2mqtt ;
3029}
3130
32- const IGNORED_PROPERTIES = [ 'linkquality' , 'local_temperature_calibration' , 'running_state' ] ;
31+ const IGNORED_PROPERTIES = [ 'linkquality' , 'local_temperature_calibration' ] ;
3332
3433export class Zigbee2MqttDevice extends Device {
3534 private deviceTopic : string ;
@@ -59,56 +58,6 @@ export class Zigbee2MqttDevice extends Device {
5958 } else {
6059 this . setTitle ( `Zigbee2MQTT (${ id } )` ) ;
6160 }
62-
63- const exposes = deviceDefinition ?. definition ?. exposes ;
64-
65- if ( Array . isArray ( exposes ) ) {
66- const properties : Record < string , unknown > = this . getKeys ( exposes ) ;
67-
68- if ( Object . keys ( properties ) . length > 0 ) {
69- const readTopic = `${ this . deviceTopic } /get` ;
70- const readPayload = JSON . stringify ( properties ) ;
71-
72- client . publish ( readTopic , readPayload , ( error ) => {
73- if ( error ) {
74- console . warn ( `Could not send ${ readPayload } to ${ readTopic } : ${ console . error ( ) } ` ) ;
75- }
76- } ) ;
77- }
78- }
79- }
80-
81- private getKeys ( exposes : Expos [ ] ) : Record < string , unknown > {
82- let properties : Record < string , unknown > = { } ;
83-
84- for ( const expose of exposes ) {
85- if ( expose . name ) {
86- if ( this . hasReadableProperties ( expose ) ) {
87- properties [ expose . name ] = '' ;
88- }
89- } else if ( Array . isArray ( expose . features ) ) {
90- properties = {
91- ...properties ,
92- ...this . getKeys ( expose . features ) ,
93- } ;
94- }
95- }
96-
97- return properties ;
98- }
99-
100- private hasReadableProperties ( expose : Expos ) : boolean {
101- if ( typeof expose . access === 'number' ) {
102- return isReadable ( expose . access ) ;
103- } else if ( Array . isArray ( expose . features ) ) {
104- for ( const feature of expose . features ) {
105- if ( this . hasReadableProperties ( feature ) ) {
106- return true ;
107- }
108- }
109- }
110-
111- return false ;
11261 }
11362
11463 protected detectProperties ( deviceDefinition : DeviceDefinition ) : void {
@@ -456,4 +405,35 @@ export class Zigbee2MqttDevice extends Device {
456405 } ) ;
457406 } ) ;
458407 }
408+
409+ fetchValues ( ) : void {
410+ const { properties } = ( this as unknown ) as {
411+ properties : Map < string , Zigbee2MqttProperty < PropertyValue > > ;
412+ } ;
413+
414+ const payload : Record < string , string > = { } ;
415+
416+ for ( const property of properties . values ( ) ) {
417+ if ( property . isReadable ( ) ) {
418+ payload [ property . getName ( ) ] = '' ;
419+ }
420+ }
421+
422+ if ( Object . keys ( payload ) . length > 0 ) {
423+ const readTopic = `${ this . deviceTopic } /get` ;
424+ const readPayload = JSON . stringify ( payload ) ;
425+
426+ if ( debug ( ) ) {
427+ console . log ( `Sending ${ readPayload } to ${ readTopic } ` ) ;
428+ }
429+
430+ this . client . publish ( readTopic , readPayload , ( error ) => {
431+ if ( error ) {
432+ console . warn ( `Could not send ${ readPayload } to ${ readTopic } : ${ console . error ( ) } ` ) ;
433+ }
434+ } ) ;
435+ } else if ( debug ( ) ) {
436+ console . log ( `${ this . getTitle ( ) } has no readable properties` ) ;
437+ }
438+ }
459439}
0 commit comments