-
Notifications
You must be signed in to change notification settings - Fork 0
/
mi-home.js
80 lines (67 loc) · 2.45 KB
/
mi-home.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
const miio = require('miio');
const config = require('./config.json')
class MiHome {
constructor() {
this.devices = {};
}
load() {
console.info('Looking for gateway..')
const devices = miio.devices({
cacheTime: 300
});
devices.on('available', async deviceDiscovered => {
if (deviceDiscovered.id == config.gateway.id) {
console.info('Gateway found!')
let device = await miio.device({ address: deviceDiscovered.address, token: config.gateway.token });
console.debug('Connected to: ', device.miioModel)
let children = device.children();
for (let child of children) {
if (child.model == 'lumi.gateway.v3.light') {
this.devices.light = child;
continue;
}
console.debug('Child found: ' + child.miioModel);
switch (child.miioModel) {
case 'lumi.plug':
this.devices.plug = child;
break;
case 'lumi.magnet':
this.devices.door = child;
break;
case 'lumi.switch':
this.devices.switch = child;
break;
case 'lumi.sensor_ht':
this.devices.sensor = child;
break;
case 'lumi.motion':
this.devices.motion = child;
break;
default:
console.debug('Connected child not recognized:', child.metadata.types);
}
}
}
});
devices.on('unavailable', device => {
console.error('device disconnected:', device)
});
}
async getTemperature() {
if (this.devices.sensor) {
let response = await this.devices.sensor.value('temperature')
return response.value;
}else{
return null;
}
}
async getHumidity() {
if (this.devices.sensor) {
let response = await this.devices.sensor.value('humidity')
return response.value;
}else{
return null;
}
}
}
module.exports = new MiHome();