1
1
var miio = require ( 'miio' ) ;
2
- var Accessory , Service , Characteristic , UUIDGen ;
2
+ var Accessory , Service , Characteristic ;
3
3
var devices = [ ] ;
4
4
5
5
module . exports = function ( homebridge ) {
6
6
Accessory = homebridge . platformAccessory ;
7
7
Service = homebridge . hap . Service ;
8
8
Characteristic = homebridge . hap . Characteristic ;
9
- UUIDGen = homebridge . hap . uuid ;
10
9
11
10
homebridge . registerAccessory ( 'homebridge-mi-air-purifier' , 'MiAirPurifier' , MiAirPurifier ) ;
12
11
}
13
12
14
13
function MiAirPurifier ( log , config ) {
15
14
this . log = log ;
16
15
this . name = config . name || 'Air Purifier' ;
16
+ this . showAirQuality = config . showAirQuality || false ;
17
+ this . showTemperature = config . showTemperature || false ;
18
+ this . showHumidity = config . showTemperature || false ;
17
19
20
+ this . services = [ ] ;
21
+
22
+ // Modes supported
18
23
this . modes = [
19
24
[ 0 , 'idle' ] , [ 60 , 'auto' ] , [ 80 , 'silent' ] , [ 100 , 'favorite' ]
20
25
] ;
21
26
22
- // Air purifier is not available in Homekit yet, use as fan for now
27
+ // Air purifier is not available in Homekit yet, register as Fan
23
28
this . fanService = new Service . Fan ( this . name ) ;
24
29
25
- // Register another service as air quality sensor
26
- this . airQualitySensorService = new Service . AirQualitySensor ( 'Air Quality Sensor' ) ;
30
+ this . fanService
31
+ . getCharacteristic ( Characteristic . On )
32
+ . on ( 'get' , this . getPowerState . bind ( this ) )
33
+ . on ( 'set' , this . setPowerState . bind ( this ) ) ;
34
+
35
+ this . fanService
36
+ . getCharacteristic ( Characteristic . RotationSpeed )
37
+ . on ( 'get' , this . getRotationSpeed . bind ( this ) )
38
+ . on ( 'set' , this . setRotationSpeed . bind ( this ) ) ;
39
+
40
+ this . services . push ( this . fanService ) ;
27
41
28
42
this . serviceInfo = new Service . AccessoryInformation ( ) ;
29
43
30
44
this . serviceInfo
31
45
. setCharacteristic ( Characteristic . Manufacturer , 'Xiaomi' )
32
46
. setCharacteristic ( Characteristic . Model , 'Air Purifier' ) ;
47
+
48
+ this . services . push ( this . serviceInfo ) ;
33
49
34
- this . fanService
35
- . getCharacteristic ( Characteristic . On )
36
- . on ( 'get' , this . getOn . bind ( this ) )
37
- . on ( 'set' , this . setOn . bind ( this ) ) ;
50
+ if ( this . showAirQuality ) {
51
+ this . airQualitySensorService = new Service . AirQualitySensor ( 'Air Quality Sensor' ) ;
38
52
39
- this . fanService
40
- . getCharacteristic ( Characteristic . RotationSpeed )
41
- . on ( 'get' , this . getRotationSpeed . bind ( this ) )
42
- . on ( 'set' , this . setRotationSpeed . bind ( this ) ) ;
53
+ this . airQualitySensorService
54
+ . getCharacteristic ( Characteristic . AirQuality )
55
+ . on ( 'get' , this . getAirQuality . bind ( this ) ) ;
43
56
44
- this . fanService
45
- . addCharacteristic ( Characteristic . CurrentRelativeHumidity )
46
- . on ( 'get' , this . getCurrentRelativeHumidity . bind ( this ) ) ;
57
+ this . services . push ( this . airQualitySensorService ) ;
58
+ }
59
+
60
+ if ( this . showTemperature ) {
61
+ this . temperatureSensorService = new Service . TemperatureSensor ( 'Temperature' ) ;
62
+
63
+ this . temperatureSensorService
64
+ . getCharacteristic ( Characteristic . CurrentTemperature )
65
+ . on ( 'get' , this . getCurrentTemperature . bind ( this ) ) ;
66
+
67
+ this . services . push ( this . temperatureSensorService ) ;
68
+ }
47
69
48
- this . airQualitySensorService
49
- . getCharacteristic ( Characteristic . AirQuality )
50
- . on ( 'get' , this . getAirQuality . bind ( this ) ) ;
70
+ if ( this . showHumidity ) {
71
+ this . humiditySensorService = new Service . HumiditySensor ( 'Humidity' ) ;
72
+
73
+ this . humiditySensorService
74
+ . getCharacteristic ( Characteristic . CurrentRelativeHumidity )
75
+ . on ( 'get' , this . getCurrentRelativeHumidity . bind ( this ) ) ;
76
+
77
+ this . services . push ( this . humiditySensorService ) ;
78
+ }
51
79
52
80
this . discover ( ) ;
53
81
}
@@ -93,39 +121,54 @@ MiAirPurifier.prototype = {
93
121
} ) ;
94
122
} ,
95
123
96
- getOn : function ( callback ) {
124
+ getPowerState : function ( callback ) {
125
+ if ( ! this . device ) {
126
+ callback ( null , false ) ;
127
+ return ;
128
+ }
129
+
97
130
callback ( null , this . device . power ) ;
98
131
} ,
99
132
100
- setOn : function ( powerOn , callback ) {
133
+ setPowerState : function ( state , callback ) {
101
134
if ( ! this . device ) {
102
135
callback ( new Error ( 'No air purifier is discovered.' ) ) ;
103
136
return ;
104
137
}
105
138
106
- var accessory = this ;
107
-
108
- this . device . setPower ( powerOn )
109
- . then ( function ( state ) {
110
- callback ( null , state ) ;
111
- } ) ;
112
-
139
+ this . device . setPower ( state ) ;
113
140
callback ( ) ;
114
141
} ,
115
142
116
143
getCurrentRelativeHumidity : function ( callback ) {
144
+ if ( ! this . device ) {
145
+ callback ( null , 0 ) ;
146
+ return ;
147
+ }
148
+
117
149
callback ( null , this . device . humidity ) ;
118
150
} ,
119
151
120
152
getRotationSpeed : function ( callback ) {
153
+ if ( ! this . device ) {
154
+ callback ( null , 0 ) ;
155
+ return ;
156
+ }
157
+
121
158
for ( var item of this . modes ) {
122
159
if ( this . device . mode == item [ 1 ] ) {
123
160
callback ( null , item [ 0 ] ) ;
161
+ return ;
124
162
}
125
163
}
126
164
} ,
127
165
128
166
setRotationSpeed : function ( speed , callback ) {
167
+ if ( ! this . device ) {
168
+ callback ( new Error ( 'No air purifier is discovered.' ) ) ;
169
+ return ;
170
+ }
171
+
129
172
for ( var item of this . modes ) {
130
173
if ( speed <= item [ 0 ] ) {
131
174
this . log . debug ( 'Set mode: ' + item [ 1 ] ) ;
@@ -138,31 +181,45 @@ MiAirPurifier.prototype = {
138
181
} ,
139
182
140
183
getAirQuality : function ( callback ) {
141
- var airQuality = Characteristic . AirQuality . UNKNOWN ;
184
+ if ( ! this . device ) {
185
+ callback ( null , Characteristic . AirQuality . UNKNOWN ) ;
186
+ return ;
187
+ }
142
188
143
- if ( this . device . aqi > 200 )
144
- airQuality = Characteristic . AirQuality . POOR ;
189
+ var levels = [
190
+ [ 200 , Characteristic . AirQuality . POOR ] ,
191
+ [ 150 , Characteristic . AirQuality . INFERIOR ] ,
192
+ [ 100 , Characteristic . AirQuality . FAIR ] ,
193
+ [ 50 , Characteristic . AirQuality . GOOD ] ,
194
+ [ 0 , Characteristic . AirQuality . EXCELLENT ] ,
195
+ ] ;
145
196
146
- else if ( this . device . aqi > 150 )
147
- airQuality = Characteristic . AirQuality . INFERIOR ;
197
+ var quality = Characteristic . AirQuality . UNKNOWN ;
148
198
149
- else if ( this . device . aqi > 100 )
150
- airQuality = Characteristic . AirQuality . FAIR ;
199
+ for ( var item of levels ) {
200
+ if ( this . device . aqi > item [ 0 ] ) {
201
+ quality = item [ 1 ] ;
202
+ break ;
203
+ }
204
+ }
151
205
152
- else if ( this . device . aqi > 50 )
153
- airQuality = Characteristic . AirQuality . GOOD ;
206
+ callback ( null , quality ) ;
207
+ } ,
154
208
155
- else
156
- airQuality = Characteristic . AirQuality . EXCELLENT ;
209
+ getCurrentTemperature : function ( callback ) {
210
+ if ( ! this . device ) {
211
+ callback ( null , 0 ) ;
212
+ return ;
213
+ }
157
214
158
- callback ( null , airQuality ) ;
215
+ callback ( null , this . device . temperature ) ;
159
216
} ,
160
217
161
218
identify : function ( callback ) {
162
219
callback ( ) ;
163
220
} ,
164
221
165
222
getServices : function ( ) {
166
- return [ this . serviceInfo , this . fanService , this . airQualitySensorService ] ;
223
+ return this . services ;
167
224
}
168
225
} ;
0 commit comments