diff --git a/package.json b/package.json index b920048..a936599 100644 --- a/package.json +++ b/package.json @@ -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": "", diff --git a/platforms/Wink.js b/platforms/Wink.js index d9de07f..b8da568 100644 --- a/platforms/Wink.js +++ b/platforms/Wink.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 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;