-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
44 lines (35 loc) · 1.32 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
var request = require('request');
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-smartthings-routine', 'SmartThingsRoutine', SmartThingsRoutineAccessory);
}
function SmartThingsRoutineAccessory(log, config) {
var accessory = this;
this.log = log;
this.name = config['name'];
this.appId = config['appId']
this.accessToken = config['accessToken'];
this.service = new Service.Switch(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on('set', function(state, callback) {
var url = "https://graph.api.smartthings.com/api/smartapps/installations/" + accessory.appId + "/action/execute?access_token=" + accessory.accessToken;
accessory.log('Activating `' + accessory.name + '`: ' + url);
request.get({
url: url,
}, function(err, response) {
if (!err && response.statusCode == 200) {
accessory.log("Triggered successfully")
callback(null);
} else {
accessory.log("Error '" + err + "': " + response.body);
callback(err);
}
});
});
}
SmartThingsRoutineAccessory.prototype.getServices = function() {
return [this.service];
};