Merge pull request #337 from KraigM/hotfix/PhilipsHue

Fixed issue on Philips Hue where initial state was not being loaded
This commit is contained in:
Nick Farina
2015-10-27 21:56:32 -07:00

View File

@@ -187,6 +187,20 @@ PhilipsHueAccessory.prototype = {
value = Math.round(value); value = Math.round(value);
return 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 // Create and set a light state
executeChange: function(characteristic, value, callback) { executeChange: function(characteristic, value, callback) {
var state = lightState.create(); var state = lightState.create();
@@ -254,24 +268,14 @@ PhilipsHueAccessory.prototype = {
} }
else { else {
switch(characteristic.toLowerCase()) { var newValue = this.extractValue(characteristic, status);
case 'power': if (newValue != undefined) {
callback(null, status.state.on ? 1 : 0); callback(null, newValue);
break; } else {
case 'hue': // this.log("Device " + that.device.name + " does not support reading characteristic " + characteristic);
callback(null, this.hueToArcDegrees(status.state.hue)); // callback(Error("Device " + that.device.name + " does not support reading characteristic " + characteristic) );
break;
case 'brightness':
callback(null, this.bitsToPercentage(status.state.bri));
break;
case 'saturation':
callback(null, this.bitsToPercentage(status.state.sat));
break;
//default:
// 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; callback = null;
//this.log("Get " + that.device.name + ", characteristic: " + characteristic + ", value: " + value + "."); //this.log("Get " + that.device.name + ", characteristic: " + characteristic + ", value: " + value + ".");
@@ -295,24 +299,28 @@ PhilipsHueAccessory.prototype = {
lightbulbService lightbulbService
.getCharacteristic(Characteristic.On) .getCharacteristic(Characteristic.On)
.on('get', function(callback) { that.getState("power", callback);}) .on('get', function(callback) { that.getState("power", callback);})
.on('set', function(value, callback) { that.executeChange("power", value, callback);}); .on('set', function(value, callback) { that.executeChange("power", value, callback);})
.value = this.extractValue("power", this.device);
lightbulbService lightbulbService
.addCharacteristic(Characteristic.Brightness) .addCharacteristic(Characteristic.Brightness)
.on('get', function(callback) { that.getState("brightness", callback);}) .on('get', function(callback) { that.getState("brightness", callback);})
.on('set', function(value, callback) { that.executeChange("brightness", value, callback);}); .on('set', function(value, callback) { that.executeChange("brightness", value, callback);})
.value = this.extractValue("brightness", this.device);
// Handle the Hue/Hue Lux divergence // Handle the Hue/Hue Lux divergence
if (this.device.state.hasOwnProperty('hue') && this.device.state.hasOwnProperty('sat')) { if (this.device.state.hasOwnProperty('hue') && this.device.state.hasOwnProperty('sat')) {
lightbulbService lightbulbService
.addCharacteristic(Characteristic.Hue) .addCharacteristic(Characteristic.Hue)
.on('get', function(callback) { that.getState("hue", callback);}) .on('get', function(callback) { that.getState("hue", callback);})
.on('set', function(value, callback) { that.executeChange("hue", value, callback);}); .on('set', function(value, callback) { that.executeChange("hue", value, callback);})
.value = this.extractValue("hue", this.device);
lightbulbService lightbulbService
.addCharacteristic(Characteristic.Saturation) .addCharacteristic(Characteristic.Saturation)
.on('get', function(callback) { that.getState("saturation", callback);}) .on('get', function(callback) { that.getState("saturation", callback);})
.on('set', function(value, callback) { that.executeChange("saturation", value, callback);}); .on('set', function(value, callback) { that.executeChange("saturation", value, callback);})
.value = this.extractValue("saturation", this.device);
} }
var informationService = new Service.AccessoryInformation(); var informationService = new Service.AccessoryInformation();