Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Andre Schröter
2015-09-12 20:11:31 +02:00
22 changed files with 3067 additions and 938 deletions

View File

@@ -8,6 +8,15 @@
// - Added support for Scenes
// - Sorting device names
//
// 26 August 2015 [EddyK69]
// - Added parameter in config.json: 'loadscenes' for enabling/disabling loading scenes
// - Fixed issue with dimmer-range; was 0-100, should be 0-16
//
// 27 August 2015 [EddyK69]
// - Fixed issue that 'on/off'-type lights showed as dimmers in HomeKit. Checking now on SwitchType instead of HaveDimmer
// - Fixed issue that 'on-off'-type lights would not react on Siri 'Switch on/off light'; On/Off types are now handled as Lights instead of Switches
// (Cannot determine if 'on/off'-type device is a Light or a Switch :( )
//
// Domoticz JSON API required
// https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Lights_and_switches
//
@@ -18,7 +27,8 @@
// "name": "Domoticz",
// "server": "127.0.0.1",
// "port": "8080",
// "roomid": 123 (0=no roomplan)
// "roomid": 123, (0=no roomplan)
// "loadscenes": 1 (0=disable scenes)
// }
// ],
//
@@ -47,6 +57,10 @@ function DomoticzPlatform(log, config){
if (typeof config["roomid"] != 'undefined') {
this.roomid = config["roomid"];
}
this.loadscenes = 1;
if (typeof config["loadscenes"] != 'undefined') {
this.loadscenes = config["loadscenes"];
}
}
function sortByKey(array, key) {
@@ -66,11 +80,17 @@ DomoticzPlatform.prototype = {
},
accessories: function(callback) {
this.log("Fetching Domoticz lights and switches...");
var that = this;
var foundAccessories = [];
if (this.roomid == 0) {
this.log("Fetching Domoticz lights and switches...");
var that = this;
var foundAccessories = [];
// mechanism to ensure callback is only executed once all requests complete
var asyncCalls = 0;
function callbackLater() { if (--asyncCalls == 0) callback(foundAccessories); }
if (this.roomid == 0) {
//Get Lights
asyncCalls++;
request.get({
url: this.urlForQuery("type=devices&filter=light&used=true&order=Name"),
json: true
@@ -79,18 +99,20 @@ DomoticzPlatform.prototype = {
if (json['result'] != undefined) {
var sArray=sortByKey(json['result'],"Name");
sArray.map(function(s) {
accessory = new DomoticzAccessory(that.log, that, false, s.idx, s.Name, s.HaveDimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW"));
var havedimmer = (s.SwitchType == 'Dimmer')
accessory = new DomoticzAccessory(that.log, that, false, s.idx, s.Name, havedimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW"));
foundAccessories.push(accessory);
})
}
callback(foundAccessories);
callbackLater();
} else {
that.log("There was a problem connecting to Domoticz. (" + err + ")");
}
});
}
else {
}
else {
//Get all devices specified in the room
asyncCalls++;
request.get({
url: this.urlForQuery("type=devices&plan=" + this.roomid),
json: true
@@ -101,36 +123,39 @@ DomoticzPlatform.prototype = {
sArray.map(function(s) {
//only accept switches for now
if (typeof s.SwitchType != 'undefined') {
accessory = new DomoticzAccessory(that.log, that, false, s.idx, s.Name, s.HaveDimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW"));
var havedimmer = (s.SwitchType == 'Dimmer')
accessory = new DomoticzAccessory(that.log, that, false, s.idx, s.Name, havedimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW"));
foundAccessories.push(accessory);
}
})
}
callback(foundAccessories);
callbackLater();
} else {
that.log("There was a problem connecting to Domoticz.");
}
});
}
}
//Get Scenes
foundAccessories = [];
request.get({
url: this.urlForQuery("type=scenes"),
json: true
}, function(err, response, json) {
if (!err && response.statusCode == 200) {
if (json['result'] != undefined) {
var sArray=sortByKey(json['result'],"Name");
sArray.map(function(s) {
accessory = new DomoticzAccessory(that.log, that, true, s.idx, s.Name, false, 0, false);
foundAccessories.push(accessory);
})
if (this.loadscenes == 1) {
asyncCalls++;
request.get({
url: this.urlForQuery("type=scenes"),
json: true
}, function(err, response, json) {
if (!err && response.statusCode == 200) {
if (json['result'] != undefined) {
var sArray=sortByKey(json['result'],"Name");
sArray.map(function(s) {
accessory = new DomoticzAccessory(that.log, that, true, s.idx, s.Name, false, 0, false);
foundAccessories.push(accessory);
})
}
callbackLater();
} else {
that.log("There was a problem connecting to Domoticz.");
}
callback(foundAccessories);
} else {
that.log("There was a problem connecting to Domoticz.");
}
});
});
}
}
}
@@ -158,6 +183,9 @@ DomoticzAccessory.prototype = {
url = this.platform.urlForQuery("type=command&param=setcolbrightnessvalue&idx=" + this.idx + "&hue=" + value + "&brightness=100" + "&iswhite=false");
}
else if (c == "setLevel") {
//Range should be 0-16 instead of 0-100
//See http://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s#Set_a_dimmable_light_to_a_certain_level
value = Math.round((value / 100) * 16)
url = this.platform.urlForQuery("type=command&param=switchlight&idx=" + this.idx + "&switchcmd=Set%20Level&level=" + value);
}
else if (value != undefined) {
@@ -311,11 +339,11 @@ DomoticzAccessory.prototype = {
},
sType: function() {
if (this.HaveDimmer == true) {
//if (this.HaveDimmer == true) {
return types.LIGHTBULB_STYPE
} else {
return types.SWITCH_STYPE
}
//} else {
// return types.SWITCH_STYPE
//}
},
getServices: function() {

203
platforms/KNX.js Normal file
View File

@@ -0,0 +1,203 @@
/** Sample platform outline
* based on Sonos platform
*/
'use strict';
var types = require("HAP-NodeJS/accessories/types.js");
//var hardware = require('myHardwareSupport'); //require any additional hardware packages
var knxd = require('eibd');
function KNXPlatform(log, config){
this.log = log;
this.config = config;
// this.property1 = config.property1;
// this.property2 = config.property2;
// initiate connection to bus for listening ==> done with first shim
};
KNXPlatform.prototype = {
accessories: function(callback) {
this.log("Fetching KNX devices.");
var that = this;
// iterate through all devices the platform my offer
// for each device, create an accessory
// read accessories from file !!!!!
var foundAccessories = this.config.accessories;
//create array of accessories
var myAccessories = [];
for (var int = 0; int < foundAccessories.length; int++) {
this.log("parsing acc " + int + " of " + foundAccessories.length);
// instantiate and push to array
switch (foundAccessories[int].accessory_type) {
case "knxdevice":
this.log("push new universal device "+foundAccessories[int].name);
// push knxd connection setting to each device from platform
foundAccessories[int].knxd_ip = this.config.knxd_ip;
foundAccessories[int].knxd_port = this.config.knxd_port;
var accConstructor = require('./../accessories/knxdevice.js');
var acc = new accConstructor.accessory(this.log,foundAccessories[int]);
this.log("created "+acc.name+" universal accessory");
myAccessories.push(acc);
break;
default:
// do something else
this.log("unkown accessory type found")
}
};
// if done, return the array to callback function
this.log("returning "+myAccessories.length+" accessories");
callback(myAccessories);
}
};
/**
* The buscallbacks module is to expose a simple function to listen on the bus and register callbacks for value changes
* of registered addresses.
*
* Usage:
* You can start the monitoring process at any time
startMonitor({host: name-ip, port: port-num });
* You can add addresses to the subscriptions using
registerGA(groupAddress, callback)
* groupAddress has to be an groupAddress in common knx notation string '1/2/3'
* the callback has to be a
* var f = function(value) { handle value update;}
* so you can do a
* registerGA('1/2/3', function(value){
* console.log('1/2/3 got a hit with '+value);
* });
* but of course it is meant to be used programmatically, not literally, otherwise it has no advantage
*
* You can also use arrays of addresses if your callback is supposed to listen to many addresses:
registerGA(groupAddresses[], callback)
* as in
* registerGA(['1/2/3','1/0/0'], function(value){
* console.log('1/2/3 or 1/0/0 got a hit with '+value);
* });
* if you are having central addresses like "all lights off" or additional response objects
*
*
* callbacks can have a signature of
* function(value, src, dest, type) but do not have to support these parameters (order matters)
* src = physical address such as '1.1.20'
* dest = groupAddress hit (you subscribed to that address, remember?), as '1/2/3'
* type = Data point type, as 'DPT1'
*
*
*/
//array of registered addresses and their callbacks
var subscriptions = [];
//check variable to avoid running two listeners
var running;
function groupsocketlisten(opts, callback) {
var conn = knxd.Connection();
conn.socketRemote(opts, function() {
conn.openGroupSocket(0, callback);
});
}
var registerSingleGA = function registerSingleGA (groupAddress, callback) {
subscriptions.push({address: groupAddress, callback: callback });
}
/*
* public busMonitor.startMonitor()
* starts listening for telegrams on KNX bus
*
*/
var startMonitor = function startMonitor(opts) { // using { host: name-ip, port: port-num } options object
if (!running) {
running = true;
} else {
console.log("<< knxd socket listener already running >>");
return null;
}
console.log(">>> knxd groupsocketlisten starting <<<");
groupsocketlisten(opts, function(parser) {
//console.log("knxfunctions.read: in callback parser");
parser.on('write', function(src, dest, type, val){
// search the registered group addresses
//console.log('recv: Write from '+src+' to '+dest+': '+val+' ['+type+'], listeners:' + subscriptions.length);
for (var i = 0; i < subscriptions.length; i++) {
// iterate through all registered addresses
if (subscriptions[i].address === dest) {
// found one, notify
console.log('HIT: Write from '+src+' to '+dest+': '+val+' ['+type+']');
subscriptions[i].callback(val, src, dest, type);
}
}
});
parser.on('response', function(src, dest, type, val) {
// search the registered group addresses
// console.log('recv: resp from '+src+' to '+dest+': '+val+' ['+type+']');
for (var i = 0; i < subscriptions.length; i++) {
// iterate through all registered addresses
if (subscriptions[i].address === dest) {
// found one, notify
// console.log('HIT: Response from '+src+' to '+dest+': '+val+' ['+type+']');
subscriptions[i].callback(val, src, dest, type);
}
}
});
//dont care about reads here
// parser.on('read', function(src, dest) {
// console.log('Read from '+src+' to '+dest);
// });
//console.log("knxfunctions.read: in callback parser at end");
}); // groupsocketlisten parser
}; //startMonitor
/*
* public registerGA(groupAdresses[], callback(value))
* parameters
* callback: function(value, src, dest, type) called when a value is sent on the bus
* groupAddresses: (Array of) string(s) for group addresses
*
*
*
*/
var registerGA = function (groupAddresses, callback) {
// check if the groupAddresses is an array
if (groupAddresses.constructor.toString().indexOf("Array") > -1) {
// handle multiple addresses
for (var i = 0; i < groupAddresses.length; i++) {
if (groupAddresses[i]) { // do not bind empty addresses
registerSingleGA (groupAddresses[i], callback);
}
}
} else {
// it's only one
registerSingleGA (groupAddresses, callback);
}
// console.log("listeners now: " + subscriptions.length);
};
module.exports.platform = KNXPlatform;
module.exports.registerGA = registerGA;
module.exports.startMonitor = startMonitor;

302
platforms/LIFx.js Normal file
View File

@@ -0,0 +1,302 @@
'use strict';
// LiFX Platform Shim for HomeBridge
//
// Remember to add platform to config.json. Example:
// "platforms": [
// {
// "platform": "LIFx", // required
// "name": "LIFx", // required
// "access_token": "access token", // required
// "use_lan": "true" // optional set to "true" (gets and sets over the lan) or "get" (gets only over the lan)
// }
// ],
//
// When you attempt to add a device, it will ask for a "PIN code".
// The default code for all HomeBridge accessories is 031-45-154.
//
var Service = require("HAP-NodeJS").Service;
var Characteristic = require("HAP-NodeJS").Characteristic;
var lifxRemoteObj = require('lifx-api');
var lifx_remote;
var lifxLanObj;
var lifx_lan;
var use_lan;
function LIFxPlatform(log, config){
// auth info
this.access_token = config["access_token"];
lifx_remote = new lifxRemoteObj(this.access_token);
// use remote or lan api ?
use_lan = config["use_lan"] || false;
if (use_lan != false) {
lifxLanObj = require('lifx');
lifx_lan = lifxLanObj.init();
}
this.log = log;
}
LIFxPlatform.prototype = {
accessories: function(callback) {
this.log("Fetching LIFx devices.");
var that = this;
var foundAccessories = [];
lifx_remote.listLights("all", function(body) {
var bulbs = JSON.parse(body);
for(var i = 0; i < bulbs.length; i ++) {
var accessory = new LIFxBulbAccessory(that.log, bulbs[i]);
foundAccessories.push(accessory);
}
callback(foundAccessories)
});
}
}
function LIFxBulbAccessory(log, bulb) {
// device info
this.name = bulb.label;
this.model = bulb.product_name;
this.deviceId = bulb.id;
this.serial = bulb.uuid;
this.capabilities = bulb.capabilities;
this.log = log;
}
LIFxBulbAccessory.prototype = {
getLan: function(type, callback){
var that = this;
if (!lifx_lan.bulbs[this.deviceId]) {
callback(new Error("Device not found"), false);
return;
}
lifx_lan.requestStatus();
lifx_lan.on('bulbstate', function(bulb) {
if (callback == null) {
return;
}
if (bulb.addr.toString('hex') == that.deviceId) {
switch(type) {
case "power":
callback(null, bulb.state.power > 0);
break;
case "brightness":
callback(null, Math.round(bulb.state.brightness * 100 / 65535));
break;
case "hue":
callback(null, Math.round(bulb.state.hue * 360 / 65535));
break;
case "saturation":
callback(null, Math.round(bulb.state.saturation * 100 / 65535));
break;
}
callback = null
}
});
},
getRemote: function(type, callback){
var that = this;
lifx_remote.listLights("id:"+ that.deviceId, function(body) {
var bulb = JSON.parse(body);
if (bulb.connected != true) {
callback(new Error("Device not found"), false);
return;
}
switch(type) {
case "power":
callback(null, bulb.power == "on" ? 1 : 0);
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;
}
});
},
identify: function(callback) {
lifx_remote.breatheEffect("id:"+ this.deviceId, 'green', null, 1, 3, false, true, 0.5, function (body) {
callback();
});
},
setLanColor: function(type, value, callback){
var bulb = lifx_lan.bulbs[this.deviceId];
if (!bulb) {
callback(new Error("Device not found"), false);
return;
}
var state = {
hue: bulb.state.hue,
saturation: bulb.state.saturation,
brightness: bulb.state.brightness,
kelvin: bulb.state.kelvin
};
var scale = type == "hue" ? 360 : 100;
state[type] = Math.round(value * 65535 / scale) & 0xffff;
lifx_lan.lightsColour(state.hue, state.saturation, state.brightness, state.kelvin, 0, bulb);
callback(null);
},
setLanPower: function(state, callback){
var bulb = lifx_lan.bulbs[this.deviceId];
if (!bulb) {
callback(new Error("Device not found"), false);
return;
}
if (state) {
lifx_lan.lightsOn(bulb);
}
else {
lifx_lan.lightsOff(bulb);
}
callback(null);
},
setRemoteColor: function(type, value, callback){
var color;
switch(type) {
case "brightness":
color = "brightness:" + (value / 100);
break;
case "hue":
color = "hue:" + value;
break;
case "saturation":
color = "saturation:" + (value / 100);
break;
}
lifx_remote.setColor("id:"+ this.deviceId, color, 0, null, function (body) {
callback();
});
},
setRemotePower: function(state, callback){
var that = this;
lifx_remote.setPower("id:"+ that.deviceId, (state == 1 ? "on" : "off"), 0, function (body) {
callback();
});
},
getServices: function() {
var that = this;
var services = []
var service = new Service.Lightbulb(this.name);
switch(use_lan) {
case true:
case "true":
// gets and sets over the lan api
service
.getCharacteristic(Characteristic.On)
.on('get', function(callback) { that.getLan("power", callback);})
.on('set', function(value, callback) {that.setLanPower(value, callback);});
service
.addCharacteristic(Characteristic.Brightness)
.on('get', function(callback) { that.getLan("brightness", callback);})
.on('set', function(value, callback) { that.setLanColor("brightness", value, callback);});
if (this.capabilities.has_color == true) {
service
.addCharacteristic(Characteristic.Hue)
.on('get', function(callback) { that.getLan("hue", callback);})
.on('set', function(value, callback) { that.setLanColor("hue", value, callback);});
service
.addCharacteristic(Characteristic.Saturation)
.on('get', function(callback) { that.getLan("saturation", callback);})
.on('set', function(value, callback) { that.setLanColor("saturation", value, callback);});
}
break;
case "get":
// gets over the lan api, sets over the remote api
service
.getCharacteristic(Characteristic.On)
.on('get', function(callback) { that.getLan("power", callback);})
.on('set', function(value, callback) {that.setRemotePower(value, callback);});
service
.addCharacteristic(Characteristic.Brightness)
.on('get', function(callback) { that.getLan("brightness", callback);})
.on('set', function(value, callback) { that.setRemoteColor("brightness", value, callback);});
if (this.capabilities.has_color == true) {
service
.addCharacteristic(Characteristic.Hue)
.on('get', function(callback) { that.getLan("hue", callback);})
.on('set', function(value, callback) { that.setRemoteColor("hue", value, callback);});
service
.addCharacteristic(Characteristic.Saturation)
.on('get', function(callback) { that.getLan("saturation", callback);})
.on('set', function(value, callback) { that.setRemoteColor("saturation", value, callback);});
}
break;
default:
// gets and sets over the remote api
service
.getCharacteristic(Characteristic.On)
.on('get', function(callback) { that.getRemote("power", callback);})
.on('set', function(value, callback) {that.setRemotePower(value, callback);});
service
.addCharacteristic(Characteristic.Brightness)
.on('get', function(callback) { that.getRemote("brightness", callback);})
.on('set', function(value, callback) { that.setRemoteColor("brightness", value, callback);});
if (this.capabilities.has_color == true) {
service
.addCharacteristic(Characteristic.Hue)
.on('get', function(callback) { that.getRemote("hue", callback);})
.on('set', function(value, callback) { that.setRemoteColor("hue", value, callback);});
service
.addCharacteristic(Characteristic.Saturation)
.on('get', function(callback) { that.getRemote("saturation", callback);})
.on('set', function(value, callback) { that.setRemoteColor("saturation", value, callback);});
}
}
services.push(service);
service = new Service.AccessoryInformation();
service
.setCharacteristic(Characteristic.Manufacturer, "LIFX")
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
services.push(service);
return services;
}
}
module.exports.accessory = LIFxBulbAccessory;
module.exports.platform = LIFxPlatform;

242
platforms/MiLight.js Normal file
View File

@@ -0,0 +1,242 @@
/*
MiLight platform shim for Homebridge
Written by Sam Edwards (https://samedwards.ca/)
Uses the node-milight-promise library (https://github.com/mwittig/node-milight-promise) which features some code from
applamp.nl (http://www.applamp.nl/service/applamp-api/) and uses other details from (http://www.limitlessled.com/dev/)
Configure in config.json as follows:
"platforms": [
{
"platform":"MiLight",
"name":"MiLight",
"ip_address": "255.255.255.255",
"port": 8899,
"type": "rgbw",
"delay": 30,
"repeat": 3,
"zones":["Kitchen Lamp","Bedroom Lamp","Living Room Lamp","Hallway Lamp"]
}
]
Where the parameters are:
*platform (required): This must be "MiLight", and refers to the name of the accessory as exported from this file
*name (optional): The display name used for logging output by Homebridge. Best to set to "MiLight"
*ip_address (optional): The IP address of the WiFi Bridge. Default to the broadcast address of 255.255.255.255 if not specified
*port (optional): Port of the WiFi bridge. Defaults to 8899 if not specified
*type (optional): One of either "rgbw", "rgb", or "white", depending on the type of bulb being controlled. This applies to all zones. Defaults to rgbw.
*delay (optional): Delay between commands sent over UDP. Default 30ms. May cause delays when sending a lot of commands. Try decreasing to improve.
*repeat (optional): Number of times to repeat the UDP command for better reliability. Default 3
*zones (required): An array of the names of the zones, in order, 1-4. Use null if a zone is skipped. RGB lamps can only have a single zone.
Tips and Tricks:
*Setting the brightness of an rgbw or a white bulb will set it to "night mode", which is dimmer than the lowest brightness setting
*White and rgb bulbs don't support absolute brightness setting, so we just send a brightness up/brightness down command depending
if we got a percentage above/below 50% respectively
*The only exception to the above is that white bulbs support a "maximum brightness" command, so we send that when we get 100%
*Implemented warmer/cooler for white lamps in a similar way to brightnes, except this time above/below 180 degrees on the colour wheel
*I welcome feedback on a better way to work the brightness/hue for white and rgb bulbs
Troubleshooting:
The node-milight-promise library provides additional debugging output when the MILIGHT_DEBUG environmental variable is set
TODO:
*Possibly build in some sort of state logging and persistance so that we can answswer HomeKit status queries to the best of our ability
*/
var Service = require("HAP-NodeJS").Service;
var Characteristic = require("HAP-NodeJS").Characteristic;
var Milight = require('node-milight-promise').MilightController;
var commands = require('node-milight-promise').commands;
module.exports = {
accessory: MiLightAccessory,
platform: MiLightPlatform
}
function MiLightPlatform(log, config) {
this.log = log;
this.config = config;
}
MiLightPlatform.prototype = {
accessories: function(callback) {
var zones = [];
// Various error checking
if (this.config.zones) {
var zoneLength = this.config.zones.length;
} else {
this.log("ERROR: Could not read zones from configuration.");
return;
}
if (!this.config["type"]) {
this.log("INFO: Type not specified, defaulting to rgbw");
this.config["type"] = "rgbw";
}
if (zoneLength == 0) {
this.log("ERROR: No zones found in configuration.");
return;
} else if (this.config["type"] == "rgb" && zoneLength > 1) {
this.log("WARNING: RGB lamps only have a single zone. Only the first defined zone will be used.");
zoneLength = 1;
} else if (zoneLength > 4) {
this.log("WARNING: Only a maximum of 4 zones are supported per bridge. Only recognizing the first 4 zones.");
zoneLength = 4;
}
// Create lamp accessories for all of the defined zones
for (var i=0; i < zoneLength; i++) {
if (!!this.config.zones[i]) {
this.config["name"] = this.config.zones[i];
this.config["zone"] = i+1;
lamp = new MiLightAccessory(this.log, this.config);
zones.push(lamp);
}
}
if (zones.length > 0) {
callback(zones);
} else {
this.log("ERROR: Unable to find any valid zones");
return;
}
}
}
function MiLightAccessory(log, config) {
this.log = log;
// config info
this.ip_address = config["ip_address"];
this.port = config["port"];
this.name = config["name"];
this.zone = config["zone"];
this.type = config["type"];
this.delay = config["delay"];
this.repeat = config["repeat"];
this.light = new Milight({
ip: this.ip_address,
port: this.port,
delayBetweenCommands: this.delay,
commandRepeat: this.repeat
});
}
MiLightAccessory.prototype = {
setPowerState: function(powerOn, callback) {
if (powerOn) {
this.log("["+this.name+"] Setting power state to on");
this.light.sendCommands(commands[this.type].on(this.zone));
} else {
this.log("["+this.name+"] Setting power state to off");
this.light.sendCommands(commands[this.type].off(this.zone));
}
callback();
},
setBrightness: function(level, callback) {
if (level == 0) {
// If brightness is set to 0, turn off the lamp
this.log("["+this.name+"] Setting brightness to 0 (off)");
this.light.sendCommands(commands[this.type].off(this.zone));
} else 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("["+this.name+"] Setting night mode", level);
this.light.sendCommands(commands[this.type].off(this.zone));
// Ensure we're pausing for 100ms between these commands as per the spec
this.light.pause(100);
this.light.sendCommands(commands[this.type].nightMode(this.zone));
} else {
this.log("["+this.name+"] Setting brightness to %s", level);
// Send on command to ensure we're addressing the right bulb
this.light.sendCommands(commands[this.type].on(this.zone));
// If this is an rgbw lamp, set the absolute brightness specified
if (this.type == "rgbw") {
this.light.sendCommands(commands.rgbw.brightness(level));
} else {
// 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
this.light.sendCommands(commands.white.maxBright(this.zone));
} else {
this.light.sendCommands(commands[this.type].brightUp());
}
} else {
this.light.sendCommands(commands[this.type].brightDown());
}
}
}
callback();
},
setHue: function(value, callback) {
this.log("["+this.name+"] Setting hue to %s", value);
var hue = Array(value, 0, 0);
// Send on command to ensure we're addressing the right bulb
this.light.sendCommands(commands[this.type].on(this.zone));
if (this.type == "rgbw") {
if (value == 0) {
this.light.sendCommands(commands.rgbw.whiteMode(this.zone));
} else {
this.light.sendCommands(commands.rgbw.hue(commands.rgbw.hsvToMilightColor(hue)));
}
} else if (this.type == "rgb") {
this.light.sendCommands(commands.rgb.hue(commands.rgbw.hsvToMilightColor(hue)));
} 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
if (value >= 180) {
this.light.sendCommands(commands.white.cooler());
} else {
this.light.sendCommands(commands.white.warmer());
}
}
callback();
},
identify: function(callback) {
this.log("["+this.name+"] Identify requested!");
callback(); // success
},
getServices: function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "MiLight")
.setCharacteristic(Characteristic.Model, this.type)
.setCharacteristic(Characteristic.SerialNumber, "MILIGHT12345");
var lightbulbService = new Service.Lightbulb();
lightbulbService
.getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this));
lightbulbService
.addCharacteristic(new Characteristic.Brightness())
.on('set', this.setBrightness.bind(this));
lightbulbService
.addCharacteristic(new Characteristic.Hue())
.on('set', this.setHue.bind(this));
return [informationService, lightbulbService];
}
};

