Merge pull request #135 from dotsam/milight

[MiLight] Update to address correct bulbs and implement all features
This commit is contained in:
Nick Farina
2015-08-31 16:27:05 -07:00

View File

@@ -29,12 +29,11 @@ var light = new Milight({
MiLight.prototype = { MiLight.prototype = {
setPowerState: function(powerOn, callback) { setPowerState: function(powerOn, callback) {
if (powerOn) { if (powerOn) {
light.sendCommands(commands[this.type].on(this.zone)); light.sendCommands(commands[this.type].on(this.zone));
this.log("Setting power state to on"); this.log("Setting power state to on");
} } else {
else {
light.sendCommands(commands[this.type].off(this.zone)); light.sendCommands(commands[this.type].off(this.zone));
this.log("Setting power state to off"); this.log("Setting power state to off");
} }
@@ -42,18 +41,35 @@ MiLight.prototype = {
}, },
setBrightness: function(level, callback) { setBrightness: function(level, callback) {
this.log("Setting brightness to %s", level); if (level <= 2 && (this.type == "rgbw" || this.type == "white")) {
// If setting brightness to 2 or lower, instead set night mode for lamps that support it
this.log("Setting night mode", level);
// If this is an rgbw lamp, set the absolute brightness specified light.sendCommands(commands[this.type].off(this.zone));
if (this.type == "rgbw") { // Not sure if this timing is going to work or not? It's supposed to be 100ms after the off command
light.sendCommands(commands.rgbw.brightness(level)); light.sendCommands(commands[this.type].nightMode(this.zone));
} else { } else {
// If this is an rgb or a white lamp, they only support brightness up and down. this.log("Setting brightness to %s", level);
// Set brightness up when value is >50 and down otherwise. Not sure how well this works real-world.
if (level >= 50) { // Send on command to ensure we're addressing the right bulb
light.sendCommands(commands[this.type].brightUp()); light.sendCommands(commands[this.type].on(this.zone));
// If this is an rgbw lamp, set the absolute brightness specified
if (this.type == "rgbw") {
light.sendCommands(commands.rgbw.brightness(level));
} else { } else {
light.sendCommands(commands[this.type].brightDown()); // If this is an rgb or a white lamp, they only support brightness up and down.
// Set brightness up when value is >50 and down otherwise. Not sure how well this works real-world.
if (level >= 50) {
if (this.type == "white" && level == 100) {
// But the white lamps do have a "maximum brightness" command
light.sendCommands(commands.white.maxBright(this.zone));
} else {
light.sendCommands(commands[this.type].brightUp());
}
} else {
light.sendCommands(commands[this.type].brightDown());
}
} }
} }
callback(); callback();
@@ -62,6 +78,9 @@ MiLight.prototype = {
setHue: function(value, callback) { setHue: function(value, callback) {
this.log("Setting hue to %s", value); this.log("Setting hue to %s", value);
// Send on command to ensure we're addressing the right bulb
light.sendCommands(commands[this.type].on(this.zone));
if (this.type == "rgbw") { if (this.type == "rgbw") {
if (value == 0) { if (value == 0) {
light.sendCommands(commands.rgbw.whiteMode(this.zone)); light.sendCommands(commands.rgbw.whiteMode(this.zone));
@@ -69,7 +88,7 @@ MiLight.prototype = {
light.sendCommands(commands.rgbw.hue(commands.rgbw.hsvToMilightColor(Array(value, 0, 0)))); light.sendCommands(commands.rgbw.hue(commands.rgbw.hsvToMilightColor(Array(value, 0, 0))));
} }
} else if (this.type == "rgb") { } else if (this.type == "rgb") {
light.sendCommands(commands.rgb.hue(commands.rgbw.hsvToMilightColor(Array(value, 0, 0)))); light.sendCommands(commands.rgb.hue(commands.rgbw.hsvToMilightColor(Array(value, 0, 0))));
} else if (this.type == "white") { } else if (this.type == "white") {
// Again, white lamps don't support setting an absolue colour temp, so trying to do warmer/cooler step at a time based on colour // Again, white lamps don't support setting an absolue colour temp, so trying to do warmer/cooler step at a time based on colour
if (value >= 180) { if (value >= 180) {
@@ -80,26 +99,26 @@ MiLight.prototype = {
} }
}, },
identify: function(callback) { identify: function(callback) {
this.log("Identify requested!"); this.log("Identify requested!");
callback(); // success callback(); // success
}, },
getServices: function() { getServices: function() {
var informationService = new Service.AccessoryInformation(); var informationService = new Service.AccessoryInformation();
informationService informationService
.setCharacteristic(Characteristic.Manufacturer, "MiLight") .setCharacteristic(Characteristic.Manufacturer, "MiLight")
.setCharacteristic(Characteristic.Model, this.type) .setCharacteristic(Characteristic.Model, this.type)
.setCharacteristic(Characteristic.SerialNumber, "MILIGHT12345"); .setCharacteristic(Characteristic.SerialNumber, "MILIGHT12345");
var lightbulbService = new Service.Lightbulb(); var lightbulbService = new Service.Lightbulb();
lightbulbService lightbulbService
.getCharacteristic(Characteristic.On) .getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this)); .on('set', this.setPowerState.bind(this));
lightbulbService lightbulbService
.addCharacteristic(new Characteristic.Brightness()) .addCharacteristic(new Characteristic.Brightness())
.on('set', this.setBrightness.bind(this)); .on('set', this.setBrightness.bind(this));
@@ -107,7 +126,7 @@ MiLight.prototype = {
lightbulbService lightbulbService
.addCharacteristic(new Characteristic.Hue()) .addCharacteristic(new Characteristic.Hue())
.on('set', this.setHue.bind(this)); .on('set', this.setHue.bind(this));
return [informationService, lightbulbService]; return [informationService, lightbulbService];
} }
}; };