From 94619e40d580fd98c5876637c45fc000f70816e8 Mon Sep 17 00:00:00 2001 From: Alistair Galbraith Date: Mon, 29 Jun 2015 14:40:17 -0700 Subject: [PATCH] Changed approach to getting accessories Avoid hitting rate limiting iterating over bulbs for installations with many hue bulbs on a hub --- platforms/PhilipsHue.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/platforms/PhilipsHue.js b/platforms/PhilipsHue.js index e069bcd..ae4e1e2 100644 --- a/platforms/PhilipsHue.js +++ b/platforms/PhilipsHue.js @@ -102,25 +102,28 @@ var locateBridge = function (callback) { }; 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 and loop through lights - api.lights(function(err, response) { + + // 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; - response.lights.map(function(light) { - var foundAccessories = []; - // Get the state of each individual light and add to platform - api.lightStatus(light.id, function(err, device) { - if (err) throw err; - device.id = light.id; - var accessory = new PhilipsHueAccessory(that.log, device, api); - foundAccessories.push(accessory); - callback(foundAccessories); - }); - }); + + 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); + }); };