forked from mtflud/homebridge-dchs220-siren
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
91 lines (77 loc) · 3.39 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
'use strict'
let Service, Characteristic;
let WaterDetector = require('./lib/WaterDetector');
module.exports = (homebridge) => {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-DCH-S160', 'DCH-S160', WaterSensorAccessory)
}
class WaterSensorAccessory {
constructor(log, config) {
this.log = log
this.config = config || {};
// :: Config parameters
this.name = this.config.name || 'D-Link Water Detector';
this.pin = this.config.pin || 123456;
this.ipAddress = this.config.ipAddress || '127.0.0.1';
this.detectorClient = new Siren(this.ipAddress, this.pin);
this.updateInterval = (this.config.updateInterval && !isNaN(this.config.updateInterval) && this.config.updateInterval >= 100) ? this.config.updateInterval : false;
//this.service = new Service.Switch(this.config.name);
this.service = new this.Service(this.Service.LeakSensor);
}
getServices() {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, 'd-link')
.setCharacteristic(Characteristic.Model, 'dch-S160')
.setCharacteristic(Characteristic.SerialNumber, 'dlink-dch-S160')
// :: Fault status (Home does not seem to support it, but Eve app does)
this.service.addOptionalCharacteristic(Characteristic.StatusFault);
// :: Status polling
if (this.updateInterval) {
setInterval(() => {
this.getStatus().then(status => {
this.service.getCharacteristic(this.Characteristic.LeakDetected).setValue(status);
}).catch(err => {
console.log(":: Error from status polling: " + err);
});
}, this.updateInterval);
}
return [informationService, this.service]
}
/*
* Check to see if detector senses water:
*/
getStatus() {
return new Promise((resolve, reject) => {
// :: Log In to the Siren
this.detectorClient.login().then(status => {
if (status !== 'success') {
this.log(":: An error occurred while logging in to the siren, please check the credentials in config.");
return reject("Error while logging in to the siren.");
}
// :: Retrieve playing status
this.detectorClient.getWaterStatus().then(status => {
return resolve(status);
}).catch(err => {
this.log(":: An error occurred while retrieving siren status: " + err);
return reject(err);
});
}).catch(err => {
this.log(":: An error occurred while logging in to the siren: " + err);
return reject(err);
});
});
}
getOnCharacteristicHandler(callback) {
this.getStatus().then(status => {
this.service.getCharacteristic(Characteristic.StatusFault)
.updateValue(false);
return callback(null, status);
}).catch(err => {
console.log(":: Error from status polling: " + err);
this.service.getCharacteristic(Characteristic.StatusFault)
.updateValue(true);
return callback(false);
});
}
}