Merge branch 'nfarina/master' into z-way-server-2

This commit is contained in:
S'pht'Kr
2015-09-06 06:37:31 +02:00
6 changed files with 253 additions and 443 deletions

View File

@@ -1,166 +1,128 @@
var types = require("HAP-NodeJS/accessories/types.js"); var Service = require("HAP-NodeJS").Service;
var Characteristic = require("HAP-NodeJS").Characteristic;
var wemo = require('wemo'); var wemo = require('wemo');
// extend our search timeout from 5 seconds to 60 module.exports = {
wemo.SearchTimeout = 60000; accessory: WeMoAccessory
wemo.timeout = wemo.SearchTimeout // workaround for a bug in wemo.js v0.0.4 }
function WeMoAccessory(log, config) { function WeMoAccessory(log, config) {
this.log = log; this.log = log;
this.name = config["name"]; this.name = config["name"];
this.wemoName = config["wemo_name"]; this.service = config["service"] || "Switch";
this.device = null; this.wemoName = config["wemo_name"] || this.name; // fallback to "name" if you didn't specify an exact "wemo_name"
this.device = null; // instance of WeMo, for controlling the discovered device
this.log("Searching for WeMo device with exact name '" + this.wemoName + "'..."); this.log("Searching for WeMo device with exact name '" + this.wemoName + "'...");
this.search(); this.search();
} }
WeMoAccessory.prototype = { WeMoAccessory.prototype.search = function() {
wemo.Search(this.wemoName, function(err, device) {
search: function() { if (!err && device) {
var that = this; this.log("Found '"+this.wemoName+"' device at " + device.ip);
this.device = new wemo(device.ip, device.port);
wemo.Search(this.wemoName, function(err, device) {
if (!err && device) {
that.log("Found '"+that.wemoName+"' device at " + device.ip);
that.device = new wemo(device.ip, device.port);
}
else {
that.log("Error finding device '" + that.wemoName + "': " + err);
that.log("Continuing search for WeMo device with exact name '" + that.wemoName + "'...");
that.search();
}
});
},
setPowerState: function(powerOn) {
if (!this.device) {
this.log("No '"+this.wemoName+"' device found (yet?)");
return;
} }
else {
var binaryState = powerOn ? 1 : 0; this.log("Error finding device '" + this.wemoName + "': " + err);
var that = this; this.log("Continuing search for WeMo device with exact name '" + this.wemoName + "'...");
this.search();
this.log("Setting power state on the '"+this.wemoName+"' to " + binaryState);
this.device.setBinaryState(binaryState, function(err, result) {
if (!err) {
that.log("Successfully set power state on the '"+that.wemoName+"' to " + binaryState);
}
else {
that.log("Error setting power state on the '"+that.wemoName+"'")
}
});
},
getPowerState: function(callback) {
if (!this.device) {
this.log("No '"+this.wemoName+"' device found (yet?)");
return;
} }
}.bind(this));
}
var that = this; WeMoAccessory.prototype.getPowerOn = function(callback) {
this.log("checking power state for: " + this.wemoName); if (!this.device) {
this.device.getBinaryState(function(err, result) { this.log("No '%s' device found (yet?)", this.wemoName);
if (!err) { callback(new Error("Device not found"), false);
var binaryState = parseInt(result) return;
that.log("power state for " + that.wemoName + " is: " + binaryState)
callback(binaryState > 0 ? 1 : 0);
}
else {
that.log(err)
}
});
},
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: "WeMo",
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.SWITCH_STYPE,
characteristics: [{
cType: types.NAME_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: this.name,
supportEvents: false,
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: false,
supportEvents: false,
supportBonjour: false,
manfDescription: "Change the power state of the WeMo",
designedMaxLength: 1
}]
}];
} }
};
module.exports.accessory = WeMoAccessory; this.log("Getting power state on the '%s'...", this.wemoName);
this.device.getBinaryState(function(err, result) {
if (!err) {
var binaryState = parseInt(result);
var powerOn = binaryState > 0;
this.log("Power state for the '%s' is %s", this.wemoName, binaryState);
callback(null, powerOn);
}
else {
this.log("Error getting power state on the '%s': %s", this.wemoName, err.message);
callback(err);
}
}.bind(this));
}
WeMoAccessory.prototype.setPowerOn = function(powerOn, callback) {
if (!this.device) {
this.log("No '%s' device found (yet?)", this.wemoName);
callback(new Error("Device not found"));
return;
}
var binaryState = powerOn ? 1 : 0; // wemo langauge
this.log("Setting power state on the '%s' to %s", this.wemoName, binaryState);
this.device.setBinaryState(binaryState, function(err, result) {
if (!err) {
this.log("Successfully set power state on the '%s' to %s", this.wemoName, binaryState);
callback(null);
}
else {
this.log("Error setting power state to %s on the '%s'", binaryState, this.wemoName);
callback(err);
}
}.bind(this));
}
WeMoAccessory.prototype.setTargetDoorState = function(targetDoorState, callback) {
if (!this.device) {
this.log("No '%s' device found (yet?)", this.wemoName);
callback(new Error("Device not found"));
return;
}
this.log("Activating WeMo switch '%s'", this.wemoName);
this.device.setBinaryState(1, function(err, result) {
if (!err) {
this.log("Successfully activated WeMo switch '%s'", this.wemoName);
callback(null);
}
else {
this.log("Error activating WeMo switch '%s'", this.wemoName);
callback(err);
}
}.bind(this));
}
WeMoAccessory.prototype.getServices = function() {
if (this.service == "Switch") {
var switchService = new Service.Switch(this.name);
switchService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerOn.bind(this))
.on('set', this.setPowerOn.bind(this));
return [switchService];
}
else if (this.service == "GarageDoor") {
var garageDoorService = new Service.GarageDoorOpener("Garage Door Opener");
garageDoorService
.getCharacteristic(Characteristic.TargetDoorState)
.on('set', this.setTargetDoorState.bind(this))
.supportsEventNotification = false;
return [garageDoorService];
}
else {
throw new Error("Unknown service type '%s'", this.service);
}
}

