forked from homebridge/homebridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhilipsHue.js
338 lines (295 loc) · 10.6 KB
/
PhilipsHue.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
// Philips Hue Platform Shim for HomeBridge
//
// Remember to add platform to config.json. Example:
// "platforms": [
// {
// "platform": "PhilipsHue",
// "name": "Philips Hue",
// "ip_address": "127.0.0.1",
// "username": "252deadbeef0bf3f34c7ecb810e832f"
// }
// ],
//
// If you do not know the IP address of your Hue Bridge, simply leave it blank and your Bridge
// will be discovered automatically.
//
// If you do not have a "username" for your Hue API already, simply leave the field blank and
// you will be prompted to press the link button on your Hue Bridge before running HomeBridge.
// A username will be created for you and printed out, then the server will exit so you may
// enter it in your config.json.
//
//
// When you attempt to add a device, it will ask for a "PIN code".
// The default code for all HomeBridge accessories is 031-45-154.
//
/* jslint node: true */
/* globals require: false */
/* globals config: false */
"use strict";
var hue = require("node-hue-api"),
HueApi = hue.HueApi,
lightState = hue.lightState;
var Service = require("hap-nodejs").Service;
var Characteristic = require("hap-nodejs").Characteristic;
function PhilipsHuePlatform(log, config) {
this.log = log;
this.ip_address = config["ip_address"];
this.username = config["username"];
}
function PhilipsHueAccessory(log, device, api) {
this.id = device.id;
this.name = device.name;
this.model = device.modelid;
this.device = device;
this.api = api;
this.log = log;
}
// Get the ip address of the first available bridge with meethue.com or a network scan.
var locateBridge = function (callback) {
var that = this;
// Report the results of the scan to the user
var getIp = function (err, bridges) {
if (!bridges || bridges.length === 0) {
that.log("No Philips Hue bridges found.");
callback(err || new Error("No bridges found"));
return;
}
if (bridges.length > 1) {
that.log("Warning: Multiple Philips Hue bridges detected. The first bridge will be used automatically. To use a different bridge, set the `ip_address` manually in the configuration.");
}
that.log(
"Philips Hue bridges found:\n" +
(bridges.map(function (bridge) {
// Bridge name is only returned from meethue.com so use id instead if it isn't there
return "\t" + bridge.ipaddress + ' - ' + (bridge.name || bridge.id);
})).join("\n")
);
callback(null, bridges[0].ipaddress);
};
// Try to discover the bridge ip using meethue.com
that.log("Attempting to discover Philips Hue bridge with meethue.com...");
hue.nupnpSearch(function (locateError, bridges) {
if (locateError) {
that.log("Philips Hue bridge discovery with meethue.com failed. Register your bridge with the meethue.com for more reliable discovery.");
that.log("Attempting to discover Philips Hue bridge with network scan...");
// Timeout after one minute
hue.upnpSearch(60000)
.then(function (bridges) {
that.log("Scan complete");
getIp(null, bridges);
})
.fail(function (scanError) {
that.log("Philips Hue bridge discovery with network scan failed. Check your network connection or set ip_address manually in configuration.");
getIp(new Error("Scan failed: " + scanError.message));
}).done();
} else {
getIp(null, bridges);
}
});
};
PhilipsHuePlatform.prototype = {
accessories: function(callback) {
this.log("Fetching Philips Hue lights...");
var that = this;
var getLights = function () {
var api = new HueApi(that.ip_address, that.username);
// Connect to the API
// Get a dump of all lights, so as not to hit rate limiting for installations with larger amounts of bulbs
api.fullState(function(err, response) {
if (err) throw err;
var foundAccessories = [];
for (var deviceId in response.lights) {
var device = response.lights[deviceId];
device.id = deviceId;
var accessory = new PhilipsHueAccessory(that.log, device, api);
foundAccessories.push(accessory);
}
callback(foundAccessories);
});
};
// Create a new user if needed
function checkUsername() {
if (!that.username) {
var api = new HueApi(that.ip_address);
api.createUser(that.ip_address, null, null, function(err, user) {
// try and help explain this particular error
if (err && err.message == "link button not pressed")
throw "Please press the link button on your Philips Hue bridge, then start the HomeBridge server within 30 seconds.";
if (err) throw err;
throw "Created a new username " + JSON.stringify(user) + " for your Philips Hue. Please add it to your config.json then start the HomeBridge server again: ";
});
}
else {
getLights();
}
}
// Discover the bridge if needed
if (!this.ip_address) {
locateBridge.call(this, function (err, ip_address) {
if (err) throw err;
// TODO: Find a way to persist this
that.ip_address = ip_address;
that.log("Save the Philips Hue bridge ip address "+ ip_address +" to your config to skip discovery.");
checkUsername();
});
} else {
checkUsername();
}
}
};
PhilipsHueAccessory.prototype = {
// Convert 0-65535 to 0-360
hueToArcDegrees: function(value) {
value = value/65535;
value = value*360;
value = Math.round(value);
return value;
},
// Convert 0-360 to 0-65535
arcDegreesToHue: function(value) {
value = value/360;
value = value*65535;
value = Math.round(value);
return value;
},
// Convert 0-255 to 0-100
bitsToPercentage: function(value) {
value = value/255;
value = value*100;
value = Math.round(value);
return value;
},
extractValue: function(characteristic, status) {
switch(characteristic.toLowerCase()) {
case 'power':
return status.state.on ? 1 : 0;
case 'hue':
return this.hueToArcDegrees(status.state.hue);
case 'brightness':
return this.bitsToPercentage(status.state.bri);
case 'saturation':
return this.bitsToPercentage(status.state.sat);
default:
return null;
}
},
// Create and set a light state
executeChange: function(characteristic, value, callback) {
var state = lightState.create();
switch(characteristic.toLowerCase()) {
case 'identify':
state.alert('select');
break;
case 'power':
if (value) {
state.on();
}
else {
state.off();
}
break;
case 'hue':
state.hue(this.arcDegreesToHue(value));
break;
case 'brightness':
state.brightness(value);
break;
case 'saturation':
state.saturation(value);
break;
}
this.api.setLightState(this.id, state, function(err, lights) {
if (callback == null) {
return;
}
if (!err) {
if (callback) callback(); // Success
callback = null;
this.log("Set " + this.device.name + ", characteristic: " + characteristic + ", value: " + value + ".");
}
else {
if (err.code == "ECONNRESET") {
setTimeout(function() {
this.executeChange(characteristic, value, callback);
}, 300);
} else {
this.log(err);
callback(new Error(err));
}
}
}.bind(this));
},
// Read light state
// TODO: implement clever polling/update and caching
// maybe a better NodeJS hue API exists for this
getState: function(characteristic, callback) {
this.api.lightStatus(this.id, function(err, status) {
if (callback == null) {
return;
}
if (err) {
if (err.code == "ECONNRESET") {
setTimeout(function() {
this.getState(characteristic, callback);
}.bind(this), 300);
} else {
this.log(err);
callback(new Error(err));
}
}
else {
var newValue = this.extractValue(characteristic, status);
if (newValue != undefined) {
callback(null, newValue);
} else {
// this.log("Device " + that.device.name + " does not support reading characteristic " + characteristic);
// callback(Error("Device " + that.device.name + " does not support reading characteristic " + characteristic) );
}
callback = null;
//this.log("Get " + that.device.name + ", characteristic: " + characteristic + ", value: " + value + ".");
}
}.bind(this));
},
// Respond to identify request
identify: function(callback) {
this.executeChange("identify", true, callback);
},
// Get Services
getServices: function() {
var that = this;
// Use HomeKit types defined in HAP node JS
var lightbulbService = new Service.Lightbulb(this.name);
// Basic light controls, common to Hue and Hue lux
lightbulbService
.getCharacteristic(Characteristic.On)
.on('get', function(callback) { that.getState("power", callback);})
.on('set', function(value, callback) { that.executeChange("power", value, callback);})
.value = this.extractValue("power", this.device);
lightbulbService
.addCharacteristic(Characteristic.Brightness)
.on('get', function(callback) { that.getState("brightness", callback);})
.on('set', function(value, callback) { that.executeChange("brightness", value, callback);})
.value = this.extractValue("brightness", this.device);
// Handle the Hue/Hue Lux divergence
if (this.device.state.hasOwnProperty('hue') && this.device.state.hasOwnProperty('sat')) {
lightbulbService
.addCharacteristic(Characteristic.Hue)
.on('get', function(callback) { that.getState("hue", callback);})
.on('set', function(value, callback) { that.executeChange("hue", value, callback);})
.value = this.extractValue("hue", this.device);
lightbulbService
.addCharacteristic(Characteristic.Saturation)
.on('get', function(callback) { that.getState("saturation", callback);})
.on('set', function(value, callback) { that.executeChange("saturation", value, callback);})
.value = this.extractValue("saturation", this.device);
}
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Philips")
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.device.uniqueid)
.addCharacteristic(Characteristic.FirmwareRevision, this.device.swversion);
return [informationService, lightbulbService];
}
};
module.exports.platform = PhilipsHuePlatform;