-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
53 lines (43 loc) · 1.5 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
var Service;
var Characteristic;
var applescript = require('applescript');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-applescript', 'Applescript', ApplescriptAccessory);
}
function ApplescriptAccessory(log, config) {
this.log = log;
this.service = 'Switch';
this.name = config['name'];
this.onCommand = config['on'];
this.offCommand = config['off'];
}
ApplescriptAccessory.prototype.setState = function(powerOn, callback) {
var accessory = this;
var state = powerOn ? 'on' : 'off';
var prop = state + 'Command';
var command = accessory[prop].replace(/''/g, '"');
applescript.execString(command, done);
function done(err, rtn) {
if (err) {
accessory.log('Error: ' + err);
callback(err || new Error('Error setting ' + accessory.name + ' to ' + state));
} else {
accessory.log('Set ' + accessory.name + ' to ' + state);
callback(null);
}
}
}
ApplescriptAccessory.prototype.getServices = function() {
var informationService = new Service.AccessoryInformation();
var switchService = new Service.Switch(this.name);
informationService
.setCharacteristic(Characteristic.Manufacturer, 'Applescript Manufacturer')
.setCharacteristic(Characteristic.Model, 'Applescript Model')
.setCharacteristic(Characteristic.SerialNumber, 'Applescript Serial Number');
switchService
.getCharacteristic(Characteristic.On)
.on('set', this.setState.bind(this));
return [switchService];
}