From 34110039f57c5fe81bd5f5de9044a388cd9decac Mon Sep 17 00:00:00 2001 From: Luke Redpath Date: Wed, 22 Jul 2015 15:27:52 +0100 Subject: [PATCH 1/3] Add SSL and basic auth support for Domoticz platform. --- platforms/Domoticz.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/platforms/Domoticz.js b/platforms/Domoticz.js index aacce99..0ba50cf 100644 --- a/platforms/Domoticz.js +++ b/platforms/Domoticz.js @@ -22,6 +22,14 @@ // } // ], // +// If your server uses HTTPS, you can specify "ssl": true in your config. If +// your server uses a self-signed certificate, you'll need to run the following +// before starting the server or you will get an error: +// +// export NODE_TLS_REJECT_UNAUTHORIZED=0 +// +// For basic auth support, specify the "user" and "password" in your config. +// // 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. // @@ -30,8 +38,11 @@ var request = require("request"); function DomoticzPlatform(log, config){ this.log = log; + this.user = config["user"]; + this.password = config["password"]; this.server = config["server"]; this.port = config["port"]; + this.protocol = config["ssl"] ? "https" : "http"; this.roomid = 0; if (typeof config["roomid"] != 'undefined') { this.roomid = config["roomid"]; @@ -46,15 +57,22 @@ function sortByKey(array, key) { } DomoticzPlatform.prototype = { + urlForQuery: function(query) { + var serverString = this.server; + if (this.user && this.password) { + serverString = this.user + ":" + this.password + "@" + serverString; + } + return this.protocol + "://" + serverString + ":" + this.port + "/json.htm?" + query; + }, + accessories: function(callback) { this.log("Fetching Domoticz lights and switches..."); - var that = this; var foundAccessories = []; if (this.roomid == 0) { //Get Lights request.get({ - url: "http://" + this.server + ":" + this.port + "/json.htm?type=devices&filter=light&used=true&order=Name", + url: this.urlForQuery("type=devices&filter=light&used=true&order=Name"), json: true }, function(err, response, json) { if (!err && response.statusCode == 200) { @@ -67,14 +85,14 @@ DomoticzPlatform.prototype = { } callback(foundAccessories); } else { - that.log("There was a problem connecting to Domoticz."); + that.log("There was a problem connecting to Domoticz. (" + err + ")"); } }); } else { //Get all devices specified in the room request.get({ - url: "http://" + this.server + ":" + this.port + "/json.htm?type=devices&plan=" + this.roomid, + url: this.urlForQuery("type=devices&plan=" + this.roomid), json: true }, function(err, response, json) { if (!err && response.statusCode == 200) { @@ -97,7 +115,7 @@ DomoticzPlatform.prototype = { //Get Scenes foundAccessories = []; request.get({ - url: "http://" + this.server + ":" + this.port + "/json.htm?type=scenes", + url: this.urlForQuery("type=scenes"), json: true }, function(err, response, json) { if (!err && response.statusCode == 200) { From 7b01e6c1d007adf16f21c4f24b2b39495e3a531c Mon Sep 17 00:00:00 2001 From: Luke Redpath Date: Wed, 22 Jul 2015 16:12:22 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Won=E2=80=99t=20be=20able=20to=20do=20much?= =?UTF-8?q?=20unless=20accessories=20can=20use=20SSL/basic=20auth=20too.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platforms/Domoticz.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/platforms/Domoticz.js b/platforms/Domoticz.js index 0ba50cf..a9e8e72 100644 --- a/platforms/Domoticz.js +++ b/platforms/Domoticz.js @@ -79,7 +79,7 @@ DomoticzPlatform.prototype = { if (json['result'] != undefined) { var sArray=sortByKey(json['result'],"Name"); sArray.map(function(s) { - accessory = new DomoticzAccessory(that.log, that.server, that.port, false, s.idx, s.Name, s.HaveDimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW")); + accessory = new DomoticzAccessory(that.log, that, false, s.idx, s.Name, s.HaveDimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW")); foundAccessories.push(accessory); }) } @@ -101,7 +101,7 @@ DomoticzPlatform.prototype = { sArray.map(function(s) { //only accept switches for now if (typeof s.SwitchType != 'undefined') { - accessory = new DomoticzAccessory(that.log, that.server, that.port, false, s.idx, s.Name, s.HaveDimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW")); + accessory = new DomoticzAccessory(that.log, that, false, s.idx, s.Name, s.HaveDimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW")); foundAccessories.push(accessory); } }) @@ -122,7 +122,7 @@ DomoticzPlatform.prototype = { if (json['result'] != undefined) { var sArray=sortByKey(json['result'],"Name"); sArray.map(function(s) { - accessory = new DomoticzAccessory(that.log, that.server, that.port, true, s.idx, s.Name, false, 0, false); + accessory = new DomoticzAccessory(that.log, that, true, s.idx, s.Name, false, 0, false); foundAccessories.push(accessory); }) } @@ -134,7 +134,7 @@ DomoticzPlatform.prototype = { } } -function DomoticzAccessory(log, server, port, IsScene, idx, name, HaveDimmer, MaxDimLevel, HaveRGB) { +function DomoticzAccessory(log, platform, IsScene, idx, name, HaveDimmer, MaxDimLevel, HaveRGB) { // device info this.IsScene = IsScene; this.idx = idx; @@ -143,8 +143,7 @@ function DomoticzAccessory(log, server, port, IsScene, idx, name, HaveDimmer, Ma this.MaxDimLevel = MaxDimLevel; this.HaveRGB = HaveRGB; this.log = log; - this.server = server; - this.port = port; + this.platform = platform; } DomoticzAccessory.prototype = { @@ -153,13 +152,13 @@ DomoticzAccessory.prototype = { if (this.IsScene == false) { //Lights if (c == "On" || c == "Off") { - url = "http://" + this.server + ":" + this.port + "/json.htm?type=command¶m=switchlight&idx=" + this.idx + "&switchcmd=" + c + "&level=0"; + url = this.platform.urlForQuery("type=command¶m=switchlight&idx=" + this.idx + "&switchcmd=" + c + "&level=0"); } else if (c == "setHue") { - url = "http://" + this.server + ":" + this.port + "/json.htm?type=command¶m=setcolbrightnessvalue&idx=" + this.idx + "&hue=" + value + "&brightness=100" + "&iswhite=false"; + url = this.platform.urlForQuery("type=command¶m=setcolbrightnessvalue&idx=" + this.idx + "&hue=" + value + "&brightness=100" + "&iswhite=false"); } else if (c == "setLevel") { - url = "http://" + this.server + ":" + this.port + "/json.htm?type=command¶m=switchlight&idx=" + this.idx + "&switchcmd=Set%20Level&level=" + value; + url = this.platform.urlForQuery("type=command¶m=switchlight&idx=" + this.idx + "&switchcmd=Set%20Level&level=" + value); } else if (value != undefined) { this.log(this.name + " Unhandled Light command! cmd=" + c + ", value=" + value); @@ -168,7 +167,7 @@ DomoticzAccessory.prototype = { else { //Scenes if (c == "On" || c == "Off") { - url = "http://" + this.server + ":" + this.port + "/json.htm?type=command¶m=switchscene&idx=" + this.idx + "&switchcmd=" + c; + url = this.platform.urlForQuery("type=command¶m=switchscene&idx=" + this.idx + "&switchcmd=" + c); } else if (value != undefined) { this.log(this.name + " Unhandled Scene command! cmd=" + c + ", value=" + value); From 385b615b81c51d86f172ec00548d84ca84c1ef61 Mon Sep 17 00:00:00 2001 From: Paul Beswick Date: Thu, 23 Jul 2015 13:47:41 -0400 Subject: [PATCH 3/3] Creation of an accessory to control the climate control for a Tesla Model S with accompanying changes to package.json (to add the teslams package) and the sample config file. --- accessories/Tesla.js | 119 +++++++++++++++++++++++++++++++++++++++++++ config-sample.json | 7 +++ package.json | 3 +- 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 accessories/Tesla.js diff --git a/accessories/Tesla.js b/accessories/Tesla.js new file mode 100644 index 0000000..29e9d48 --- /dev/null +++ b/accessories/Tesla.js @@ -0,0 +1,119 @@ +var types = require("HAP-NodeJS/accessories/types.js"); +var tesla = require("teslams"); + +function TeslaAccessory(log, config) { + this.log = log; + this.name = config["name"]; + this.username = config["username"]; + this.password = config["password"]; +} + +TeslaAccessory.prototype = { + + setPowerState: function(powerOn) { + var that = this; + + tesla.get_vid({email: this.username, password: this.password}, function(vehicle) { + + if (powerOn) { + tesla.auto_conditioning({id:vehicle, climate: 'start'}, function(response) { + if (!response.result) + that.log("Started climate control."); + else + that.log("Error starting climate control: " + response.reason); + }); + } + else { + tesla.auto_conditioning({id:vehicle, climate: 'stop'}, function(response) { + if (!response.result) + that.log("Stopped climate control."); + else + that.log("Error starting climate control: " + response.reason); + }); + } + }) + }, + + 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: "Tesla", + 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); }, + perms: ["pw","pr","ev"], + format: "bool", + initialValue: false, + supportEvents: false, + supportBonjour: false, + manfDescription: "Change the power state of the car", + designedMaxLength: 1 + }] + }]; + } +}; + +module.exports.accessory = TeslaAccessory; diff --git a/config-sample.json b/config-sample.json index 125d632..2c81ce4 100644 --- a/config-sample.json +++ b/config-sample.json @@ -148,6 +148,13 @@ "type": "rgbw", // Bulb type (rgbw, rgb, white) | "delay": 35, // Delay between commands sent to the WiFi bridge (default 35) | "repeat": 3 // Number of times each command is repeated for reliability (default 3) | + }, + { + "accessory": "Tesla", + "name": "Tesla", + "description": "This shim supports controlling climate control on the Tesla Model S.", + "username": "tesla_email", + "password" : "tesla_password" } ] } diff --git a/package.json b/package.json index b55bbd6..48916e3 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "wemo": "0.2.x", "wink-js": "0.0.5", "xml2js": "0.4.x", - "xmldoc": "0.1.x" + "xmldoc": "0.1.x", + "teslams": "1.0.1" } }