View File

@@ -1,284 +0,0 @@
var types = require("HAP-NodeJS/accessories/types.js");
var request = require("request");
var xmldoc = require("xmldoc");
function XfinityHomeAccessory(log, config) {
this.log = log;
this.name = config["name"];
this.email = config["email"];
this.password = config["password"];
this.dsig = config["dsig"];
this.pinCode = config["pin"];
}
XfinityHomeAccessory.prototype = {
armWithType: function(armed, type) {
this.log("Arming with type " + type + " = " + armed + "...");
this.targetArmed = armed;
this.targetArmType = type;
this.getLoginToken();
},
getLoginToken: function() {
this.log("Retrieving login token...");
var that = this;
request.post({
url: "https://login.comcast.net/api/login",
form: {
appkey:"iControl",
dsig: this.dsig,
u: this.email,
p: this.password
}
}, function(err, response, body) {
if (!err && response.statusCode == 200) {
var doc = new xmldoc.XmlDocument(body);
that.loginToken = doc.valueWithPath("LoginToken");
that.refreshLoginCookie();
}
else {
that.log("Error '"+err+"' getting login token: " + body);
}
});
},
refreshLoginCookie: function() {
this.log("Refreshing login cookie...");
var that = this;
request.post({
url: "https://www.xfinityhomesecurity.com/rest/icontrol/login",
form: {
token: this.loginToken
}
}, function(err, response, body) {
if (!err && response.statusCode == 200) {
// extract our "site" from the login response
var json = JSON.parse(body);
that.siteHref = json["login"]["site"]["href"];
// manual cookie handling
that.loginCookie = response.headers["set-cookie"];
that.getInstances();
}
else {
that.log("Error '"+err+"' refreshing login cookie: " + body);
}
});
},
getInstances: function() {
this.log("Getting instances for site " + this.siteHref + "...");
this.panelHref = null;
var that = this;
request.get({
url: "https://www.xfinityhomesecurity.com/"+that.siteHref+"/network/instances",
headers: { Cookie: this.loginCookie },
json: true
}, function(err, response, json) {
if (!err && response.statusCode == 200) {
// extract our "instance" from the response. look for the first "panel"
var instances = json["instances"]["instance"];
for (var i=0; i<instances.length; i++) {
var instance = instances[i];
if (instance["mediaType"] == "instance/panel") {
that.panelHref = instance.href;
}
}
if (that.panelHref) {
that.log("Found panel " + that.panelHref + ". Ready to arm.");
that.finishArm();
}
else {
that.log("Couldn't find a panel.");
}
}
else {
that.log("Error '"+err+"' getting instances: " + JSON.stringify(json));
}
});
},
finishArm: function() {
this.log("Finish arming with type " + this.targetArmType + " = " + this.targetArmed + "...");
var path, form;
var that = this;
if (!this.targetArmed) {
path = this.panelHref + "/functions/disarm";
form = {code: this.pinCode};
}
else {
path = this.panelHref + "/functions/arm";
form = {code: this.pinCode, armType: this.targetArmType };
}
request.post({
url: "https://www.xfinityhomesecurity.com"+path,
headers: { Cookie: this.loginCookie },
form: form
}, function(err, response, body) {
if (!err && response.statusCode >= 200 && response.statusCode < 300) {
that.log("Arm response: " + response);
}
else {
that.log("Error '"+err+"' performing arm request: " + body);
}
});
},
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: "Comcast",
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.SWITCH_STYPE,
characteristics: [{
cType: types.NAME_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Away Mode",
supportEvents: false,
supportBonjour: false,
manfDescription: "Away Mode service",
designedMaxLength: 255
},{
cType: types.POWER_STATE_CTYPE,
onUpdate: function(value) { that.armWithType(value, "away"); },
perms: ["pw","pr","ev"],
format: "bool",
initialValue: false,
supportEvents: false,
supportBonjour: false,
manfDescription: "Turn on the Away alarm",
designedMaxLength: 1
}]
},{
sType: types.SWITCH_STYPE,
characteristics: [{
cType: types.NAME_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Night Mode",
supportEvents: false,
supportBonjour: false,
manfDescription: "Night Mode service",
designedMaxLength: 255
},{
cType: types.POWER_STATE_CTYPE,
onUpdate: function(value) { that.armWithType(value, "night"); },
perms: ["pw","pr","ev"],
format: "bool",
initialValue: false,
supportEvents: false,
supportBonjour: false,
manfDescription: "Turn on the Night alarm",
designedMaxLength: 1
}]
},{
sType: types.SWITCH_STYPE,
characteristics: [{
cType: types.NAME_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Stay Mode",
supportEvents: false,
supportBonjour: false,
manfDescription: "Stay Mode service",
designedMaxLength: 255
},{
cType: types.POWER_STATE_CTYPE,
onUpdate: function(value) { that.armWithType(value, "stay"); },
perms: ["pw","pr","ev"],
format: "bool",
initialValue: false,
supportEvents: false,
supportBonjour: false,
manfDescription: "Turn on the Stay alarm",
designedMaxLength: 1
}]
}];
}
};
// Enable cookie handling and append our expected headers
request = request.defaults({
headers: {
"X-appkey": "comcastTokenKey",
"X-ClientInfo": "5.2.51",
"X-format": "json"
}
});
module.exports.accessory = XfinityHomeAccessory;

