Skip to content

Commit

Permalink
Domoticz Internal Security System support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramon Swilem committed Oct 20, 2016
1 parent d1f0080 commit 77b4c8f
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 5 deletions.
3 changes: 0 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,6 @@ eDomoticzPlatform.prototype = {
headers: this.myopt,
json: true
}, function(err, response, json) {
// console.log(err);
// console.log(response);
// console.log(json);
if (!err && response.statusCode == 200)
{
if (json.result !== undefined)
Expand Down
5 changes: 4 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ module.exports = {
DeviceTypeDoorLock: 11,
DeviceTypeBlindsPercentage: 13,
DeviceTypeBlindsPercentageInverted: 16,
DeviceTypeMedia: 17 //media - only supported as on/off switch at the moment.
DeviceTypeMedia: 17, //media - only supported as on/off switch at the moment.

/* Made up, interal types */
DeviceTypeSecuritySystem: 254, // Has HardwareTypeVal 67 w/o any SwitchTypeVal

/*
DeviceTypeNotSupportedYet: 4, //x10siren
Expand Down
109 changes: 108 additions & 1 deletion lib/domoticz_accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function eDomoticzAccessory(platform, server, port, IsScene, status, idx, name,
} else {
this.haveDimmer = false;
}

this.services = [];
this.platform = platform;
this.server = server;
Expand All @@ -29,6 +30,16 @@ function eDomoticzAccessory(platform, server, port, IsScene, status, idx, name,
this.swType = swType;
this.swTypeVal = swTypeVal;
this.isSwitch = (typeof this.swTypeVal !== 'undefined' && this.swTypeVal >= 0 && this.name.indexOf("Occupied") == -1);
switch (hwType)
{
case 67: // Domoticz Security Panel
this.swTypeVal = Constants.DeviceTypeSecuritySystem;
break;

default:
break;
}

this.Type = Type;
this.batteryRef = batteryRef;
this.CounterToday = 1;
Expand Down Expand Up @@ -555,12 +566,63 @@ eDomoticzAccessory.prototype = {
callback();
}.bind(this));
},
getSecuritySystemStatus: function(callback) {
var cachedValue = this.cachedValues[Characteristic.SecuritySystemCurrentState.UUID];
if (cachedValue) {
callback(null, cachedValue);
}

Domoticz.deviceStatus(this, function(json) {
var value;
var sArray = Helper.sortByKey(json.result, "Name");
sArray.map(function(s) {
switch (s.Data) {
case "Arm Home":
value = Characteristic.SecuritySystemCurrentState.STAY_ARM;
break;

case "Arm Away":
value = Characteristic.SecuritySystemCurrentState.AWAY_ARM;
break;

case "Normal":
default:
value = Characteristic.SecuritySystemCurrentState.DISARMED;
break;
}
}.bind(this));

if (!cachedValue) {
callback(null, value);
}

this.cachedValues[Characteristic.SecuritySystemCurrentState.UUID] = value;
}.bind(this));
},
setSecuritySystemStatus: function(securityService, alarmState, callback, context) {
if (context && (context == "eDomoticz-MQTT" || context == "callback-self")) {
if (callback) {
callback();
}
return;
}

// Updating the state with alarmState is not implemented yet. We'd need the users' alarm code for that.
// Return the last alarm status (before the change) for now.
securityService.getCharacteristic(Characteristic.SecuritySystemCurrentState).getValue(function(sender, currentState) {
callback(null, currentState);
this.cachedValues[Characteristic.SecuritySystemCurrentState.UUID] = currentState;
setTimeout(function() {
securityService.getCharacteristic(Characteristic.SecuritySystemTargetState).setValue(currentState, false, "callback-self");
}.bind(this), 100);
}.bind(this));
},
handleMQTTMessage: function(message, callback) {
this.platform.log("MQTT Message received for %s.\nName:\t\t%s\nDevice:\t\t%s,%s\nIs Switch:\t%s\nSwitchTypeVal:\t%s\nMQTT Message:\n%s", this.name, this.name, this.Type, this.subType, this.isSwitch, this.swTypeVal, JSON.stringify(message, null, 4));
if ((this.Type == "P1 Smart Meter" && this.swTypeVal == 0 && this.subType == "Energy") || (this.Type == "P1 Smart Meter" && this.swTypeVal == 1 && this.subType == "Gas") || (this.Type == "General" && this.swTypeVal == 2 && this.subType == "Counter Incremental") || (this.name.indexOf("Occupied") > -1) || (this.Type == "General" && this.swTypeVal == 1 && this.subType == "Visibility") || (this.Type == "General" && this.swTypeVal === 0 && this.subType == "kWh") || (this.Type == "General" && this.subType == "Solar Radiation" && this.swTypeVal === 0) || (this.Type == "YouLess Meter" && this.swTypeVal === 0) || (this.name.indexOf("Location") > -1) || (this.Type == "RFXMeter")) {
this.swTypeVal = false;
this.isSwitch = false;
//cludgey fix for a P1 SmartMeter Virtual Sensor being ID'd as a doorbell in Domoticz, and Incremental COunters being id'd as contact switches
//cludgey fix for a P1 SmartMeter Virtual Sensor being ID'd as a doorbell in Domoticz, and Incremental Counters being id'd as contact switches
//and other such Domoticz-generated oddities
}
if (this.isSwitch) {
Expand Down Expand Up @@ -639,6 +701,42 @@ eDomoticzAccessory.prototype = {
callback(targetPositionCharacteristic, position);
break;
}
case this.swTypeVal == Constants.DeviceTypeSecuritySystem:
{
var systemState = Characteristic.SecuritySystemCurrentState.STAY_ARM;
switch (message.nvalue)
{
case 0: //Disarm
case 1: //Normal Delay
case 13: //Disarm
systemState = Characteristic.SecuritySystemCurrentState.DISARMED;
break;
case 9: //Arm Away
case 10: //Arm Away Delayed
systemState = Characteristic.SecuritySystemCurrentState.AWAY_ARM;
break;
case 11: //Arm Home
case 12: //Arm Home Delayed
systemState = Characteristic.SecuritySystemCurrentState.STAY_ARM;
break;

default:
case 2: //Alarm
case 3: //Alarm Delayed
case 4: //Motion
case 5: //No Motion
case 6: //Panic
case 7: //Panic End
case 8:
}

var currentStateCharacteristic = this.getService(Service.SecuritySystem).getCharacteristic(Characteristic.SecuritySystemCurrentState);
var targetStateCharacteristic = this.getService(Service.SecuritySystem).getCharacteristic(Characteristic.SecuritySystemTargetState);
callback(currentStateCharacteristic, systemState);
callback(targetStateCharacteristic, systemState);
break;
}

default:
break;
}
Expand Down Expand Up @@ -939,6 +1037,15 @@ eDomoticzAccessory.prototype = {
this.services.push(blindService);
break;
}
case this.swTypeVal == Constants.DeviceTypeSecuritySystem:
{
var securityService = new Service.SecuritySystem(this.name);
securityService.getCharacteristic(Characteristic.SecuritySystemCurrentState).on('get', this.getSecuritySystemStatus.bind(this));
securityService.getCharacteristic(Characteristic.SecuritySystemTargetState).on('get', this.getSecuritySystemStatus.bind(this)).on('set', this.setSecuritySystemStatus.bind(this, securityService));
this.services.push(securityService);
break;
}

default:
break;
}
Expand Down

0 comments on commit 77b4c8f

Please sign in to comment.