mirror of
https://github.com/mtan93/homebridge.git
synced 2026-03-08 05:31:55 +00:00
Switched to use the new HAP APIs, extracted base class WinkAccessory, and changed update system to properly notify changes on a timed basis. Functionality wise, left the LightBulb implementation the same.
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
"debug": "^2.2.0",
|
||||
"eibd": "^0.3.1",
|
||||
"elkington": "kevinohara80/elkington",
|
||||
"hap-nodejs": "^0.0.2",
|
||||
"hap-nodejs": "^0.0.3",
|
||||
"harmonyhubjs-client": "^1.1.6",
|
||||
"harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git",
|
||||
"isy-js": "",
|
||||
|
||||
@@ -1,28 +1,56 @@
|
||||
var types = require("hap-nodejs/accessories/types.js");
|
||||
var wink = require('wink-js');
|
||||
var Service = require("hap-nodejs").Service;
|
||||
var Characteristic = require("hap-nodejs").Characteristic;
|
||||
var Accessory = require("hap-nodejs").Accessory;
|
||||
var uuid = require("hap-nodejs").uuid;
|
||||
var inherits = require('util').inherits;
|
||||
|
||||
process.env.WINK_NO_CACHE = true;
|
||||
|
||||
var model = {
|
||||
light_bulbs: require('wink-js/lib/model/light')
|
||||
};
|
||||
|
||||
|
||||
function WinkPlatform(log, config){
|
||||
|
||||
// auth info
|
||||
this.client_id = config["client_id"];
|
||||
this.client_secret = config["client_secret"];
|
||||
|
||||
this.username = config["username"];
|
||||
this.password = config["password"];
|
||||
|
||||
this.log = log;
|
||||
this.deviceLookup = {};
|
||||
}
|
||||
|
||||
WinkPlatform.prototype = {
|
||||
reloadData: function(callback) {
|
||||
this.log("Refreshing Wink Data");
|
||||
var that = this;
|
||||
wink.user().devices(function(devices) {
|
||||
for (var i=0; i<devices.data.length; i++){
|
||||
var device = devices.data[i];
|
||||
var accessory = that.deviceLookup[device.light_bulb_id | ""];
|
||||
if (accessory != undefined) {
|
||||
accessory.device = device;
|
||||
accessory.loadData();
|
||||
}
|
||||
}
|
||||
if (callback) callback();
|
||||
});
|
||||
},
|
||||
accessories: function(callback) {
|
||||
this.log("Fetching Wink devices.");
|
||||
|
||||
var that = this;
|
||||
var foundAccessories = [];
|
||||
this.deviceLookup = {};
|
||||
|
||||
var refreshLoop = function(){
|
||||
setInterval(that.reloadData.bind(that), 30000);
|
||||
};
|
||||
|
||||
wink.init({
|
||||
"client_id": this.client_id,
|
||||
@@ -34,219 +62,172 @@ WinkPlatform.prototype = {
|
||||
that.log("There was a problem authenticating with Wink.");
|
||||
} else {
|
||||
// success
|
||||
wink.user().devices('light_bulbs', function(devices) {
|
||||
wink.user().devices(function(devices) {
|
||||
for (var i=0; i<devices.data.length; i++){
|
||||
device = model.light_bulbs(devices.data[i], wink)
|
||||
accessory = new WinkAccessory(that.log, device);
|
||||
foundAccessories.push(accessory);
|
||||
var device = devices.data[i];
|
||||
var accessory = null;
|
||||
if (device.light_bulb_id !== undefined) {
|
||||
accessory = new WinkLightAccessory(that.log, device);
|
||||
}
|
||||
if (accessory != undefined) {
|
||||
that.deviceLookup[accessory.deviceId] = accessory;
|
||||
foundAccessories.push(accessory);
|
||||
}
|
||||
}
|
||||
refreshLoop();
|
||||
callback(foundAccessories);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function WinkAccessory(log, device) {
|
||||
// device info
|
||||
this.name = device.name;
|
||||
this.device = device;
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
WinkAccessory.prototype = {
|
||||
getPowerState: function(callback){
|
||||
if (!this.device) {
|
||||
this.log("No '"+this.name+"' device found (yet?)");
|
||||
return;
|
||||
}
|
||||
|
||||
var that = this;
|
||||
|
||||
this.log("checking power state for: " + this.name);
|
||||
wink.user().device(this.name, function(light_obj){
|
||||
powerState = light_obj.desired_state.powered
|
||||
that.log("power state for " + that.name + " is: " + powerState)
|
||||
callback(powerState);
|
||||
});
|
||||
|
||||
|
||||
},
|
||||
|
||||
getBrightness: function(callback){
|
||||
if (!this.device) {
|
||||
this.log("No '"+this.name+"' device found (yet?)");
|
||||
return;
|
||||
}
|
||||
|
||||
var that = this;
|
||||
|
||||
this.log("checking brightness level for: " + this.name);
|
||||
wink.user().device(this.name, function(light_obj){
|
||||
level = light_obj.desired_state.brightness * 100
|
||||
that.log("brightness level for " + that.name + " is: " + level)
|
||||
callback(level);
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
setPowerState: function(powerOn) {
|
||||
if (!this.device) {
|
||||
this.log("No '"+this.name+"' device found (yet?)");
|
||||
return;
|
||||
}
|
||||
|
||||
var that = this;
|
||||
|
||||
if (powerOn) {
|
||||
this.log("Setting power state on the '"+this.name+"' to on");
|
||||
this.device.power.on(function(response) {
|
||||
if (response === undefined) {
|
||||
that.log("Error setting power state on the '"+that.name+"'")
|
||||
} else {
|
||||
that.log("Successfully set power state on the '"+that.name+"' to on");
|
||||
}
|
||||
});
|
||||
}else{
|
||||
this.log("Setting power state on the '"+this.name+"' to off");
|
||||
this.device.power.off(function(response) {
|
||||
if (response === undefined) {
|
||||
that.log("Error setting power state on the '"+that.name+"'")
|
||||
} else {
|
||||
that.log("Successfully set power state on the '"+that.name+"' to off");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
setBrightness: function(level) {
|
||||
if (!this.device) {
|
||||
this.log("No '"+this.name+"' device found (yet?)");
|
||||
return;
|
||||
}
|
||||
|
||||
var that = this;
|
||||
|
||||
this.log("Setting brightness on the '"+this.name+"' to " + level);
|
||||
this.device.brightness(level, function(response) {
|
||||
if (response === undefined) {
|
||||
that.log("Error setting brightness on the '"+that.name+"'")
|
||||
} else {
|
||||
that.log("Successfully set brightness on the '"+that.name+"' to " + level);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
getServices: function() {
|
||||
var that = this;
|
||||
return [{
|
||||
sType: types.ACCESSORY_INFORMATION_STYPE,
|
||||
characteristics: [{
|
||||
cType: types.NAME_CTYPE,
|
||||
onUpdate: null,
|
||||
perms: ["pr"],
|
||||
format: "string",
|
||||
initialValue: this.name,
|
||||
supportEvents: false,
|
||||
supportBonjour: false,
|
||||
manfDescription: "Name of the accessory",
|
||||
designedMaxLength: 255
|
||||
},{
|
||||
cType: types.MANUFACTURER_CTYPE,
|
||||
onUpdate: null,
|
||||
perms: ["pr"],
|
||||
format: "string",
|
||||
initialValue: "Wink",
|
||||
supportEvents: false,
|
||||
supportBonjour: false,
|
||||
manfDescription: "Manufacturer",
|
||||
designedMaxLength: 255
|
||||
},{
|
||||
cType: types.MODEL_CTYPE,
|
||||
onUpdate: null,
|
||||
perms: ["pr"],
|
||||
format: "string",
|
||||
initialValue: "Rev-1",
|
||||
supportEvents: false,
|
||||
supportBonjour: false,
|
||||
manfDescription: "Model",
|
||||
designedMaxLength: 255
|
||||
},{
|
||||
cType: types.SERIAL_NUMBER_CTYPE,
|
||||
onUpdate: null,
|
||||
perms: ["pr"],
|
||||
format: "string",
|
||||
initialValue: "A1S2NASF88EW",
|
||||
supportEvents: false,
|
||||
supportBonjour: false,
|
||||
manfDescription: "SN",
|
||||
designedMaxLength: 255
|
||||
},{
|
||||
cType: types.IDENTIFY_CTYPE,
|
||||
onUpdate: null,
|
||||
perms: ["pw"],
|
||||
format: "bool",
|
||||
initialValue: false,
|
||||
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: true,
|
||||
supportBonjour: false,
|
||||
manfDescription: "Name of service",
|
||||
designedMaxLength: 255
|
||||
},{
|
||||
cType: types.POWER_STATE_CTYPE,
|
||||
onUpdate: function(value) {
|
||||
that.setPowerState(value);
|
||||
},
|
||||
onRead: function(callback) {
|
||||
that.getPowerState(function(powerState){
|
||||
callback(powerState);
|
||||
});
|
||||
},
|
||||
perms: ["pw","pr","ev"],
|
||||
format: "bool",
|
||||
initialValue: 0,
|
||||
supportEvents: true,
|
||||
supportBonjour: false,
|
||||
manfDescription: "Change the power state of the Bulb",
|
||||
designedMaxLength: 1
|
||||
},{
|
||||
cType: types.BRIGHTNESS_CTYPE,
|
||||
onUpdate: function(value) {
|
||||
that.setBrightness(value);
|
||||
},
|
||||
onRead: function(callback) {
|
||||
that.getBrightness(function(level){
|
||||
callback(level);
|
||||
});
|
||||
},
|
||||
perms: ["pw","pr","ev"],
|
||||
format: "int",
|
||||
initialValue: 0,
|
||||
supportEvents: true,
|
||||
supportBonjour: false,
|
||||
manfDescription: "Adjust Brightness of Light",
|
||||
designedMinValue: 0,
|
||||
designedMaxValue: 100,
|
||||
designedMinStep: 1,
|
||||
unit: "%"
|
||||
}]
|
||||
}];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Base Accessory
|
||||
*/
|
||||
|
||||
function WinkAccessory(log, device, type, typeId) {
|
||||
// construct base
|
||||
this.device = device;
|
||||
this.name = device.name;
|
||||
this.log = log;
|
||||
if (typeId == undefined) {
|
||||
typeId = this.name;
|
||||
log("WARN: Unable to find id of " + this.name + " so using name instead");
|
||||
}
|
||||
this.deviceGroup = type + 's';
|
||||
this.deviceId = typeId;
|
||||
var idKey = 'hbdev:wink:' + type + ':' + typeId;
|
||||
var id = uuid.generate(idKey);
|
||||
Accessory.call(this, this.name, id);
|
||||
this.uuid_base = id;
|
||||
|
||||
this.control = wink.device_group(this.deviceGroup).device_id(this.deviceId);
|
||||
|
||||
// set some basic properties (these values are arbitrary and setting them is optional)
|
||||
this
|
||||
.getService(Service.AccessoryInformation)
|
||||
.setCharacteristic(Characteristic.Manufacturer, this.device.device_manufacturer)
|
||||
.setCharacteristic(Characteristic.Model, this.device.model_name);
|
||||
|
||||
WinkAccessory.prototype.loadData.call(this);
|
||||
}
|
||||
|
||||
inherits(WinkAccessory, Accessory);
|
||||
WinkAccessory.prototype.parent = Accessory.prototype;
|
||||
|
||||
WinkAccessory.prototype.getServices = function() {
|
||||
return this.services;
|
||||
};
|
||||
|
||||
WinkAccessory.prototype.loadData = function() {
|
||||
};
|
||||
|
||||
WinkAccessory.prototype.handleResponse = function(res) {
|
||||
if (!res) {
|
||||
return Error("No response from Wink");
|
||||
} else if (res.errors && res.errors.length > 0) {
|
||||
return res.errors[0];
|
||||
} else if (res.data) {
|
||||
this.device = res.data;
|
||||
this.loadData();
|
||||
}
|
||||
};
|
||||
|
||||
WinkAccessory.prototype.reloadData = function(callback){
|
||||
var that = this;
|
||||
this.control.get(function(res) {
|
||||
callback(that.handleResponse(res));
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Light Accessory
|
||||
*/
|
||||
|
||||
function WinkLightAccessory(log, device) {
|
||||
// construct base
|
||||
WinkAccessory.call(this, log, device, 'light_bulb', device.light_bulb_id);
|
||||
|
||||
// accessor
|
||||
var that = this;
|
||||
|
||||
that.device = device;
|
||||
that.deviceControl = model.light_bulbs(device, wink);
|
||||
|
||||
this
|
||||
.addService(Service.Lightbulb)
|
||||
.getCharacteristic(Characteristic.On)
|
||||
.on('get', function(callback) {
|
||||
var powerState = that.device.desired_state.powered;
|
||||
that.log("power state for " + that.name + " is: " + powerState);
|
||||
callback(null, powerState != undefined ? powerState : false);
|
||||
})
|
||||
.on('set', function(powerOn, callback) {
|
||||
if (powerOn) {
|
||||
that.log("Setting power state on the '"+that.name+"' to on");
|
||||
that.deviceControl.power.on(function(response) {
|
||||
if (response === undefined) {
|
||||
that.log("Error setting power state on the '"+that.name+"'");
|
||||
callback(Error("Error setting power state on the '"+that.name+"'"));
|
||||
} else {
|
||||
that.log("Successfully set power state on the '"+that.name+"' to on");
|
||||
callback(null, powerOn);
|
||||
}
|
||||
});
|
||||
}else{
|
||||
that.log("Setting power state on the '"+that.name+"' to off");
|
||||
that.deviceControl.power.off(function(response) {
|
||||
if (response === undefined) {
|
||||
that.log("Error setting power state on the '"+that.name+"'");
|
||||
callback(Error("Error setting power state on the '"+that.name+"'"));
|
||||
} else {
|
||||
that.log("Successfully set power state on the '"+that.name+"' to off");
|
||||
callback(null, powerOn);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
this
|
||||
.getService(Service.Lightbulb)
|
||||
.getCharacteristic(Characteristic.Brightness)
|
||||
.on('get', function(callback) {
|
||||
var level = that.device.desired_state.brightness * 100;
|
||||
that.log("brightness level for " + that.name + " is: " + level);
|
||||
callback(null, level);
|
||||
})
|
||||
.on('set', function(level, callback) {
|
||||
that.log("Setting brightness on the '"+this.name+"' to " + level);
|
||||
that.deviceControl.brightness(level, function(response) {
|
||||
if (response === undefined) {
|
||||
that.log("Error setting brightness on the '"+that.name+"'");
|
||||
callback(Error("Error setting brightness on the '"+that.name+"'"));
|
||||
} else {
|
||||
that.log("Successfully set brightness on the '"+that.name+"' to " + level);
|
||||
callback(null, level);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
WinkLightAccessory.prototype.loadData.call(this);
|
||||
}
|
||||
|
||||
inherits(WinkLightAccessory, WinkAccessory);
|
||||
WinkLightAccessory.prototype.parent = WinkAccessory.prototype;
|
||||
|
||||
WinkLightAccessory.prototype.loadData = function() {
|
||||
this.parent.loadData.call(this);
|
||||
this.getService(Service.Lightbulb)
|
||||
.getCharacteristic(Characteristic.On)
|
||||
.getValue();
|
||||
this.getService(Service.Lightbulb)
|
||||
.getCharacteristic(Characteristic.Brightness)
|
||||
.getValue();
|
||||
};
|
||||
|
||||
module.exports.accessory = WinkAccessory;
|
||||
module.exports.lightAccessory = WinkLightAccessory;
|
||||
module.exports.platform = WinkPlatform;
|
||||
|
||||
Reference in New Issue
Block a user