forked from nt0xa/homebridge-mi-humidifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
347 lines (284 loc) · 9.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
const miio = require('miio')
const defaults = {
model: 'v1',
name: 'Humidifier',
showTemperature: false,
nameTemperature: 'Temperature',
showHumidity: false,
nameHumidity: 'Humidity'
}
// Note: the `auto` mode can be set only for the Smartmi Evaporative Humidifier
const speedLevels = ['off', 'silent', 'medium', 'high', 'auto']
let Service, Characteristic
module.exports = homebridge => {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-mi-humidifier', 'MiHumidifier', MiHumidifier)
}
class MiHumidifier {
constructor(log, config) {
if (!config.ip) throw new Error('Your must provide IP address of the Humidifier')
if (!config.token) throw new Error('Your must provide token of the Humidifier')
let options = { ...defaults, ...config },
info = new Service.AccessoryInformation(),
device = new Service.HumidifierDehumidifier(options.name),
isModel2 = /ca1|cb1/.test(options.model)
this.log = log
this.ip = config.ip
this.token = config.token
this.services = [device, info]
// Device info
info
.setCharacteristic(Characteristic.Manufacturer, 'Xiaomi')
.setCharacteristic(Characteristic.Model, 'Humidifier')
.setCharacteristic(Characteristic.SerialNumber, 'Undefined')
// Active
device
.getCharacteristic(Characteristic.Active)
.on('get', this.getActive.bind(this))
.on('set', this.setActive.bind(this))
// Current state
device
.getCharacteristic(Characteristic.CurrentHumidifierDehumidifierState)
.on('get', this.getCurrentHumidifierState.bind(this))
// Target state (only humidifier is supported)
device
.getCharacteristic(Characteristic.TargetHumidifierDehumidifierState)
.setValue(Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER)
// Current relative humidity
device
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on('get', this.getCurrentRelativeHumidity.bind(this))
// Target relative humidity
// Note: this Characteristic cannot be viewed in the Home.app, but it can be changed using Siri Voice Commands or by using some 3rd Party HomeKit apps
device
.addCharacteristic(Characteristic.TargetRelativeHumidity)
.on('get', this.getTargetRelativeHumidity.bind(this))
.on('set', this.setTargetRelativeHumidity.bind(this))
// Current water level (remaining water level)
// Note: this characteristic works only for Smartmi Evaporative Humidifier
isModel2 && device
.getCharacteristic(Characteristic.WaterLevel)
.on('get', this.getWaterLevel.bind(this))
// Rotation speed
device
.getCharacteristic(Characteristic.RotationSpeed)
.setProps({
minValue: 0,
maxValue: isModel2 ? 4 : 3,
minStep: 1
})
.on('get', this.getRotationSpeed.bind(this))
.on('set', this.setRotationSpeed.bind(this))
// Child lock
// Note: this characteristic works only for Smartmi Evaporative Humidifier
isModel2 && device
.addCharacteristic(Characteristic.LockPhysicalControls)
.on('get', this.getLockPhysicalControls.bind(this))
.on('set', this.setLockPhysicalControls.bind(this))
// Drying mode
// Note: this characteristic works only for Smartmi Evaporative Humidifier
// TODO: maybe here we need to use something else instead of SwingMode, but this is the closest Characteristic type
isModel2 && device
.addCharacteristic(Characteristic.SwingMode)
.on('get', this.getDryingMode.bind(this))
.on('set', this.setDryingMode.bind(this))
// Temperature sensor
if (options.showTemperature) {
let temperature = new Service.TemperatureSensor(options.nameTemperature),
handler = options.model === 'cb1' ? this.getCurrentTemperatureCB1 : this.getCurrentTemperature
temperature
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', handler.bind(this))
this.services.push(temperature)
}
// Humidity sensor
if (options.showHumidity){
let humidity = new Service.HumiditySensor(options.nameHumidity)
humidity
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on('get', this.getCurrentRelativeHumidity.bind(this))
this.services.push(humidity)
}
this.discover()
}
getServices() {
return this.services
}
async discover() {
try {
this.device = await miio.device({ address: this.ip, token: this.token })
} catch (e) {
this.log.error('Fail to discover the device. Retry in 1 minute', e)
setTimeout(() => { this.discover() }, 60000)
}
}
async getActive(callback) {
try {
const [ power ] = await this.device.call('get_prop', ['power']),
state = power === 'on' ? Characteristic.Active.ACTIVE : Characteristic.Active.INACTIVE
callback(null, state)
} catch (e) {
this.log.error('getActive', e)
callback(e)
}
}
async setActive(state, callback) {
try {
const power = state === Characteristic.Active.ACTIVE ? 'on' : 'off',
[ result ] = await this.device.call('set_power', [power])
if (result !== 'ok')
throw new Error(result)
callback()
} catch (e) {
this.log.error('setActive', e)
callback(e)
}
}
async getCurrentHumidifierState(callback) {
try {
const [ power ] = await this.device.call('get_prop', ['power']),
state = power === 'on'
? Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING
: Characteristic.CurrentHumidifierDehumidifierState.INACTIVE
callback(null, state)
} catch (e) {
this.log.error('getCurrentHumidifierState', e)
callback(e)
}
}
async getCurrentRelativeHumidity(callback) {
try {
const [ humidity ] = await this.device.call('get_prop', ['humidity'])
callback(null, humidity)
} catch (e) {
this.log.error('getCurrentRelativeHumidity', e)
callback(e)
}
}
async getTargetRelativeHumidity(callback) {
try {
const [ limit_hum ] = await this.device.call('get_prop', ['limit_hum'])
callback(null, limit_hum)
} catch (e) {
this.log.error('getTargetRelativeHumidity', e)
callback(e)
}
}
async setTargetRelativeHumidity(value, callback) {
try {
const [ result ] = await this.device.call('set_limit_hum', [value])
if (result !== 'ok')
throw new Error(result)
callback()
} catch (e) {
this.log.error('setTargetRelativeHumidity', e)
callback(e)
}
}
async getWaterLevel(callback) {
try {
const [ waterLevel ] = await this.device.call('get_prop', ['depth'])
callback(null, waterLevel / 1.2)
} catch (e) {
this.log.error('getWaterLevel', e)
callback(e)
}
}
async getRotationSpeed(callback) {
try {
const [ mode ] = await this.device.call('get_prop', ['mode']),
speed = speedLevels.findIndex(item => item === mode)
callback(null, speed)
} catch (e) {
this.log.error('getRotationSpeed', e)
callback(e)
}
}
async setRotationSpeed(value, callback) {
try {
const [ power ] = await this.device.call('get_prop', ['power'])
let result
if (value > 0) {
if (power === 'off') {
await this.device.call('set_power', ['on'])
}
[ result ] = await this.device.call('set_mode', [speedLevels[value]])
} else {
[ result ] = await this.device.call('set_power', ['off'])
}
if (result !== 'ok')
throw new Error(result)
callback()
} catch (e) {
this.log.error('setRotationSpeed', e)
callback(e)
}
}
async getCurrentTemperature(callback) {
try {
const [ temperature ] = await this.device.call('get_prop', ['temp_dec'])
callback(null, temperature / 10)
} catch (e) {
this.log.error('getCurrentTemperature', e)
callback(e)
}
}
async getCurrentTemperatureCB1(callback) {
try {
const [ temperature ] = await this.device.call('get_prop', ['temperature'])
callback(null, temperature)
} catch (e) {
this.log.error('getCurrentTemperatureCB1', e)
callback(e)
}
}
async getLockPhysicalControls(callback) {
try {
const [ locked ] = await this.device.call('get_prop', ['child_lock']),
state = locked === 'on'
? Characteristic.LockPhysicalControls.CONTROL_LOCK_ENABLED
: Characteristic.LockPhysicalControls.CONTROL_LOCK_DISABLED
callback(null, state)
} catch (e) {
this.log.error('getLockPhysicalControls', e)
callback(e)
}
}
async setLockPhysicalControls(state, callback) {
try {
const locked = state === Characteristic.LockPhysicalControls.CONTROL_LOCK_ENABLED ? 'on' : 'off',
[ result ] = await this.device.call('set_child_lock', [locked])
if (result !== 'ok')
throw new Error(result)
callback()
} catch (e) {
this.log.error('setLockPhysicalControls', e)
callback(e)
}
}
async getDryingMode(callback) {
try {
const [ mode ] = await this.device.call('get_prop', ['dry']),
state = mode === 'on'
? Characteristic.SwingMode.SWING_ENABLED
: Characteristic.SwingMode.SWING_DISABLED
callback(null, state)
} catch (e) {
this.log.error('getDryingMode', e)
callback(e)
}
}
async setDryingMode(state, callback) {
try {
const mode = state === Characteristic.SwingMode.SWING_ENABLED ? 'on' : 'off',
[ result ] = await this.device.call('set_dry', [mode])
if (result !== 'ok')
throw new Error(result)
callback()
} catch (e) {
this.log.error('setDryingMode', e)
callback(e)
}
}
}