View File

@@ -216,7 +216,13 @@ PhilipsHueAccessory.prototype = {
that.log(device.name + ", characteristic: " + characteristic + ", value: " + value + ".");
}
else {
that.log(err);
if (err.code == "ECONNRESET") {
setTimeout(function() {
that.executeChange(api, device, characteristic, value);
}, 300);
} else {
that.log(err);
}
}
});
},

View File

@@ -6,6 +6,8 @@ function SonosPlatform(log, config){
this.config = config;
this.name = config["name"];
this.playVolume = config["play_volume"];
// timeout for device discovery
this.discoveryTimeout = (config.deviceDiscoveryTimeout || 10)*1000; // assume 10sec as a default
}
SonosPlatform.prototype = {
@@ -13,15 +15,42 @@ SonosPlatform.prototype = {
this.log("Fetching Sonos devices.");
var that = this;
// track found devices so we don't add duplicates
var roomNamesFound = {};
// collector array for the devices from callbacks
var devicesFound = [];
// tell the sonos callbacks if timeout already occured
var timeout = false;
// the timeout event will push the accessories back
setTimeout(function(){
timeout=true;
callback(devicesFound);
}, this.discoveryTimeout);
sonos.search(function (device) {
that.log("Found device at " + device.host);
device.deviceDescription(function (err, description) {
if (description["zoneType"] != '11') {
that.log("Found playable device - " + description["roomName"]);
// device is an instance of sonos.Sonos
var accessory = new SonosAccessory(that.log, that.config, device, description);
callback([accessory]);
if (description["zoneType"] != '11' && description["zoneType"] != '8') { // 8 is the Sonos SUB
var roomName = description["roomName"];
if (!roomNamesFound[roomName]) {
roomNamesFound[roomName] = true;
that.log("Found playable device - " + roomName);
if (timeout) {
that.log("Ignored: Discovered after timeout (Set deviceDiscoveryTimeout parameter in Sonos section of config.json)");
}
// device is an instance of sonos.Sonos
var accessory = new SonosAccessory(that.log, that.config, device, description);
// add it to the collector array
devicesFound.push(accessory);
}
else {
that.log("Ignoring playable device with duplicate room name - " + roomName);
}
}
});
});

View File

@@ -1,13 +1,19 @@
var types = require("HAP-NodeJS/accessories/types.js");
var Yamaha = require('yamaha-nodejs');
var mdns = require('mdns');
//workaround for raspberry pi
var sequence = [
mdns.rst.DNSServiceResolve(),
'DNSServiceGetAddrInfo' in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({families:[4]}),
mdns.rst.makeAddressesUnique()
];
function YamahaAVRPlatform(log, config){
this.log = log;
this.config = config;
this.playVolume = config["play_volume"];
this.setMainInputTo = config["setMainInputTo"];
this.browser = mdns.createBrowser(mdns.tcp('http'));
this.browser = mdns.createBrowser(mdns.tcp('http'), {resolverSequence: sequence});
}
YamahaAVRPlatform.prototype = {

667
platforms/ZWayServer.js Normal file
View File

@@ -0,0 +1,667 @@
var debug = require('debug')('ZWayServer');
var Service = require("HAP-NodeJS").Service;
var Characteristic = require("HAP-NodeJS").Characteristic;
var types = require("HAP-NodeJS/accessories/types.js");
var request = require("request");
var tough = require('tough-cookie');
var Q = require("q");
function ZWayServerPlatform(log, config){
this.log = log;
this.url = config["url"];
this.login = config["login"];
this.password = config["password"];
this.name_overrides = config["name_overrides"];
this.batteryLow = config["battery_low_level"] || 15;
this.pollInterval = config["poll_interval"] || 2;
this.splitServices= config["split_services"] || false;
this.lastUpdate = 0;
this.cxVDevMap = {};
this.vDevStore = {};
this.sessionId = "";
this.jar = request.jar(new tough.CookieJar());
}
ZWayServerPlatform.getVDevTypeKey = function(vdev){
return vdev.deviceType + (vdev.metrics && vdev.metrics.probeTitle ? "." + vdev.metrics.probeTitle : "")
}
ZWayServerPlatform.prototype = {
zwayRequest: function(opts){
var that = this;
var deferred = Q.defer();
opts.jar = true;//this.jar;
opts.json = true;
opts.headers = {
"Cookie": "ZWAYSession=" + this.sessionId
};
request(opts, function(error, response, body){
if(response.statusCode == 401){
debug("Authenticating...");
request({
method: "POST",
url: that.url + 'ZAutomation/api/v1/login',
body: { //JSON.stringify({
"form": true,
"login": that.login,
"password": that.password,
"keepme": false,
"default_ui": 1
},
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
json: true,
jar: true//that.jar
}, function(error, response, body){
if(response.statusCode == 200){
that.sessionId = body.data.sid;
opts.headers["Cookie"] = "ZWAYSession=" + that.sessionId;
debug("Authenticated. Resubmitting original request...");
request(opts, function(error, response, body){
if(response.statusCode == 200){
deferred.resolve(body);
} else {
deferred.reject(response);
}
});
} else {
deferred.reject(response);
}
});
} else if(response.statusCode == 200) {
deferred.resolve(body);
} else {
deferred.reject(response);
}
});
return deferred.promise;
}
,
accessories: function(callback) {
debug("Fetching Z-Way devices...");
//TODO: Unify this with getVDevServices, so there's only one place with mapping between service and vDev type.
//Note: Order matters!
var primaryDeviceClasses = [
"switchBinary",
"thermostat",
"sensorBinary.Door/Window",
"sensorMultilevel.Temperature",
"switchMultilevel"
];
var that = this;
var foundAccessories = [];
this.zwayRequest({
method: "GET",
url: this.url + 'ZAutomation/api/v1/devices'
}).then(function(result){
this.lastUpdate = result.data.updateTime;
var devices = result.data.devices;
var groupedDevices = {};
for(var i = 0; i < devices.length; i++){
var vdev = devices[i];
if(vdev.tags.indexOf("Homebridge:Skip") >= 0) { debug("Tag says skip!"); continue; }
var gdid = vdev.id.replace(/^(.*?)_zway_(\d+-\d+)-\d.*/, '$1_$2');
var gd = groupedDevices[gdid] || (groupedDevices[gdid] = {devices: [], types: {}, primary: undefined});
gd.devices.push(vdev);
gd.types[ZWayServerPlatform.getVDevTypeKey(vdev)] = gd.devices.length - 1;
gd.types[vdev.deviceType] = gd.devices.length - 1; // also include the deviceType only as a possibility
}
//TODO: Make a second pass, re-splitting any devices that don't make sense together
for(var gdid in groupedDevices) {
if(!groupedDevices.hasOwnProperty(gdid)) continue;
// Debug/log...
debug('Got grouped device ' + gdid + ' consiting of devices:');
var gd = groupedDevices[gdid];
for(var j = 0; j < gd.devices.length; j++){
debug(gd.devices[j].id + " - " + gd.devices[j].deviceType + (gd.devices[j].metrics && gd.devices[j].metrics.probeTitle ? "." + gd.devices[j].metrics.probeTitle : ""));
}
var accessory = null;
for(var ti = 0; ti < primaryDeviceClasses.length; ti++){
if(gd.types[primaryDeviceClasses[ti]] !== undefined){
gd.primary = gd.types[primaryDeviceClasses[ti]];
var pd = gd.devices[gd.primary];
var name = pd.metrics && pd.metrics.title ? pd.metrics.title : pd.id;
debug("Using primary device with type " + primaryDeviceClasses[ti] + ", " + name + " (" + pd.id + ") as primary.");
accessory = new ZWayServerAccessory(name, gd, that);
break;
}
}
if(!accessory)
debug("WARN: Didn't find suitable device class!");
else
foundAccessories.push(accessory);
}
//foundAccessories = foundAccessories.slice(0, 10); // Limit to a few devices for testing...
callback(foundAccessories);
// Start the polling process...
this.pollingTimer = setTimeout(this.pollUpdate.bind(this), this.pollInterval*1000);
}.bind(this));
}
,
pollUpdate: function(){
//debug("Polling for updates since " + this.lastUpdate + "...");
return this.zwayRequest({
method: "GET",
url: this.url + 'ZAutomation/api/v1/devices',
qs: {since: this.lastUpdate}
}).then(function(result){
this.lastUpdate = result.data.updateTime;
if(result.data && result.data.devices && result.data.devices.length){
var updates = result.data.devices;
debug("Got " + updates.length + " updates.");
for(var i = 0; i < updates.length; i++){
var upd = updates[i];
if(this.cxVDevMap[upd.id]){
var vdev = this.vDevStore[upd.id];
vdev.metrics.level = upd.metrics.level;
vdev.updateTime = upd.updateTime;
var cxs = this.cxVDevMap[upd.id];
for(var j = 0; j < cxs.length; j++){
var cx = cxs[j];
if(typeof cx.zway_getValueFromVDev !== "function") continue;
var oldValue = cx.value;
var newValue = cx.zway_getValueFromVDev(vdev);
if(oldValue !== newValue){
cx.value = newValue;
cx.emit('change', { oldValue:oldValue, newValue:cx.value, context:null });
debug("Updated characteristic " + cx.displayName + " on " + vdev.metrics.title);
}
}
}
}
}
// setup next poll...
this.pollingTimer = setTimeout(this.pollUpdate.bind(this), this.pollInterval*1000);
}.bind(this));
}
}
function ZWayServerAccessory(name, devDesc, platform) {
// device info
this.name = name;
this.devDesc = devDesc;
this.platform = platform;
this.log = platform.log;
}
ZWayServerAccessory.prototype = {
getVDev: function(vdev){
return this.platform.zwayRequest({
method: "GET",
url: this.platform.url + 'ZAutomation/api/v1/devices/' + vdev.id
})//.then(function());
}
,
command: function(vdev, command, value) {
return this.platform.zwayRequest({
method: "GET",
url: this.platform.url + 'ZAutomation/api/v1/devices/' + vdev.id + '/command/' + command,
qs: (value === undefined ? undefined : value)
});
},
getVDevServices: function(vdev){
var typeKey = ZWayServerPlatform.getVDevTypeKey(vdev);
var services = [], service;
switch (typeKey) {
case "switchBinary":
services.push(new Service.Switch(vdev.metrics.title));
break;
case "switchMultilevel":
services.push(new Service.Lightbulb(vdev.metrics.title));
break;
case "thermostat":
services.push(new Service.Thermostat(vdev.metrics.title));
break;
case "sensorMultilevel.Temperature":
services.push(new Service.TemperatureSensor(vdev.metrics.title));
break;
case "sensorBinary.Door/Window":
services.push(new Service.GarageDoorOpener(vdev.metrics.title));
break;
case "battery.Battery":
services.push(new Service.BatteryService(vdev.metrics.title));
break;
case "sensorMultilevel.Luminiscence":
services.push(new Service.LightSensor(vdev.metrics.title));
break;
}
var validServices =[];
for(var i = 0; i < services.length; i++){
if(this.configureService(services[i], vdev))
validServices.push(services[i]);
}
return validServices;
}
,
uuidToTypeKeyMap: null
,
extraCharacteristicsMap: {
"battery.Battery": [Characteristic.BatteryLevel, Characteristic.StatusLowBattery],
"sensorMultilevel.Temperature": [Characteristic.CurrentTemperature, Characteristic.TemperatureDisplayUnits],
"sensorMultilevel.Luminiscence": [Characteristic.CurrentAmbientLightLevel]
}
,
getVDevForCharacteristic: function(cx, vdevPreferred){
var map = this.uuidToTypeKeyMap;
if(!map){
this.uuidToTypeKeyMap = map = {};
map[(new Characteristic.On).UUID] = ["switchBinary","switchMultilevel"];
map[(new Characteristic.Brightness).UUID] = ["switchMultilevel"];
map[(new Characteristic.CurrentTemperature).UUID] = ["sensorMultilevel.Temperature","thermostat"];
map[(new Characteristic.TargetTemperature).UUID] = ["thermostat"];
map[(new Characteristic.TemperatureDisplayUnits).UUID] = ["sensorMultilevel.Temperature","thermostat"]; //TODO: Always a fixed result
map[(new Characteristic.CurrentHeatingCoolingState).UUID] = ["thermostat"]; //TODO: Always a fixed result
map[(new Characteristic.TargetHeatingCoolingState).UUID] = ["thermostat"]; //TODO: Always a fixed result
map[(new Characteristic.CurrentDoorState).UUID] = ["sensorBinary.Door/Window","sensorBinary"];
map[(new Characteristic.TargetDoorState).UUID] = ["sensorBinary.Door/Window","sensorBinary"]; //TODO: Always a fixed result
map[(new Characteristic.ObstructionDetected).UUID] = ["sensorBinary.Door/Window","sensorBinary"]; //TODO: Always a fixed result
map[(new Characteristic.BatteryLevel).UUID] = ["battery.Battery"];
map[(new Characteristic.StatusLowBattery).UUID] = ["battery.Battery"];
map[(new Characteristic.ChargingState).UUID] = ["battery.Battery"]; //TODO: Always a fixed result
map[(new Characteristic.CurrentAmbientLightLevel).UUID] = ["sensorMultilevel.Luminiscence"];
}
if(cx instanceof Characteristic.Name) return vdevPreferred;
// Special case!: If cx is a CurrentTemperature, ignore the preferred device...we want the sensor if available!
if(cx instanceof Characteristic.CurrentTemperature) vdevPreferred = null;
//
var typekeys = map[cx.UUID];
if(typekeys === undefined) return null;
if(vdevPreferred && typekeys.indexOf(ZWayServerPlatform.getVDevTypeKey(vdevPreferred)) >= 0){
return vdevPreferred;
}
var candidates = this.devDesc.devices;
for(var i = 0; i < typekeys.length; i++){
for(var j = 0; j < candidates.length; j++){
if(ZWayServerPlatform.getVDevTypeKey(candidates[j]) === typekeys[i]) return candidates[j];
}
}
return null;
}
,
configureCharacteristic: function(cx, vdev){
var that = this;
// Add this combination to the maps...
if(!this.platform.cxVDevMap[vdev.id]) this.platform.cxVDevMap[vdev.id] = [];
this.platform.cxVDevMap[vdev.id].push(cx);
if(!this.platform.vDevStore[vdev.id]) this.platform.vDevStore[vdev.id] = vdev;
if(cx instanceof Characteristic.Name){
cx.zway_getValueFromVDev = function(vdev){
return vdev.metrics.title;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
callback(false, that.name);
});
cx.writable = false;
return cx;
}
if(cx instanceof Characteristic.On){
cx.zway_getValueFromVDev = function(vdev){
var val = false;
if(vdev.metrics.level === "on"){
val = true;
} else if(vdev.metrics.level <= 5) {
val = false;
} else if (vdev.metrics.level > 5) {
val = true;
}
return val;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
this.getVDev(vdev).then(function(result){
debug("Got value: " + cx.zway_getValueFromVDev(result.data) + ", for " + vdev.metrics.title + ".");
callback(false, cx.zway_getValueFromVDev(result.data));
});
}.bind(this));
cx.on('set', function(powerOn, callback){
this.command(vdev, powerOn ? "on" : "off").then(function(result){
callback();
});
}.bind(this));
cx.on('change', function(ev){
debug("Device " + vdev.metrics.title + ", characteristic " + cx.displayName + " changed from " + ev.oldValue + " to " + ev.newValue);
});
return cx;
}
if(cx instanceof Characteristic.Brightness){
cx.zway_getValueFromVDev = function(vdev){
return vdev.metrics.level;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
this.getVDev(vdev).then(function(result){
debug("Got value: " + cx.zway_getValueFromVDev(result.data) + ", for " + vdev.metrics.title + ".");
callback(false, cx.zway_getValueFromVDev(result.data));
});
}.bind(this));
cx.on('set', function(level, callback){
this.command(vdev, "exact", {level: parseInt(level, 10)}).then(function(result){
callback();
});
}.bind(this));
return cx;
}
if(cx instanceof Characteristic.CurrentTemperature){
cx.zway_getValueFromVDev = function(vdev){
return vdev.metrics.level;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
this.getVDev(vdev).then(function(result){
debug("Got value: " + cx.zway_getValueFromVDev(result.data) + ", for " + vdev.metrics.title + ".");
callback(false, cx.zway_getValueFromVDev(result.data));
});
}.bind(this));
cx.minimumValue = vdev.metrics && vdev.metrics.min !== undefined ? vdev.metrics.min : -40;
cx.maximumValue = vdev.metrics && vdev.metrics.max !== undefined ? vdev.metrics.max : 999;
return cx;
}
if(cx instanceof Characteristic.TargetTemperature){
cx.zway_getValueFromVDev = function(vdev){
return vdev.metrics.level;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
this.getVDev(vdev).then(function(result){
debug("Got value: " + cx.zway_getValueFromVDev(result.data) + ", for " + vdev.metrics.title + ".");
callback(false, cx.zway_getValueFromVDev(result.data));
});
}.bind(this));
cx.on('set', function(level, callback){
this.command(vdev, "exact", {level: parseInt(level, 10)}).then(function(result){
//debug("Got value: " + result.data.metrics.level + ", for " + vdev.metrics.title + ".");
callback();
});
}.bind(this));
cx.minimumValue = vdev.metrics && vdev.metrics.min !== undefined ? vdev.metrics.min : 5;
cx.maximumValue = vdev.metrics && vdev.metrics.max !== undefined ? vdev.metrics.max : 40;
return cx;
}
if(cx instanceof Characteristic.TemperatureDisplayUnits){
//TODO: Always in °C for now.
cx.zway_getValueFromVDev = function(vdev){
return Characteristic.TemperatureDisplayUnits.CELSIUS;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
callback(false, Characteristic.TemperatureDisplayUnits.CELSIUS);
});
cx.writable = false;
return cx;
}
if(cx instanceof Characteristic.CurrentHeatingCoolingState){
//TODO: Always HEAT for now, we don't have an example to work with that supports another function.
cx.zway_getValueFromVDev = function(vdev){
return Characteristic.CurrentHeatingCoolingState.HEAT;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
callback(false, Characteristic.CurrentHeatingCoolingState.HEAT);
});
return cx;
}
if(cx instanceof Characteristic.TargetHeatingCoolingState){
//TODO: Always HEAT for now, we don't have an example to work with that supports another function.
cx.zway_getValueFromVDev = function(vdev){
return Characteristic.TargetHeatingCoolingState.HEAT;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
callback(false, Characteristic.TargetHeatingCoolingState.HEAT);
});
cx.writable = false;
return cx;
}
if(cx instanceof Characteristic.CurrentDoorState){
cx.zway_getValueFromVDev = function(vdev){
return vdev.metrics.level === "off" ? Characteristic.CurrentDoorState.CLOSED : Characteristic.CurrentDoorState.OPEN;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
this.getVDev(vdev).then(function(result){
debug("Got value: " + cx.zway_getValueFromVDev(result.data) + ", for " + vdev.metrics.title + ".");
callback(false, cx.zway_getValueFromVDev(result.data));
});
}.bind(this));
cx.on('change', function(ev){
debug("Device " + vdev.metrics.title + ", characteristic " + cx.displayName + " changed from " + ev.oldValue + " to " + ev.newValue);
});
}
if(cx instanceof Characteristic.TargetDoorState){
//TODO: We only support this for Door sensors now, so it's a fixed value.
cx.zway_getValueFromVDev = function(vdev){
return Characteristic.TargetDoorState.CLOSED;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
callback(false, Characteristic.TargetDoorState.CLOSED);
});
//cx.readable = false;
cx.writable = false;
}
if(cx instanceof Characteristic.ObstructionDetected){
//TODO: We only support this for Door sensors now, so it's a fixed value.
cx.zway_getValueFromVDev = function(vdev){
return false;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
callback(false, false);
});
//cx.readable = false;
cx.writable = false;
}
if(cx instanceof Characteristic.BatteryLevel){
cx.zway_getValueFromVDev = function(vdev){
return vdev.metrics.level;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
this.getVDev(vdev).then(function(result){
debug("Got value: " + cx.zway_getValueFromVDev(result.data) + ", for " + vdev.metrics.title + ".");
callback(false, cx.zway_getValueFromVDev(result.data));
});
}.bind(this));
}
if(cx instanceof Characteristic.StatusLowBattery){
cx.zway_getValueFromVDev = function(vdev){
return vdev.metrics.level <= that.platform.batteryLow ? Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
this.getVDev(vdev).then(function(result){
debug("Got value: " + cx.zway_getValueFromVDev(result.data) + ", for " + vdev.metrics.title + ".");
callback(false, cx.zway_getValueFromVDev(result.data));
});
}.bind(this));
}
if(cx instanceof Characteristic.ChargingState){
//TODO: No known chargeable devices(?), so always return false.
cx.zway_getValueFromVDev = function(vdev){
return Characteristic.ChargingState.NOT_CHARGING;
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
callback(false, Characteristic.ChargingState.NOT_CHARGING);
});
//cx.readable = false;
cx.writable = false;
}
if(cx instanceof Characteristic.CurrentAmbientLightLevel){
cx.zway_getValueFromVDev = function(vdev){
if(vdev.metrics.scaleTitle === "%"){
// Completely unscientific guess, based on test-fit data and Wikipedia real-world lux values.
// This will probably change!
var lux = 0.0005 * (vdev.metrics.level^3.6);
if(lux < cx.minimumValue) return cx.minimumValue;
if(lux > cx.maximumValue) return cx.maximumValue;
return lux;
} else {
return vdev.metrics.level;
}
};
cx.value = cx.zway_getValueFromVDev(vdev);
cx.on('get', function(callback, context){
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
this.getVDev(vdev).then(function(result){
debug("Got value: " + cx.zway_getValueFromVDev(result.data) + ", for " + vdev.metrics.title + ".");
callback(false, cx.zway_getValueFromVDev(result.data));
});
}.bind(this));
cx.on('change', function(ev){
debug("Device " + vdev.metrics.title + ", characteristic " + cx.displayName + " changed from " + ev.oldValue + " to " + ev.newValue);
});
return cx;
}
}
,
configureService: function(service, vdev){
var success = true;
for(var i = 0; i < service.characteristics.length; i++){
var cx = service.characteristics[i];
var vdev = this.getVDevForCharacteristic(cx, vdev);
if(!vdev){
success = false;
debug("ERROR! Failed to configure required characteristic \"" + service.characteristics[i].displayName + "\"!");
}
cx = this.configureCharacteristic(cx, vdev);
}
for(var i = 0; i < service.optionalCharacteristics.length; i++){
var cx = service.optionalCharacteristics[i];
var vdev = this.getVDevForCharacteristic(cx);
if(!vdev) continue;
cx = this.configureCharacteristic(cx, vdev);
if(cx) service.addCharacteristic(cx);
}
return success;
}
,
getServices: function() {
var that = this;
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, "Z-Wave.me")
.setCharacteristic(Characteristic.Model, "Virtual Device (VDev version 1)")
.setCharacteristic(Characteristic.SerialNumber, "VDev-" + this.devDesc.devices[this.devDesc.primary].h) //FIXME: Is this valid?);
var services = [informationService];
services = services.concat(this.getVDevServices(this.devDesc.devices[this.devDesc.primary]));
if(this.platform.splitServices){
if(this.devDesc.types["battery.Battery"]){
services = services.concat(this.getVDevServices(this.devDesc.devices[this.devDesc.types["battery.Battery"]]));
}
// Odds and ends...if there are sensors that haven't been used, add services for them...
var tempSensor = this.devDesc.types["sensorMultilevel.Temperature"] !== undefined ? this.devDesc.devices[this.devDesc.types["sensorMultilevel.Temperature"]] : false;
if(tempSensor && !this.platform.cxVDevMap[tempSensor.id]){
services = services.concat(this.getVDevServices(tempSensor));
}
var lightSensor = this.devDesc.types["sensorMultilevel.Luminiscence"] !== undefined ? this.devDesc.devices[this.devDesc.types["sensorMultilevel.Luminiscence"]] : false;
if(lightSensor && !this.platform.cxVDevMap[lightSensor.id]){
services = services.concat(this.getVDevServices(lightSensor));
}
} else {
// Everything outside the primary service gets added as optional characteristics...
var service = services[1];
var existingCxUUIDs = {};
for(var i = 0; i < service.characteristics.length; i++) existingCxUUIDs[service.characteristics[i].UUID] = true;
for(var i = 0; i < this.devDesc.devices.length; i++){
var vdev = this.devDesc.devices[i];
if(this.platform.cxVDevMap[vdev.id]) continue; // Don't double-use anything
var extraCxClasses = this.extraCharacteristicsMap[ZWayServerPlatform.getVDevTypeKey(vdev)];
var extraCxs = [];
if(!extraCxClasses || extraCxClasses.length === 0) continue;
for(var j = 0; j < extraCxClasses.length; j++){
var cx = new extraCxClasses[j]();
if(existingCxUUIDs[cx.UUID]) continue; // Don't have two of the same Characteristic type in one service!
var vdev2 = this.getVDevForCharacteristic(cx, vdev); // Just in case...will probably return vdev.
if(!vdev2){
// Uh oh... one of the extraCxClasses can't be configured! Abort all extras for this vdev!
extraCxs = []; // to wipe out any already setup cxs.
break;
}
this.configureCharacteristic(cx, vdev2);
extraCxs.push(cx);
}
for(var j = 0; j < extraCxs.length; j++)
service.addCharacteristic(extraCxs[j]);
}
}
debug("Loaded services for " + this.name);
return services;
}
};
module.exports.accessory = ZWayServerAccessory;
module.exports.platform = ZWayServerPlatform;