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 7c29a75..d4760e0 100644 --- a/config-sample.json +++ b/config-sample.json @@ -147,6 +147,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" } } diff --git a/platforms/Domoticz.js b/platforms/Domoticz.js index aacce99..a9e8e72 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,35 +57,42 @@ 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) { 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); }) } 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) { @@ -83,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); } }) @@ -97,14 +115,14 @@ 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) { 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); }) } @@ -116,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; @@ -125,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 = { @@ -135,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); @@ -150,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);