130
accessories/iControl.js Normal file
View File

@@ -0,0 +1,130 @@
var iControl = require('node-icontrol').iControl;
var Service = require('HAP-NodeJS').Service;
var Characteristic = require('HAP-NodeJS').Characteristic;
module.exports = {
accessory: iControlAccessory
}
/**
* Provides a Security System accessory for an iControl-based security system like Xfinity Home.
*/
function iControlAccessory(log, config) {
this.log = log;
this.iControl = new iControl({
system: iControl.Systems[config.system],
email: config.email,
password: config.password,
pinCode: config.pin
});
this.iControl.on('change', this._handleChange.bind(this));
this.iControl.on('error', this._handleError.bind(this));
this.log("Logging into iControl...");
this.iControl.login();
this._securitySystem = new Service.SecuritySystem("Security System");
this._securitySystem
.getCharacteristic(Characteristic.SecuritySystemTargetState)
.on('get', this._getTargetState.bind(this))
.on('set', this._setTargetState.bind(this));
this._securitySystem
.getCharacteristic(Characteristic.SecuritySystemCurrentState)
.on('get', this._getCurrentState.bind(this));
}
iControlAccessory.prototype._getTargetState = function(callback) {
this.iControl.getArmState(function(err, armState) {
if (err) return callback(err);
var currentState = this._getHomeKitStateFromArmState(armState);
callback(null, currentState);
}.bind(this));
}
iControlAccessory.prototype._getCurrentState = function(callback) {
this.iControl.getArmState(function(err, armState) {
if (err) return callback(err);
var currentState = this._getHomeKitStateFromArmState(armState);
callback(null, currentState);
}.bind(this));
}
iControlAccessory.prototype._setTargetState = function(targetState, callback, context) {
if (context == "internal") return callback(null); // we set this state ourself, no need to react to it
var armState = this._getArmStateFromHomeKitState(targetState);
this.log("Setting target state to %s", armState);
this.iControl.setArmState(armState, function(err) {
if (err) return callback(err);
this.log("Successfully set target state to %s", armState);
// also update current state
this._securitySystem
.getCharacteristic(Characteristic.SecuritySystemCurrentState)
.setValue(targetState);
callback(null); // success!
}.bind(this));
}
iControlAccessory.prototype._handleChange = function(armState) {
this.log("Arm state changed to %s", armState);
var homeKitState = this._getHomeKitStateFromArmState(armState);
this._securitySystem
.getCharacteristic(Characteristic.SecuritySystemCurrentState)
.setValue(homeKitState);
this._securitySystem
.getCharacteristic(Characteristic.SecuritySystemTargetState)
.setValue(homeKitState, null, "internal"); // these characteristics happen to share underlying values
}
iControlAccessory.prototype._handleError = function(err) {
this.log(err.message);
}
iControlAccessory.prototype.getServices = function() {
return [this._securitySystem];
}
iControlAccessory.prototype._getHomeKitStateFromArmState = function(armState) {
switch (armState) {
case "disarmed": return Characteristic.SecuritySystemCurrentState.DISARMED;
case "away": return Characteristic.SecuritySystemCurrentState.AWAY_ARM;
case "night": return Characteristic.SecuritySystemCurrentState.NIGHT_ARM;
case "stay": return Characteristic.SecuritySystemCurrentState.STAY_ARM;
}
}
iControlAccessory.prototype._getArmStateFromHomeKitState = function(homeKitState) {
switch (homeKitState) {
case Characteristic.SecuritySystemCurrentState.DISARMED: return "disarmed";
case Characteristic.SecuritySystemCurrentState.AWAY_ARM: return "away";
case Characteristic.SecuritySystemCurrentState.NIGHT_ARM: return "night";
case Characteristic.SecuritySystemCurrentState.STAY_ARM: return "stay";
}
}
/**
* TESTING
*/
if (require.main === module) {
var config = JSON.parse(require('fs').readFileSync("config.json")).accessories[0];
var accessory = new iControlAccessory(console.log, config);
}

