forked from KraigM/homebridge-wink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
357 lines (309 loc) · 10.8 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
var wink = require('wink-js');
var inherits = require('util').inherits;
process.env.WINK_NO_CACHE = true;
var Service, Characteristic, Accessory, uuid;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
Accessory = homebridge.hap.Accessory;
uuid = homebridge.hap.uuid;
var copyInherit = function(orig, base){
var acc = orig.prototype;
inherits(orig, base);
orig.prototype.parent = base.prototype;
for (var mn in acc) {
orig.prototype[mn] = acc[mn];
}
};
copyInherit(WinkAccessory, Accessory);
copyInherit(WinkLightAccessory, WinkAccessory);
copyInherit(WinkLockAccessory, WinkAccessory);
homebridge.registerPlatform("homebridge-wink", "Wink", WinkPlatform);
};
var model = {
light_bulbs: require('wink-js/lib/model/light'),
refreshUntil: function(that, maxTimes, predicate, callback, interval, incrementInterval) {
if (!interval) {
interval = 500;
}
if (!incrementInterval) {
incrementInterval = 500;
}
setTimeout(function() {
that.reloadData(function() {
if (predicate == undefined || predicate(that.device) == true) {
if (callback) callback(true);
} else if (maxTimes > 0) {
maxTimes = maxTimes - 1;
interval += incrementInterval;
model.refreshUntil(that, maxTimes, predicate, callback, interval, incrementInterval);
} else {
if (callback) callback(false);
}
});
}, interval);
}
};
function WinkPlatform(log, config){
// auth info
this.client_id = config["client_id"];
this.client_secret = config["client_secret"];
this.username = config["username"];
this.password = config["password"];
this.log = log;
this.deviceLookup = {};
}
WinkPlatform.prototype = {
reloadData: function(callback) {
this.log("Refreshing Wink Data");
var that = this;
wink.user().devices(function(devices) {
if (devices && devices.data && devices.data instanceof Array) {
for (var i=0; i<devices.data.length; i++){
var device = devices.data[i];
var accessory = that.deviceLookup[device.lock_id | device.light_bulb_id | ""];
if (accessory != undefined) {
accessory.device = device;
accessory.loadData();
}
}
}
if (callback) callback();
});
},
accessories: function(callback) {
this.log("Fetching Wink devices.");
var that = this;
var foundAccessories = [];
this.deviceLookup = {};
var refreshLoop = function(){
setInterval(that.reloadData.bind(that), 30000);
};
wink.init({
"client_id": this.client_id,
"client_secret": this.client_secret,
"username": this.username,
"password": this.password
}, function(auth_return) {
if ( auth_return === undefined ) {
that.log("There was a problem authenticating with Wink.");
} else {
// success
wink.user().devices(function(devices) {
for (var i=0; i<devices.data.length; i++){
var device = devices.data[i];
var accessory = null;
if (device.light_bulb_id !== undefined) {
accessory = new WinkLightAccessory(that.log, device);
} else if (device.lock_id !== undefined) {
accessory = new WinkLockAccessory(that.log, device);
}
if (accessory != undefined) {
that.deviceLookup[accessory.deviceId] = accessory;
foundAccessories.push(accessory);
}
}
refreshLoop();
callback(foundAccessories);
});
}
});
}
};
/*
* Base Accessory
*/
function WinkAccessory(log, device, type, typeId) {
// construct base
this.device = device;
this.name = device.name;
this.log = log;
if (typeId == undefined) {
typeId = this.name;
log("WARN: Unable to find id of " + this.name + " so using name instead");
}
this.deviceGroup = type + 's';
this.deviceId = typeId;
var idKey = 'hbdev:wink:' + type + ':' + typeId;
var id = uuid.generate(idKey);
Accessory.call(this, this.name, id);
this.uuid_base = id;
this.control = wink.device_group(this.deviceGroup).device_id(this.deviceId);
// set some basic properties (these values are arbitrary and setting them is optional)
this
.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, this.device.device_manufacturer)
.setCharacteristic(Characteristic.Model, this.device.model_name);
WinkAccessory.prototype.loadData.call(this);
}
WinkAccessory.prototype.getServices = function() {
return this.services;
};
WinkAccessory.prototype.loadData = function() {
};
WinkAccessory.prototype.handleResponse = function(res) {
if (!res) {
return Error("No response from Wink");
} else if (res.errors && res.errors.length > 0) {
return res.errors[0];
} else if (res.data) {
this.device = res.data;
this.loadData();
}
};
WinkAccessory.prototype.reloadData = function(callback){
var that = this;
this.control.get(function(res) {
callback(that.handleResponse(res));
});
};
/*
* Light Accessory
*/
function WinkLightAccessory(log, device) {
// construct base
WinkAccessory.call(this, log, device, 'light_bulb', device.light_bulb_id);
// accessor
var that = this;
that.device = device;
that.deviceControl = model.light_bulbs(device, wink);
this
.addService(Service.Lightbulb)
.getCharacteristic(Characteristic.On)
.on('get', function(callback) {
var powerState = that.device.desired_state.powered;
that.log("power state for " + that.name + " is: " + powerState);
callback(null, powerState != undefined ? powerState : false);
})
.on('set', function(powerOn, callback) {
if (powerOn) {
that.log("Setting power state on the '"+that.name+"' to on");
that.deviceControl.power.on(function(response) {
if (response === undefined) {
that.log("Error setting power state on the '"+that.name+"'");
callback(Error("Error setting power state on the '"+that.name+"'"));
} else {
that.log("Successfully set power state on the '"+that.name+"' to on");
callback(null, powerOn);
}
});
}else{
that.log("Setting power state on the '"+that.name+"' to off");
that.deviceControl.power.off(function(response) {
if (response === undefined) {
that.log("Error setting power state on the '"+that.name+"'");
callback(Error("Error setting power state on the '"+that.name+"'"));
} else {
that.log("Successfully set power state on the '"+that.name+"' to off");
callback(null, powerOn);
}
});
}
});
this
.getService(Service.Lightbulb)
.getCharacteristic(Characteristic.Brightness)
.on('get', function(callback) {
var level = that.device.desired_state.brightness * 100;
that.log("brightness level for " + that.name + " is: " + level);
callback(null, level);
})
.on('set', function(level, callback) {
that.log("Setting brightness on the '"+this.name+"' to " + level);
that.deviceControl.brightness(level, function(response) {
if (response === undefined) {
that.log("Error setting brightness on the '"+that.name+"'");
callback(Error("Error setting brightness on the '"+that.name+"'"));
} else {
that.log("Successfully set brightness on the '"+that.name+"' to " + level);
callback(null, level);
}
});
});
WinkLightAccessory.prototype.loadData.call(this);
}
WinkLightAccessory.prototype.loadData = function() {
this.parent.loadData.call(this);
this.getService(Service.Lightbulb)
.getCharacteristic(Characteristic.On)
.getValue();
this.getService(Service.Lightbulb)
.getCharacteristic(Characteristic.Brightness)
.getValue();
};
/*
* Lock Accessory
*/
function WinkLockAccessory(log, device) {
// construct base
WinkAccessory.call(this, log, device, 'lock', device.lock_id);
// accessor
var that = this;
this
.addService(Service.LockMechanism)
.getCharacteristic(Characteristic.LockTargetState)
.on('get', function(callback) {
callback(null, that.isLockTarget());
})
.on('set', function(value, callback) {
var locked = that.fromLockState(value);
if (locked == undefined) {
callback(Error("Unsupported"));
return;
}
that.log("Changing target lock state of " + that.name + " to " + (locked ? "locked" : "unlocked"));
var update = function(retry) {
that.control.update({ "desired_state": { "locked": locked } }, function(res) {
var err = that.handleResponse(res);
if (!err) {
model.refreshUntil(that, 5,
function() { return that.isLocked() == that.isLockTarget(); },
function(completed) {
if (completed) {
that.log("Successfully changed lock status to " + (that.isLocked() ? "locked" : "unlocked"));
} else if (retry) {
that.log("Unable to determine if update was successful. Retrying update.");
retry();
} else {
that.log("Unable to determine if update was successful.");
}
});
}
if (callback)
{
callback(err);
callback = null;
}
});
};
update(update);
});
WinkLockAccessory.prototype.loadData.call(this);
}
WinkLockAccessory.prototype.loadData = function() {
this.parent.loadData.call(this);
this.getService(Service.LockMechanism)
.setCharacteristic(Characteristic.LockCurrentState, this.isLocked());
this.getService(Service.LockMechanism)
.getCharacteristic(Characteristic.LockTargetState)
.getValue();
};
WinkLockAccessory.prototype.toLockState= function(isLocked) {
return isLocked ? Characteristic.LockCurrentState.SECURED : Characteristic.LockCurrentState.UNSECURED;
};
WinkLockAccessory.prototype.fromLockState= function(lockState) {
switch (lockState) {
case Characteristic.LockCurrentState.SECURED:
return true;
case Characteristic.LockCurrentState.UNSECURED:
return false;
default:
return undefined;
}
};
WinkLockAccessory.prototype.isLockTarget= function() {
return this.toLockState(this.device.desired_state.locked);
};
WinkLockAccessory.prototype.isLocked= function() {
return this.toLockState(this.device.last_reading.locked);
};