Merge pull request #158 from devbobo/master

[LiFX] fix/enhance the LiFX platform
This commit is contained in:
Nick Farina
2015-09-10 07:26:08 -07:00

View File

@@ -1,4 +1,5 @@
var types = require("HAP-NodeJS/accessories/types.js"); var Service = require("HAP-NodeJS").Service;
var Characteristic = require("HAP-NodeJS").Characteristic;
var lifxObj = require('lifx-api'); var lifxObj = require('lifx-api');
var lifx; var lifx;
@@ -23,14 +24,7 @@ LIFxPlatform.prototype = {
var bulbs = JSON.parse(body); var bulbs = JSON.parse(body);
for(var i = 0; i < bulbs.length; i ++) { for(var i = 0; i < bulbs.length; i ++) {
var bulb = bulbs[i]; var accessory = new LIFxBulbAccessory(that.log, bulbs[i]);
var accessory = new LIFxBulbAccessory(
that.log,
bulb.label,
bulb.uuid,
bulb.model,
bulb.id
);
foundAccessories.push(accessory); foundAccessories.push(accessory);
} }
callback(foundAccessories) callback(foundAccessories)
@@ -38,135 +32,119 @@ LIFxPlatform.prototype = {
} }
} }
function LIFxBulbAccessory(log, label, serial, model, deviceId) { function LIFxBulbAccessory(log, bulb) {
// device info // device info
this.name = label; this.name = bulb.label;
this.model = model; this.model = bulb.product_name;
this.deviceId = deviceId; this.deviceId = bulb.id;
this.serial = serial; this.serial = bulb.uuid;
this.capabilities = bulb.capabilities;
this.log = log; this.log = log;
} }
LIFxBulbAccessory.prototype = { LIFxBulbAccessory.prototype = {
getPower: function(callback){ get: function(type, callback){
var that = this; var that = this;
lifx.listLights("all", function(body) { lifx.listLights("id:"+ that.deviceId, function(body) {
var bulbs = JSON.parse(body); var bulb = JSON.parse(body);
for(var i = 0; i < bulbs.length; i ++) { if (bulb.connected != true) {
var bulb = bulbs[i]; callback(new Error("Device not found"), false);
return;
if(bulb.deviceId == that.deviceId) {
return bulb.state;
}
} }
return "off";
});
nest.fetchStatus(function (data) { switch(type) {
var device = data.shared[that.deviceId]; case "power":
that.log("Target temperature for " + this.name + " is: " + device.target_temperature); callback(null, bulb.power == "on" ? 1 : 0);
callback(device.target_temperature); break;
case "brightness":
callback(null, Math.round(bulb.brightness * 100));
break;
case "hue":
callback(null, bulb.color.hue);
break;
case "saturation":
callback(null, Math.round(bulb.color.saturation * 100));
break;
}
}); });
}, },
setPower: function(state){ identify: function(callback) {
var that = this; var that = this;
this.log("Setting power state for heating cooling for " + this.name + " to: " + targetTemperatureType);
lifx.setPower("all", state, 1, function (body) { lifx.breatheEffect("id:"+ that.deviceId, 'green', null, 1, 3, false, true, 0.5, function (body) {
this.log("body"); callback();
});
},
setColor: function(type, state, callback){
var that = this;
var color;
switch(type) {
case "brightness":
color = "brightness:" + (state / 100);
break;
case "hue":
color = "hue:" + state;
break;
case "saturation":
color = "saturation:" + (state / 100);
break;
}
lifx.setColor("id:"+ that.deviceId, color, 0, null, function (body) {
callback();
});
},
setPower: function(state, callback){
var that = this;
lifx.setPower("id:"+ that.deviceId, (state == 1 ? "on" : "off"), 0, function (body) {
callback();
}); });
}, },
getServices: function() { getServices: function() {
var that = this; var that = this;
var chars= [{ var services = []
sType: types.ACCESSORY_INFORMATION_STYPE, var service = new Service.Lightbulb(this.name);
characteristics: [{
cType: types.NAME_CTYPE, service
onUpdate: null, .getCharacteristic(Characteristic.On)
perms: ["pr"], .on('identify', function(callback) {})
format: "string", .on('get', function(callback) { that.get("power", callback);})
initialValue: this.name, .on('set', function(value, callback) {that.setPower(value, callback);});
supportEvents: false,
supportBonjour: false, service
manfDescription: "Name of the accessory", .addCharacteristic(Characteristic.Brightness)
designedMaxLength: 255 .on('get', function(callback) { that.get("brightness", callback);})
},{ .on('set', function(value, callback) { that.setColor("brightness", value, callback);});
cType: types.MANUFACTURER_CTYPE,
onUpdate: null, if (this.capabilities.has_color == true) {
perms: ["pr"], service
format: "string", .addCharacteristic(Characteristic.Hue)
initialValue: "LIFx", .on('get', function(callback) { that.get("hue", callback);})
supportEvents: false, .on('set', function(value, callback) { that.setColor("hue", value, callback);});
supportBonjour: false,
manfDescription: "Manufacturer", service
designedMaxLength: 255 .addCharacteristic(Characteristic.Saturation)
},{ .on('get', function(callback) { that.get("saturation", callback);})
cType: types.MODEL_CTYPE, .on('set', function(value, callback) { that.setColor("saturation", value, callback);});
onUpdate: null, }
perms: ["pr"],
format: "string", services.push(service);
initialValue: this.model,
supportEvents: false, service = new Service.AccessoryInformation();
supportBonjour: false,
manfDescription: "Model", service
designedMaxLength: 255 .setCharacteristic(Characteristic.Manufacturer, "LiFX")
},{ .setCharacteristic(Characteristic.Model, this.model)
cType: types.SERIAL_NUMBER_CTYPE, .setCharacteristic(Characteristic.SerialNumber, this.serial);
onUpdate: null,
perms: ["pr"], services.push(service);
format: "string",
initialValue: this.serial, return services;
supportEvents: false,
supportBonjour: false,
manfDescription: "SN",
designedMaxLength: 255
},{
cType: types.IDENTIFY_CTYPE,
onUpdate: null,
perms: ["pw"],
format: "bool",
initialValue: true,
supportEvents: false,
supportBonjour: false,
manfDescription: "Identify Accessory",
designedMaxLength: 1
}]
}, {
sType: types.LIGHTBULB_STYPE,
characteristics: [{
cType: types.NAME_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: this.name,
supportEvents: false,
supportBonjour: false,
manfDescription: "Name of LIFx bulb",
designedMaxLength: 255
}, {
cType: types.POWER_STATE_CTYPE,
onUpdate: function (value) {
that.setPower(value);
},
onRead: function (callback) {
that.getPower(function (state) {
callback(state);
});
},
perms: ["pw", "pr", "ev"],
format: "int",
initialValue: 0,
supportEvents: false,
supportBonjour: false,
manfDescription: "Power state",
designedMinValue: 0,
designedMaxValue: 1,
designedMinStep: 1
}]
}];
return chars;
} }
} }