5
app.js
View File

@@ -8,6 +8,7 @@ var Accessory = require('HAP-NodeJS').Accessory;
var Service = require('HAP-NodeJS').Service; var Service = require('HAP-NodeJS').Service;
var Characteristic = require('HAP-NodeJS').Characteristic; var Characteristic = require('HAP-NodeJS').Characteristic;
var accessoryLoader = require('HAP-NodeJS').AccessoryLoader; var accessoryLoader = require('HAP-NodeJS').AccessoryLoader;
var once = require('HAP-NodeJS/lib/util/once').once;
console.log("Starting HomeBridge server..."); console.log("Starting HomeBridge server...");
@@ -118,7 +119,7 @@ function loadPlatforms() {
function loadPlatformAccessories(platformInstance, log) { function loadPlatformAccessories(platformInstance, log) {
asyncCalls++; asyncCalls++;
platformInstance.accessories(function(foundAccessories){ platformInstance.accessories(once(function(foundAccessories){
asyncCalls--; asyncCalls--;
// loop through accessories adding them to the list and registering them // loop through accessories adding them to the list and registering them
@@ -137,7 +138,7 @@ function loadPlatformAccessories(platformInstance, log) {
// were we the last callback? // were we the last callback?
if (asyncCalls === 0 && !asyncWait) if (asyncCalls === 0 && !asyncWait)
publish(); publish();
}); }));
} }
function createAccessory(accessoryInstance, displayName) { function createAccessory(accessoryInstance, displayName) {

View File

@@ -104,15 +104,15 @@
"password" : "your-carwings-password" "password" : "your-carwings-password"
}, },
{ {
"accessory": "XfinityHome", "accessory": "iControl",
"name": "Xfinity Home", "name": "Xfinity Home",
"description": "This shim supports the 'Xfinity Home' security system. Unfortunately I don't know how to generate the 'dsig' property, so you'll need to figure yours out by running the Xfinity Home app on your iOS device while connected to a proxy server like Charles. If you didn't understand any of that, sorry! I welcome any suggestions for how to figure out dsig automatically.", "description": "This shim supports iControl-based security systems like Xfinity Home.",
"email": "your-comcast-email@example.com", "system": "XFINITY_HOME",
"email": "your-comcast-email",
"password": "your-comcast-password", "password": "your-comcast-password",
"dsig": "your-digital-signature",
"pin": "your-security-system-pin-code" "pin": "your-security-system-pin-code"
}, },
{ {
"accessory": "HomeMatic", "accessory": "HomeMatic",
"name": "Light", "name": "Light",
"description": "Control HomeMatic devices (The XMP-API addon for the CCU is required)", "description": "Control HomeMatic devices (The XMP-API addon for the CCU is required)",

View File

@@ -15,11 +15,12 @@
"carwingsjs": "0.0.x", "carwingsjs": "0.0.x",
"color": "0.10.x", "color": "0.10.x",
"elkington": "kevinohara80/elkington", "elkington": "kevinohara80/elkington",
"hap-nodejs": "git+https://github.com/KhaosT/HAP-NodeJS#fff863d7a387636fc612cf27cb859e82d9ee3294", "hap-nodejs": "git+https://github.com/KhaosT/HAP-NodeJS#6bf0f9eaaa2d87db8d1768114c61f4acbb095c41",
"harmonyhubjs-client": "^1.1.4", "harmonyhubjs-client": "^1.1.4",
"harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git", "harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git",
"mdns": "^2.2.4", "mdns": "^2.2.4",
"node-hue-api": "^1.0.5", "node-hue-api": "^1.0.5",
"node-icontrol": "^0.1.4",
"node-milight-promise": "0.0.x", "node-milight-promise": "0.0.x",
"node-persist": "0.0.x", "node-persist": "0.0.x",
"q": "1.4.x", "q": "1.4.x",
@@ -27,13 +28,13 @@
"request": "2.49.x", "request": "2.49.x",
"sonos": "0.8.x", "sonos": "0.8.x",
"telldus-live": "0.2.x", "telldus-live": "0.2.x",
"teslams": "1.0.1",
"unofficial-nest-api": "git+https://github.com/hachidorii/unofficial_nodejs_nest.git#d8d48edc952b049ff6320ef99afa7b2f04cdee98", "unofficial-nest-api": "git+https://github.com/hachidorii/unofficial_nodejs_nest.git#d8d48edc952b049ff6320ef99afa7b2f04cdee98",
"wemo": "0.2.x", "wemo": "0.2.x",
"wink-js": "0.0.5", "wink-js": "0.0.5",
"xml2js": "0.4.x", "xml2js": "0.4.x",
"xmldoc": "0.1.x", "xmldoc": "0.1.x",
"yamaha-nodejs": "0.4.x", "yamaha-nodejs": "0.4.x",
"teslams": "1.0.1",
"debug": "^2.2.0" "debug": "^2.2.0"
} }
} }