diff --git a/lib/HueBridge.js b/lib/HueBridge.js index c11fb9d3..fc728917 100644 --- a/lib/HueBridge.js +++ b/lib/HueBridge.js @@ -278,8 +278,13 @@ HueBridge.prototype.createGroup0 = function() { HueBridge.prototype.heartbeat = function(beat) { if (beat % this.state.heartrate === 0) { - this.heartbeatResources() + this.heartbeatConfig() + .then(this.heartbeatSensors.bind(this)) + .then(this.heartbeatLights.bind(this)) + .then(this.heartbeatGroups.bind(this)) .then(this.heartbeatGroup0.bind(this)) + .then(this.heartbeatSchedules.bind(this)) + .then(this.heartbeatRules.bind(this)) .catch(function (err) { if (err.message) { this.log.error(err.message); @@ -288,50 +293,52 @@ HueBridge.prototype.heartbeat = function(beat) { } }; -HueBridge.prototype.heartbeatResources = function() { - return this.request('get', '/', null).then(function(obj) { - this.state.lastupdated = obj.config.UTC === 'none' ? - 'n/a' : String(new Date(obj.config.UTC)).substring(0, 25); +HueBridge.prototype.heartbeatConfig = function() { + return this.request('get', '/config').then(function(config) { + this.state.lastupdated = config.UTC === 'none' ? + 'n/a' : String(new Date(config.UTC)).substring(0, 25); this.service .updateCharacteristic(Characteristic.LastUpdated, this.state.lastupdated); - if (this.platform.config.sensors) { - for (const id in obj.sensors) { - const a = this.sensors[id]; - if (a) { - a.heartbeat(obj.sensors[id]); - } - } - } - if (this.platform.config.lights) { - for (const id in obj.lights) { - const a = this.lights[id]; - if (a) { - a.heartbeat(obj.lights[id]); - } - } - } - if (this.platform.config.groups) { - for (const id in obj.groups) { - const a = this.groups[id]; - if (a) { - a.heartbeat(obj.groups[id]); - } + }.bind(this)); +}; + +HueBridge.prototype.heartbeatSensors = function() { + if (!this.platform.config.sensors) { + return Promise.resolve(); + } + return this.request('get', '/sensors', null).then(function(sensors) { + for (const id in sensors) { + const a = this.sensors[id]; + if (a) { + a.heartbeat(sensors[id]); } } - if (this.platform.config.schedules) { - for (const id in obj.schedules) { - const a = this.schedules[id]; - if (a) { - a.heartbeat(obj.schedules[id]); - } + }.bind(this)); +}; + +HueBridge.prototype.heartbeatLights = function() { + if (!this.platform.config.lights) { + return Promise.resolve(); + } + return this.request('get', '/lights', null).then(function(lights) { + for (const id in lights) { + const a = this.lights[id]; + if (a) { + a.heartbeat(lights[id]); } } - if (this.platform.config.rules) { - for (const id in obj.rules) { - const a = this.rules[id]; - if (a) { - a.heartbeat(obj.rules[id]); - } + }.bind(this)); +}; + +HueBridge.prototype.heartbeatGroups = function() { + if (!this.platform.config.groups) { + return Promise.resolve(); + } + return this.request('get', '/groups', null).then(function(groups) { + for (const id in groups) { + const a = this.groups[id]; + if (a) { + a.heartbeat(groups[id]); } } }.bind(this)); @@ -349,6 +356,34 @@ HueBridge.prototype.heartbeatGroup0 = function() { }.bind(this)); }; +HueBridge.prototype.heartbeatSchedules = function() { + if (!this.platform.config.schedules) { + return Promise.resolve(); + } + return this.request('get', '/schedules', null).then(function(schedules) { + for (const id in schedules) { + const a = this.schedules[id]; + if (a) { + a.heartbeat(schedules[id]); + } + } + }.bind(this)); +}; + +HueBridge.prototype.heartbeatRules = function() { + if (!this.platform.config.rules) { + return Promise.resolve(); + } + return this.request('get', '/rules', null).then(function(rules) { + for (const id in rules) { + const a = this.rules[id]; + if (a) { + a.heartbeat(rules[id]); + } + } + }.bind(this)); +}; + // ===== Homekit Events ======================================================== HueBridge.prototype.setHeartrate = function(rate, callback) {