-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
224 lines (157 loc) · 5.07 KB
/
Copy pathindex.js
File metadata and controls
224 lines (157 loc) · 5.07 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'use strict';
var SkyRemote = require('sky-remote');
var SkyQCheck = require('sky-q');
var Accessory, Service, Characteristic;
module.exports = function(homebridge) {
Accessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-sky-q", "SkyQ", SkyQAccessory);
};
function SkyQAccessory(log, config, api) {
this.log = log;
this.config = config;
this.name = config.name || 'Sky Q';
this.stateful = false;
if (this.config.cmd === 'power' || !this.config.cmd) {
this.stateful = true;
}
var remoteControl = new SkyRemote(config.ipAddress);
var boxCheck = new SkyQCheck({ip:config.ipAddress})
this.skyQ = remoteControl;
this.box = boxCheck;
}
SkyQAccessory.prototype = {
setPowerState: function(powerOn, callback, context) {
let funcContext = 'fromSetPowerState';
var switchService = this.service;
var log = this.log;
var name = this.name;
var stateful = this.stateful;
var state = powerOn ? 'on' : 'off';
var delayed = this.config.delayed;
var skyQ = this.skyQ;
var box = this.box;
function sendCmd(callBacked) {
log('Sending "' + cmd + '" command to ' + name + '...');
if (cmd.length > 6 && callBacked === false) {
// If it's a long command string that will take a while, immedately callback() to avoid Siri timeout
callBacked = true;
callback();
}
skyQ.press(cmd, function(error) {
if (error) {
log('Failed to send cmd ' + name + '. ' + error);
}
if (stateful === true) {
callback(); // Do this early or Siri moans it takes too long
if (state === 'on' && delayed === true) {
setTimeout(function() {
skyQ.press('sky', function(error) {
log('Sending "sky" command to ' + name + '...');
if (error) {
log('Failed to send cmd ' + name + '. ' + error);
}
});
}, 20000);
}
} else {
if (callBacked === false) {
callback();
}
setTimeout(function() {
switchService.getCharacteristic(Characteristic.On).getValue(null, funcContext);
}, 1000);
}
});
}
if (this.config.cmd) {
var cmd = this.config.cmd.split(',');
} else {
var cmd = 'power';
}
if (stateful === false && this.config.autoOn === true) {
box.getPowerState().then(isOn=>{
if (isOn) {
log(name + " is on :-)")
sendCmd(false);
} else {
log(name + " is in standby :-(")
skyQ.press(['power','backup'], function(error) {
log('Sending "power,backup" command to ' + name + '...');
if (error) {
log('Failed to send cmd ' + name + '. ' + error);
}
});
if (delayed === true) {
log("Delayed 'sky' press enabled");
callback(); // Do this early or Siri moans that it takes too long
setTimeout(function() {
skyQ.press(['sky','backup'], function(error) {
log('Sending "sky,backup" command to ' + name + '...');
if (error) {
log('Failed to send cmd ' + name + '. ' + error);
}
});
setTimeout(function() {
sendCmd(true);
}, 1000); //Give the box a chance to get back to a channel before sending the cmd
}, 20000);
} else {
setTimeout(function() {
sendCmd(false);
}, 3000); //Give the box a chance to start up before we send the cmd
}
}
}).catch(err=>{
log("Unable to determine power state 1")
log("Perhaps looking at this error will help you figure out why" + err)
callback(err || new Error('Error getting state of ' + this.name));
})
} else {
box.getPowerState().then(isOn=>{
if ((isOn && state === 'on') || (!isOn && state === 'off')) {
callback();
setTimeout(function() {
switchService.getCharacteristic(Characteristic.On).getValue(null, funcContext);
}, 1000);
} else {
sendCmd(false);
}
}).catch(err=>{
log("Unable to determine power state 2")
log("Perhaps looking at this error will help you figure out why" + err)
callback(err || new Error('Error getting state of ' + this.name));
})
}
},
identify: function(callback) {
this.log("Identify...");
callback();
},
getState: function(callback) {
if (this.stateful) {
this.box.getPowerState().then(isOn=>{
if (isOn) {
this.log(this.name + " is on :-)")
} else {
this.log(this.name + " is in standby :-(")
}
callback(null, isOn);
}).catch(err=>{
this.log("Unable to determine power state 3")
this.log("Perhaps looking at this error will help you figure out why" + err)
callback(err || new Error('Error getting state of ' + this.name));
})
} else {
callback(null, 0);
}
},
getServices: function() {
var switchService = new Service.Switch(this.name);
var characteristic = switchService.getCharacteristic(Characteristic.On).on('set', this.setPowerState.bind(this));
characteristic.on('get', this.getState.bind(this));
this.service = switchService;
return [switchService];
}
};