forked from codetheweb/homebridge-tuya-outlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (44 loc) · 1.47 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
const tuya = require('tuyapi');
const debug = require('debug')('homebridge-tuya');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-tuya-outlet", "TuyaOutlet", TuyaOutlet);
}
function TuyaOutlet(log, config) {
this.log = log;
this.name = config.name;
if (config.ip != undefined) {
this.tuya = new tuya({type: 'outlet', ip: config.ip, id: config.devId, key: config.localKey});
}
else {
this.tuya = new tuya({type: 'outlet', id: config.devId, key: config.localKey});
this.tuya.resolveIds();
}
this._service = new Service.Outlet(this.name);
this._service.getCharacteristic(Characteristic.On).on('set', this._setOn.bind(this));
this._service.getCharacteristic(Characteristic.On).on('get', this._get.bind(this));
}
TuyaOutlet.prototype._setOn = function(on, callback) {
debug("Setting device to " + on);
this.tuya.set({set: on}).then(() => {
return callback(null, true);
}).catch(error => {
return callback(error, null);
});
}
TuyaOutlet.prototype._get = function(callback) {
debug("Getting device status...");
this.tuya.get().then(status => {
return callback(null, status);
}).catch(error => {
callback(error, null);
});
}
TuyaOutlet.prototype.getServices = function() {
return [this._service];
}
TuyaOutlet.prototype.identify = function (callback) {
debug(_this.config.name + " was identified.");
callback();
};