Added Temperature sensor and Fan
This commit is contained in:
parent
a8e1cb6a4c
commit
1859b247ae
@ -3,7 +3,6 @@ var WebSocketClient = require('websocket').client
|
|||||||
|
|
||||||
var colorOn = "\x1b[30;47m";
|
var colorOn = "\x1b[30;47m";
|
||||||
var colorOff = "\x1b[0m";
|
var colorOff = "\x1b[0m";
|
||||||
var version = "1.0";
|
|
||||||
|
|
||||||
function SmartHomeNGConnection(platform, log, host, port) {
|
function SmartHomeNGConnection(platform, log, host, port) {
|
||||||
this.log = log;
|
this.log = log;
|
||||||
@ -19,7 +18,7 @@ function SmartHomeNGConnection(platform, log, host, port) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SmartHomeNGConnection.prototype.init = function () {
|
SmartHomeNGConnection.prototype.init = function () {
|
||||||
var that = this
|
var that = this;
|
||||||
this.shng_ws = new WebSocketClient();
|
this.shng_ws = new WebSocketClient();
|
||||||
this.shng_ws.on('connect', function(connection) {
|
this.shng_ws.on('connect', function(connection) {
|
||||||
that.log('[SmartHomeNGConnection] connected to server!');
|
that.log('[SmartHomeNGConnection] connected to server!');
|
||||||
@ -84,7 +83,7 @@ SmartHomeNGConnection.prototype.idenfityMyself = function() {
|
|||||||
var buffer = {};
|
var buffer = {};
|
||||||
buffer.cmd = 'identity';
|
buffer.cmd = 'identity';
|
||||||
buffer.sw = 'homebridge-SmarHomeNG';
|
buffer.sw = 'homebridge-SmarHomeNG';
|
||||||
buffer.ver = version;
|
buffer.ver = this.platform.version;
|
||||||
var command = JSON.stringify(buffer);
|
var command = JSON.stringify(buffer);
|
||||||
this.connection.send(command);
|
this.connection.send(command);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
129
index.js
129
index.js
@ -45,8 +45,9 @@ function SmartHomeNGPlatform(log, config, api) {
|
|||||||
this.shng_port = 2424;
|
this.shng_port = 2424;
|
||||||
}
|
}
|
||||||
|
|
||||||
var that = this
|
var that = this;
|
||||||
this.log("SmartHomeNG Platform Plugin Version " + this.getVersion())
|
this.version = this.getVersion();
|
||||||
|
this.log("SmartHomeNG Platform Plugin Version " + this.version)
|
||||||
|
|
||||||
this.shngcon = new SmartHomeNGConnection(this, this.log, this.shng_host, this.shng_port);
|
this.shngcon = new SmartHomeNGConnection(this, this.log, this.shng_host, this.shng_port);
|
||||||
this.shngcon.updateCallback = this.update;
|
this.shngcon.updateCallback = this.update;
|
||||||
@ -143,6 +144,17 @@ SmartHomeNGAccessory.prototype = {
|
|||||||
// construct service and characteristics according to device type
|
// construct service and characteristics according to device type
|
||||||
switch (this.config.type.toLowerCase()) {
|
switch (this.config.type.toLowerCase()) {
|
||||||
// Lightbulb service
|
// Lightbulb service
|
||||||
|
case 'fan':
|
||||||
|
myServices.push(this.getFanService(this.config));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'temperaturesensor':
|
||||||
|
myServices.push(this.getTemperatureSensorService(this.config));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'thermostat':
|
||||||
|
break;
|
||||||
|
|
||||||
case 'lightbulb':
|
case 'lightbulb':
|
||||||
myServices.push(this.getLightbulbService(this.config));
|
myServices.push(this.getLightbulbService(this.config));
|
||||||
break;
|
break;
|
||||||
@ -151,6 +163,10 @@ SmartHomeNGAccessory.prototype = {
|
|||||||
myServices.push(this.getWindowCoveringService(this.config));
|
myServices.push(this.getWindowCoveringService(this.config));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'occupancysensor':
|
||||||
|
myServices.push(this.getOccupancySensorService(this.config));
|
||||||
|
break;
|
||||||
|
|
||||||
case 'motionsensor':
|
case 'motionsensor':
|
||||||
myServices.push(this.getMotionSensorService(this.config));
|
myServices.push(this.getMotionSensorService(this.config));
|
||||||
break;
|
break;
|
||||||
@ -182,7 +198,24 @@ SmartHomeNGAccessory.prototype = {
|
|||||||
/** Registering routines
|
/** Registering routines
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
// boolean: get 0 or 1 from the bus, write boolean
|
shngregister_int: function(name, shngitem, characteristic) {
|
||||||
|
this.log("[" + name + "] Registering callback for '" + shngitem + "'.");
|
||||||
|
var callback = function (shngitem, value) {
|
||||||
|
//this.log("[" + this.name + "] callback for " + characteristic.displayName);
|
||||||
|
characteristic.setValue(value, undefined, 'fromSHNG');
|
||||||
|
}.bind(this);
|
||||||
|
monitoring.push({name: name, characteristic: characteristic.displayName, item: shngitem, callback: callback, inverted: false});
|
||||||
|
},
|
||||||
|
|
||||||
|
shngregister_float: function(name, shngitem, characteristic) {
|
||||||
|
this.log("[" + name + "] Registering callback for '" + shngitem + "'.");
|
||||||
|
var callback = function (shngitem, value) {
|
||||||
|
//this.log("[" + this.name + "] callback for " + characteristic.displayName);
|
||||||
|
characteristic.setValue(value, undefined, 'fromSHNG');
|
||||||
|
}.bind(this);
|
||||||
|
monitoring.push({name: name, characteristic: characteristic.displayName, item: shngitem, callback: callback, inverted: false});
|
||||||
|
},
|
||||||
|
|
||||||
shngregister_bool: function(name, shngitem, characteristic, inverted) {
|
shngregister_bool: function(name, shngitem, characteristic, inverted) {
|
||||||
this.log("[" + name + "] Registering callback for '" + shngitem + "'.");
|
this.log("[" + name + "] Registering callback for '" + shngitem + "'.");
|
||||||
var callback = function (shngitem, value, inverted) {
|
var callback = function (shngitem, value, inverted) {
|
||||||
@ -258,6 +291,38 @@ SmartHomeNGAccessory.prototype = {
|
|||||||
if (callback) callback();
|
if (callback) callback();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
setInt: function(value, callback, context, shngitem) {
|
||||||
|
if (context === 'fromSHNG') {
|
||||||
|
if (callback) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var numericValue = 0;
|
||||||
|
if (value && value>=0) {
|
||||||
|
numericValue = value;
|
||||||
|
}
|
||||||
|
this.log("["+ this.name +"] Setting " + shngitem + " int to %s", numericValue);
|
||||||
|
this.shngcon.setValue(shngitem, numericValue);
|
||||||
|
if (callback) callback();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
setFloat: function(value, callback, context, shngitem) {
|
||||||
|
if (context === 'fromSHNG') {
|
||||||
|
if (callback) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var numericValue = 0;
|
||||||
|
if (value && value>=0) {
|
||||||
|
numericValue = value;
|
||||||
|
}
|
||||||
|
this.log("["+ this.name +"] Setting " + shngitem + " float to %s", numericValue);
|
||||||
|
this.shngcon.setValue(shngitem, numericValue);
|
||||||
|
if (callback) callback();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
/** bindCharacteristic
|
/** bindCharacteristic
|
||||||
* initializes callbacks for 'set' events (from HK) and for SmartHomeNG monitoring events (to HK)
|
* initializes callbacks for 'set' events (from HK) and for SmartHomeNG monitoring events (to HK)
|
||||||
@ -270,6 +335,24 @@ SmartHomeNGAccessory.prototype = {
|
|||||||
myCharacteristic.setValue(defaultValue);
|
myCharacteristic.setValue(defaultValue);
|
||||||
}
|
}
|
||||||
switch (valueType) {
|
switch (valueType) {
|
||||||
|
case "Int":
|
||||||
|
myCharacteristic.on('set', function(value, callback, context) {
|
||||||
|
this.setInt(value, callback, context, shngitem);
|
||||||
|
}.bind(this));
|
||||||
|
myCharacteristic.on('get', function(callback, context) {
|
||||||
|
this.getState(callback, shngitem, inverted);
|
||||||
|
}.bind(this));
|
||||||
|
this.shngregister_int(this.name, shngitem, myCharacteristic);
|
||||||
|
break;
|
||||||
|
case "Float":
|
||||||
|
myCharacteristic.on('set', function(value, callback, context) {
|
||||||
|
this.setFloat(value, callback, context, shngitem);
|
||||||
|
}.bind(this));
|
||||||
|
myCharacteristic.on('get', function(callback, context) {
|
||||||
|
this.getState(callback, shngitem, inverted);
|
||||||
|
}.bind(this));
|
||||||
|
this.shngregister_float(this.name, shngitem, myCharacteristic);
|
||||||
|
break;
|
||||||
case "Bool":
|
case "Bool":
|
||||||
myCharacteristic.on('set', function(value, callback, context) {
|
myCharacteristic.on('set', function(value, callback, context) {
|
||||||
this.setBooleanState(value, callback, context, shngitem, inverted);
|
this.setBooleanState(value, callback, context, shngitem, inverted);
|
||||||
@ -300,6 +383,32 @@ SmartHomeNGAccessory.prototype = {
|
|||||||
* returns a configured service object to the caller (accessory/device)
|
* returns a configured service object to the caller (accessory/device)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
// Create Temperature Sensor service
|
||||||
|
getTemperatureSensorService: function(config) {
|
||||||
|
var myService = new Service.TemperatureSensor(config.name,config.name);
|
||||||
|
// Current temperature
|
||||||
|
if (config.currenttemperature) {
|
||||||
|
this.log("["+ this.name +"] TemperatureSensor CurrentTemperature characteristic enabled");
|
||||||
|
this.bindCharacteristic(myService, Characteristic.CurrentTemperature, "Float", config.currenttemperature, false);
|
||||||
|
}
|
||||||
|
return myService;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Create Fan service
|
||||||
|
getFanService: function(config) {
|
||||||
|
var myService = new Service.Fan(config.name,config.name);
|
||||||
|
var inverted = false;
|
||||||
|
if (config.inverted) {
|
||||||
|
inverted = true;
|
||||||
|
}
|
||||||
|
// On (and Off)
|
||||||
|
if (config.onoff) {
|
||||||
|
this.log("["+ this.name +"] Fan on/off characteristic enabled");
|
||||||
|
this.bindCharacteristic(myService, Characteristic.On, "Bool", config.onoff, inverted);
|
||||||
|
}
|
||||||
|
return myService;
|
||||||
|
},
|
||||||
|
|
||||||
// Create Lightbulb service
|
// Create Lightbulb service
|
||||||
getLightbulbService: function(config) {
|
getLightbulbService: function(config) {
|
||||||
var myService = new Service.Lightbulb(config.name,config.name);
|
var myService = new Service.Lightbulb(config.name,config.name);
|
||||||
@ -342,6 +451,20 @@ SmartHomeNGAccessory.prototype = {
|
|||||||
return myService;
|
return myService;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Create OccupancySensor service
|
||||||
|
getOccupancySensorService: function(config) {
|
||||||
|
var myService = new Service.OccupancySensor(config.name,config.name);
|
||||||
|
var inverted = false;
|
||||||
|
if (config.inverted) {
|
||||||
|
inverted = true;
|
||||||
|
}
|
||||||
|
if (config.motiondetected) {
|
||||||
|
this.log("["+ this.name +"] OccupancySensor OccupancyDetected characteristic enabled");
|
||||||
|
this.bindCharacteristic(myService, Characteristic.OccupancyDetected, "Bool", config.motiondetected, inverted);
|
||||||
|
}
|
||||||
|
return myService;
|
||||||
|
},
|
||||||
|
|
||||||
// Create MotionSensor service
|
// Create MotionSensor service
|
||||||
getMotionSensorService: function(config) {
|
getMotionSensorService: function(config) {
|
||||||
var myService = new Service.MotionSensor(config.name,config.name);
|
var myService = new Service.MotionSensor(config.name,config.name);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user