mirror of
https://github.com/mtan93/homebridge.git
synced 2026-03-29 22:04:01 +01:00
Merge pull request #25 from gizmocuz/master
- Added support for RGB lights/Scenes and sorting device names
This commit is contained in:
@@ -1,6 +1,13 @@
|
|||||||
// Domoticz Platform Shim for HomeBridge
|
// Domoticz Platform Shim for HomeBridge
|
||||||
// Written by Joep Verhaeg (http://www.joepverhaeg.nl)
|
// Written by Joep Verhaeg (http://www.joepverhaeg.nl)
|
||||||
//
|
//
|
||||||
|
// Revisions:
|
||||||
|
//
|
||||||
|
// 12 June 2015 [GizMoCuz]
|
||||||
|
// - Added support for RGB lights
|
||||||
|
// - Added support for Scenes
|
||||||
|
// - Sorting device names
|
||||||
|
//
|
||||||
// Domoticz JSON API required
|
// Domoticz JSON API required
|
||||||
// https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Lights_and_switches
|
// https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's#Lights_and_switches
|
||||||
//
|
//
|
||||||
@@ -26,20 +33,47 @@ function DomoticzPlatform(log, config){
|
|||||||
this.port = config["port"];
|
this.port = config["port"];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sortByKey(array, key) {
|
||||||
|
return array.sort(function(a, b) {
|
||||||
|
var x = a[key]; var y = b[key];
|
||||||
|
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
DomoticzPlatform.prototype = {
|
DomoticzPlatform.prototype = {
|
||||||
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 = [];
|
||||||
|
//Get Lights
|
||||||
request.get({
|
request.get({
|
||||||
url: "http://" + this.server + ":" + this.port + "/json.htm?type=command¶m=getlightswitches",
|
url: "http://" + this.server + ":" + this.port + "/json.htm?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) {
|
||||||
json['result'].map(function(s) {
|
var sArray=sortByKey(json['result'],"Name");
|
||||||
accessory = new DomoticzAccessory(that.log, that.server, that.port, s.idx, s.Name, s.IsDimmer);
|
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"));
|
||||||
|
foundAccessories.push(accessory);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
callback(foundAccessories);
|
||||||
|
} else {
|
||||||
|
that.log("There was a problem connecting to Domoticz.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//Get Scenes
|
||||||
|
request.get({
|
||||||
|
url: "http://" + this.server + ":" + this.port + "/json.htm?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);
|
||||||
foundAccessories.push(accessory);
|
foundAccessories.push(accessory);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -51,25 +85,45 @@ DomoticzPlatform.prototype = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function DomoticzAccessory(log, server, port, idx, name, isDimmer) {
|
function DomoticzAccessory(log, server, port, IsScene, idx, name, HaveDimmer, MaxDimLevel, HaveRGB) {
|
||||||
// device info
|
// device info
|
||||||
this.idx = idx;
|
this.IsScene = IsScene;
|
||||||
this.name = name;
|
this.idx = idx;
|
||||||
this.isDimmer = isDimmer;
|
this.name = name;
|
||||||
this.log = log;
|
this.HaveDimmer = HaveDimmer;
|
||||||
this.server = server;
|
this.MaxDimLevel = MaxDimLevel;
|
||||||
this.port = port;
|
this.HaveRGB = HaveRGB;
|
||||||
|
this.log = log;
|
||||||
|
this.server = server;
|
||||||
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
DomoticzAccessory.prototype = {
|
DomoticzAccessory.prototype = {
|
||||||
command: function(c,value) {
|
command: function(c,value) {
|
||||||
this.log(this.name + " sending command " + c + " with value " + value);
|
this.log(this.name + " sending command " + c + " with value " + value);
|
||||||
|
if (this.IsScene == false) {
|
||||||
if (c == "On" || c == "Off") {
|
//Lights
|
||||||
url = "http://" + this.server + ":" + this.port + "/json.htm?type=command¶m=switchlight&idx=" + this.idx + "&switchcmd=" + c + "&level=0";
|
if (c == "On" || c == "Off") {
|
||||||
|
url = "http://" + this.server + ":" + this.port + "/json.htm?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";
|
||||||
|
}
|
||||||
|
else if (c == "setLevel") {
|
||||||
|
url = "http://" + this.server + ":" + this.port + "/json.htm?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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (value != undefined) {
|
else {
|
||||||
url = "http://" + this.server + ":" + this.port + "/json.htm?type=command¶m=switchlight&idx=" + this.idx + "&switchcmd=Set%20Level&level=" + value;
|
//Scenes
|
||||||
|
if (c == "On" || c == "Off") {
|
||||||
|
url = "http://" + this.server + ":" + this.port + "/json.htm?type=command¶m=switchscene&idx=" + this.idx + "&switchcmd=" + c;
|
||||||
|
}
|
||||||
|
else if (value != undefined) {
|
||||||
|
this.log(this.name + " Unhandled Scene command! cmd=" + c + ", value=" + value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var that = this;
|
var that = this;
|
||||||
@@ -172,7 +226,7 @@ DomoticzAccessory.prototype = {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isDimmer == true) {
|
if (this.HaveDimmer == true) {
|
||||||
cTypes.push({
|
cTypes.push({
|
||||||
cType: types.BRIGHTNESS_CTYPE,
|
cType: types.BRIGHTNESS_CTYPE,
|
||||||
onUpdate: function(value) { that.command("setLevel", value); },
|
onUpdate: function(value) { that.command("setLevel", value); },
|
||||||
@@ -183,17 +237,33 @@ DomoticzAccessory.prototype = {
|
|||||||
supportBonjour: false,
|
supportBonjour: false,
|
||||||
manfDescription: "Adjust Brightness of Light",
|
manfDescription: "Adjust Brightness of Light",
|
||||||
designedMinValue: 0,
|
designedMinValue: 0,
|
||||||
designedMaxValue: 16, //100% for KAKU devices.
|
designedMaxValue: this.MaxDimLevel,
|
||||||
designedMinStep: 1,
|
designedMinStep: 1,
|
||||||
unit: "%"
|
unit: "%"
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
if (this.HaveRGB == true) {
|
||||||
|
cTypes.push({
|
||||||
|
cType: types.HUE_CTYPE,
|
||||||
|
onUpdate: function(value) { that.command("setHue", value); },
|
||||||
|
perms: ["pw","pr","ev"],
|
||||||
|
format: "int",
|
||||||
|
initialValue: 0,
|
||||||
|
supportEvents: true,
|
||||||
|
supportBonjour: false,
|
||||||
|
manfDescription: "Adjust Hue of Light",
|
||||||
|
designedMinValue: 0,
|
||||||
|
designedMaxValue: 360,
|
||||||
|
designedMinStep: 1,
|
||||||
|
unit: "arcdegrees"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return cTypes
|
return cTypes
|
||||||
},
|
},
|
||||||
|
|
||||||
sType: function() {
|
sType: function() {
|
||||||
if (this.isDimmer == true) {
|
if (this.HaveDimmer == true) {
|
||||||
return types.LIGHTBULB_STYPE
|
return types.LIGHTBULB_STYPE
|
||||||
} else {
|
} else {
|
||||||
return types.SWITCH_STYPE
|
return types.SWITCH_STYPE
|
||||||
|
|||||||
Reference in New Issue
Block a user