This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
117 lines (91 loc) · 3.09 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
var request = require('request');
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-sonoff-basic-espeasy', 'SonoffBasicESPEasy', SonoffBasicESPEasy);
}
function SonoffBasicESPEasy(log, config) {
this.log = log;
this.name = config.name || 'Sonoff Switch';
this.type = config.type || 'switch';
this.ip = config.ip;
this.pulse = config.pulse || false;
this.action = config.action || 'off';
this.duration = config.duration || 60;
if (!this.ip) {
throw new Error('Your must provide IP address of the switch.');
}
switch (this.type) {
case 'outlet':
this.service = new Service.Outlet(this.name);
break;
default:
this.service = new Service.Switch(this.name);
break;
}
this.serviceInfo = new Service.AccessoryInformation();
this.serviceInfo
.setCharacteristic(Characteristic.Manufacturer, 'Sonoff')
.setCharacteristic(Characteristic.Model, 'Basic Switch')
.setCharacteristic(Characteristic.SerialNumber, 'EB1B-ED2E-5EA945508A66');
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
}
SonoffBasicESPEasy.prototype = {
getPowerState: function(callback) {
var log = this.log;
if (this.pulse) {
callback(null, false);
return;
}
request.get({
url: 'http://' + this.ip + '/control?cmd=status,gpio,12',
timeout: 120000
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
var json = JSON.parse(body);
log.debug('State: ' + json.state);
callback(null, (json.state == 1));
return;
}
log.debug('Error getting power state. (%s)', error);
callback();
});
},
setPowerState: function(state, callback) {
var log = this.log;
var command = '/control?cmd=event,' + ((state) ? 'PowerOn' : 'PowerOff');
if (this.pulse && state) {
command = '/control?cmd=Pulse,12,' + ((this.action == 'on') ? 1 : 0) + ',' + (parseInt(this.duration) * 1000);
}
request.get({
url: 'http://' + this.ip + command,
timeout: 120000
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
if (body == 'OK') {
return;
}
log.debug('Response Error: %s', body);
return;
}
log.debug('Error setting device control. (%s)', error);
});
if (this.pulse) {
var that = this;
setTimeout(function() {
that.service.getCharacteristic(Characteristic.On).updateValue(false);
}, 2000);
}
callback();
},
identify: function(callback) {
callback();
},
getServices: function() {
return [this.serviceInfo, this.service];
}
};