-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·101 lines (86 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
92
93
94
95
96
97
98
99
100
101
request = require('request')
var Service, Characteristic;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-sensibo-switch", "SimpleSensiboSwitch", SimpleSensiboSwitch);
}
function SimpleSensiboSwitch(log, config) {
this.log = log;
// url info
this.name = config["name"];
this.api_key = config["apiKey"];
this.device = config["device"];
}
var _SERVER = 'https://home.sensibo.com/api/v2'
SimpleSensiboSwitch.prototype = {
getPowerState: function (callback) {
// Get Sensibo power info
var options = {
url: _SERVER + "/pods/" + this.device + "/acStates",
qs: {
'limit': 1,
'fields': 'status,reason,acState',
'apiKey': this.api_key
}
};
request.get(options, function(error, response, body) {
if (!error) {
if (response && response.statusCode == 200) {
console.log("Got power state ");
callback(null, JSON.parse(body).result[0].acState.on);
}
}
} );
},
setPowerState: function(powerOn, callback) {
// Turn Sensibo on/off, then callback
device = this.device
api_key = this.api_key
var options = {
url: _SERVER + "/pods/" + device + "/acStates",
qs: {
'limit': 1,
'fields': 'status,reason,acState',
'apiKey': api_key
}
};
request.get(options, function(error, response, body) {
if (!error && response && response.statusCode == 200) {
var options_put = {
url: _SERVER + "/pods/" + device + "/acStates/on",
qs: { "apiKey": api_key },
body: {'currentAcState': JSON.parse(body).result[0].acState, 'newValue': !!powerOn},
json: true
}
request.patch(options_put, function(error, reponse, body) {
if (!error) {
if (response && response.statusCode == 200) {
console.log("setPowerState: Power state sent, result " + reponse.statusCode);
callback(null)
} else {
callback(error);
}
}
} );
}
} );
},
identify: function (callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function () {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Sensibo")
.setCharacteristic(Characteristic.Model, "Sensibo")
.setCharacteristic(Characteristic.SerialNumber, "311");
switchService = new Service.Switch(this.name);
switchService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
return [switchService];
}
};