Merge branch 'master' into Sonos-platform

This commit is contained in:
bezzers
2015-07-23 20:50:39 -04:00
4 changed files with 160 additions and 16 deletions

119
accessories/Tesla.js Normal file
View File

@@ -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;

View File

@@ -147,6 +147,13 @@
"type": "rgbw", // Bulb type (rgbw, rgb, white) | "type": "rgbw", // Bulb type (rgbw, rgb, white) |
"delay": 35, // Delay between commands sent to the WiFi bridge (default 35) | "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) | "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"
} }
] ]
} }

View File

@@ -27,6 +27,7 @@
"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",
"teslams": "1.0.1"
} }
} }

View File

@@ -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". // 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. // The default code for all HomeBridge accessories is 031-45-154.
// //
@@ -30,8 +38,11 @@ var request = require("request");
function DomoticzPlatform(log, config){ function DomoticzPlatform(log, config){
this.log = log; this.log = log;
this.user = config["user"];
this.password = config["password"];
this.server = config["server"]; this.server = config["server"];
this.port = config["port"]; this.port = config["port"];
this.protocol = config["ssl"] ? "https" : "http";
this.roomid = 0; this.roomid = 0;
if (typeof config["roomid"] != 'undefined') { if (typeof config["roomid"] != 'undefined') {
this.roomid = config["roomid"]; this.roomid = config["roomid"];
@@ -46,35 +57,42 @@ function sortByKey(array, key) {
} }
DomoticzPlatform.prototype = { 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) { accessories: function(callback) {
this.log("Fetching Domoticz lights and switches..."); this.log("Fetching Domoticz lights and switches...");
var that = this; var that = this;
var foundAccessories = []; var foundAccessories = [];
if (this.roomid == 0) { if (this.roomid == 0) {
//Get Lights //Get Lights
request.get({ 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 json: true
}, function(err, response, json) { }, function(err, response, json) {
if (!err && response.statusCode == 200) { if (!err && response.statusCode == 200) {
if (json['result'] != undefined) { if (json['result'] != undefined) {
var sArray=sortByKey(json['result'],"Name"); var sArray=sortByKey(json['result'],"Name");
sArray.map(function(s) { 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); foundAccessories.push(accessory);
}) })
} }
callback(foundAccessories); callback(foundAccessories);
} else { } else {
that.log("There was a problem connecting to Domoticz."); that.log("There was a problem connecting to Domoticz. (" + err + ")");
} }
}); });
} }
else { else {
//Get all devices specified in the room //Get all devices specified in the room
request.get({ 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 json: true
}, function(err, response, json) { }, function(err, response, json) {
if (!err && response.statusCode == 200) { if (!err && response.statusCode == 200) {
@@ -83,7 +101,7 @@ DomoticzPlatform.prototype = {
sArray.map(function(s) { sArray.map(function(s) {
//only accept switches for now //only accept switches for now
if (typeof s.SwitchType != 'undefined') { 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); foundAccessories.push(accessory);
} }
}) })
@@ -97,14 +115,14 @@ DomoticzPlatform.prototype = {
//Get Scenes //Get Scenes
foundAccessories = []; foundAccessories = [];
request.get({ request.get({
url: "http://" + this.server + ":" + this.port + "/json.htm?type=scenes", url: this.urlForQuery("type=scenes"),
json: true json: true
}, function(err, response, json) { }, function(err, response, json) {
if (!err && response.statusCode == 200) { if (!err && response.statusCode == 200) {
if (json['result'] != undefined) { if (json['result'] != undefined) {
var sArray=sortByKey(json['result'],"Name"); var sArray=sortByKey(json['result'],"Name");
sArray.map(function(s) { 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); 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 // device info
this.IsScene = IsScene; this.IsScene = IsScene;
this.idx = idx; this.idx = idx;
@@ -125,8 +143,7 @@ function DomoticzAccessory(log, server, port, IsScene, idx, name, HaveDimmer, Ma
this.MaxDimLevel = MaxDimLevel; this.MaxDimLevel = MaxDimLevel;
this.HaveRGB = HaveRGB; this.HaveRGB = HaveRGB;
this.log = log; this.log = log;
this.server = server; this.platform = platform;
this.port = port;
} }
DomoticzAccessory.prototype = { DomoticzAccessory.prototype = {
@@ -135,13 +152,13 @@ DomoticzAccessory.prototype = {
if (this.IsScene == false) { if (this.IsScene == false) {
//Lights //Lights
if (c == "On" || c == "Off") { if (c == "On" || c == "Off") {
url = "http://" + this.server + ":" + this.port + "/json.htm?type=command&param=switchlight&idx=" + this.idx + "&switchcmd=" + c + "&level=0"; url = this.platform.urlForQuery("type=command&param=switchlight&idx=" + this.idx + "&switchcmd=" + c + "&level=0");
} }
else if (c == "setHue") { else if (c == "setHue") {
url = "http://" + this.server + ":" + this.port + "/json.htm?type=command&param=setcolbrightnessvalue&idx=" + this.idx + "&hue=" + value + "&brightness=100" + "&iswhite=false"; url = this.platform.urlForQuery("type=command&param=setcolbrightnessvalue&idx=" + this.idx + "&hue=" + value + "&brightness=100" + "&iswhite=false");
} }
else if (c == "setLevel") { else if (c == "setLevel") {
url = "http://" + this.server + ":" + this.port + "/json.htm?type=command&param=switchlight&idx=" + this.idx + "&switchcmd=Set%20Level&level=" + value; url = this.platform.urlForQuery("type=command&param=switchlight&idx=" + this.idx + "&switchcmd=Set%20Level&level=" + value);
} }
else if (value != undefined) { else if (value != undefined) {
this.log(this.name + " Unhandled Light command! cmd=" + c + ", value=" + value); this.log(this.name + " Unhandled Light command! cmd=" + c + ", value=" + value);
@@ -150,7 +167,7 @@ DomoticzAccessory.prototype = {
else { else {
//Scenes //Scenes
if (c == "On" || c == "Off") { if (c == "On" || c == "Off") {
url = "http://" + this.server + ":" + this.port + "/json.htm?type=command&param=switchscene&idx=" + this.idx + "&switchcmd=" + c; url = this.platform.urlForQuery("type=command&param=switchscene&idx=" + this.idx + "&switchcmd=" + c);
} }
else if (value != undefined) { else if (value != undefined) {
this.log(this.name + " Unhandled Scene command! cmd=" + c + ", value=" + value); this.log(this.name + " Unhandled Scene command! cmd=" + c + ", value=" + value);