Skip to content
This repository has been archived by the owner on Apr 25, 2021. It is now read-only.

Commit

Permalink
Security system accessory added
Browse files Browse the repository at this point in the history
  • Loading branch information
ilcato committed Apr 21, 2017
1 parent 687e25f commit ba08fe6
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 43 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ The plugin is published through [NPM](https://www.npmjs.com/package/homebridge-f
npm install -g homebridge-fibaro-hc2

# Release notes
Version 1.1.0
+ Security system accessory added - See wiki

Version 1.0.9
+ Thermostat logic fix.

Expand Down
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"username": "PUT USERNAME OF YOUR HC2 HERE",
"password": "PUT PASSWORD OF YOUR HC2 HERE",
"grouping": "PUT none OR room",
"pollerperiod": "PUT 0 FOR DISABLING POLLING, 1 - 100 INTERVAL IN SECONDS. 2 SECONDS IS THE DEFAULT"
"pollerperiod": "PUT 0 FOR DISABLING POLLING, 1 - 100 INTERVAL IN SECONDS. 2 SECONDS IS THE DEFAULT",
"securitysystem": "PUT enabled OR disabled IN ORDER TO MANAGE THE AVAILABILITY OF THE SECURITY SYSTEM"
}

],
Expand Down
159 changes: 147 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
// "username": "PUT USERNAME OF YOUR HC2 HERE",
// "password": "PUT PASSWORD OF YOUR HC2 HERE",
// "grouping": "PUT none OR room",
// "pollerperiod": "PUT 0 FOR DISABLING POLLING, 1 - 100 INTERVAL IN SECONDS. 5 SECONDS IS THE DEFAULT"
// "pollerperiod": "PUT 0 FOR DISABLING POLLING, 1 - 100 INTERVAL IN SECONDS. 5 SECONDS IS THE DEFAULT",
// "securitysystem": "PUT enabled OR disabled IN ORDER TO MANAGE THE AVAILABILITY OF THE SECURITY SYSTEM"
// }
// ],
//
Expand Down Expand Up @@ -50,6 +51,11 @@ function FibaroHC2Platform(log, config, api){
this.pollerPeriod = parseInt(this.pollerPeriod);
else if (this.pollerPeriod == undefined)
this.pollerPeriod = 5;
this.securitySystem = config["securitysystem"];
if (this.securitySystem == undefined || (this.securitySystem != "enabled" && this.securitySystem != "disabled")) {
this.securitySystem = "disabled";
}
this.securitySystemScenes = {};

var self = this;
this.requestServer = http.createServer();
Expand All @@ -75,19 +81,45 @@ function FibaroHC2Platform(log, config, api){
}
FibaroHC2Platform.prototype.addAccessories = function() {
var that = this;
this.fibaroClient.getRooms()
.then(function (rooms) {
rooms.map(function(s, i, a) {
that.rooms[s.id] = s.name;
});
return that.fibaroClient.getDevices();
this.fibaroClient.getScenes()
.then(function (scenes) {
if (that.securitySystem == "enabled") {
scenes.map(function(s) {
switch (s.name) {
case "SetStayArmed":
that.securitySystemScenes.SetStayArmed = s.id;
break;
case "SetAwayArmed":
that.securitySystemScenes.SetAwayArmed = s.id;
break;
case "SetNightArmed":
that.securitySystemScenes.SetNightArmed = s.id;
break;
case "SetDisarmed":
that.securitySystemScenes.SetDisarmed = s.id;
break;
case "SetAlarmTriggered":
that.securitySystemScenes.SetAlarmTriggered = s.id;
break;
default:
break;
}
});
}
return that.fibaroClient.getRooms()
})
.then(function (devices) {
.then(function (rooms) {
rooms.map(function(s, i, a) {
that.rooms[s.id] = s.name;
});
return that.fibaroClient.getDevices();
})
.then(function (devices) {
that.HomeCenterDevices2HomeKitAccessories(devices);
})
.catch(function (err, response) {
})
.catch(function (err, response) {
that.log("Error getting data from Home Center: " + err + " " + response);
});
});
}
FibaroHC2Platform.prototype.HomeCenterDevices2HomeKitAccessories = function(devices) {
var foundAccessories = [];
Expand Down Expand Up @@ -211,6 +243,15 @@ FibaroHC2Platform.prototype.HomeCenterDevices2HomeKitAccessories = function(devi
that.addAccessory(services, null, currentRoomID, null)
}
}
// Create Security System accessory
if (this.securitySystem == "enabled") {
services = [];
service = {controlService: new Service.SecuritySystem("FibaroSecuritySystem"), characteristics: [Characteristic.SecuritySystemCurrentState, Characteristic.SecuritySystemTargetState]};
service.controlService.subtype = "0--";
services.push(service);
service = null;
this.addAccessory(services, "FibaroSecuritySystem", null, 0)
}
// Remove not reviewd accessories: cached accessories no more present in Home Center
for (var a in this.accessories) {
if (!this.accessories[a].reviewed) {
Expand All @@ -224,7 +265,7 @@ FibaroHC2Platform.prototype.HomeCenterDevices2HomeKitAccessories = function(devi
}
FibaroHC2Platform.prototype.addAccessory = function(services, name, currentRoomID, deviceID) {
var accessoryName = (name) ? name : this.rooms[currentRoomID] + "-Devices";
var uniqueSeed = accessoryName + currentRoomID;
var uniqueSeed = accessoryName + (currentRoomID ? currentRoomID : "");
var a = this.existingAccessory(uniqueSeed);
var isNewAccessory = false;
if (a == null) {
Expand All @@ -240,6 +281,10 @@ FibaroHC2Platform.prototype.addAccessory = function(services, name, currentRoomI
.setCharacteristic(Characteristic.Model, "HomeCenterBridgedAccessory")
.setCharacteristic(Characteristic.SerialNumber, "<unknown>");

// Store SecuritySystem Accessory
if (this.securitySystem == "enabled") {
this.securitySystemService = a.getService(Service.SecuritySystem);
}
// Remove services existing in HomeKit accessory no more present in Home Center
for (var t = 0; t < a.services.length; t++) {
var found = false;
Expand Down Expand Up @@ -341,6 +386,27 @@ FibaroHC2Platform.prototype.bindCharacteristicEvents = function(characteristic,
setTimeout( function(){
characteristic.setValue(0, undefined, 'fromSetValue');
}, 100 );
} else if (characteristic.UUID == (new Characteristic.SecuritySystemTargetState()).UUID) {
var sceneID;
switch (value) {
case Characteristic.SecuritySystemTargetState.AWAY_ARM:
sceneID = this.securitySystemScenes.SetAwayArmed;
break
case Characteristic.SecuritySystemTargetState.DISARM:
sceneID = this.securitySystemScenes.SetDisarmed;
value = Characteristic.SecuritySystemCurrentState.DISARMED;
break
case Characteristic.SecuritySystemTargetState.NIGHT_ARM:
sceneID = this.securitySystemScenes.SetNightArmed;
break;
case Characteristic.SecuritySystemTargetState.STAY_ARM:
sceneID = this.securitySystemScenes.SetStayArmed;
break;
default:
break;
}
service.setCharacteristic(Characteristic.SecuritySystemCurrentState, value);
this.scene(sceneID);
} else if (characteristic.UUID == (new Characteristic.On()).UUID) {
if (characteristic.value == true && value == 0 || characteristic.value == false && value == 1)
this.command(value == 0 ? "turnOff": "turnOn", null, service, IDs);
Expand Down Expand Up @@ -388,6 +454,58 @@ FibaroHC2Platform.prototype.bindCharacteristicEvents = function(characteristic,
}.bind(this));
}
FibaroHC2Platform.prototype.getAccessoryValue = function(callback, returnBoolean, characteristic, service, IDs) {
if (IDs[0] == "0") {
this.fibaroClient.getGlobalVariable("SecuritySystem")
.then(function(json) {
var state;
// return global variable state for all the current and target state, it should normally be the same
if (characteristic.UUID == (new Characteristic.SecuritySystemCurrentState()).UUID) {
switch (json.value) {
case "AwayArmed":
state = Characteristic.SecuritySystemCurrentState.AWAY_ARM;
break
case "Disarmed":
state = Characteristic.SecuritySystemCurrentState.DISARM;
break
case "NightArmed":
state = Characteristic.SecuritySystemCurrentState.NIGHT_ARM;
break;
case "StayArmed":
state = Characteristic.SecuritySystemCurrentState.STAY_ARM;
break
case "AlarmTriggered":
state = Characteristic.SecuritySystemCurrentState.ALARM_TRIGGERED;
break
default:
state = Characteristic.SecuritySystemCurrentState.DISARMED;
break;
}
} else if (characteristic.UUID == (new Characteristic.SecuritySystemTargetState()).UUID) {
switch (json.value) {
case "AwayArmed":
state = Characteristic.SecuritySystemTargetState.AWAY_ARM;
break
case "Disarmed":
state = Characteristic.SecuritySystemTargetState.DISARM;
break
case "NightArmed":
state = Characteristic.SecuritySystemTargetState.NIGHT_ARM;
break;
case "StayArmed":
state = Characteristic.SecuritySystemTargetState.STAY_ARM;
break
default:
state = Characteristic.SecuritySystemTargetState.DISARM;
break;
}
}
callback(undefined, state);
})
.catch(function(err, response) {
that.log("There was a problem getting value from Global Variable: SecuritySystem" + " - Err: " + err + " - Response: " + response);
});
return;
}
var that = this;
this.fibaroClient.getDeviceProperties(IDs[0])
.then(function(properties) {
Expand Down Expand Up @@ -462,6 +580,16 @@ FibaroHC2Platform.prototype.command = function(c,value, service, IDs) {
that.log("There was a problem sending command " + c + " to " + IDs[0]);
});
}
FibaroHC2Platform.prototype.scene = function(sceneID) {
var that = this;
this.fibaroClient.executeScene(sceneID)
.then(function (response) {
that.log("Executed scene: " + sceneID);
})
.catch(function (err, response) {
that.log("There was a problem executing scene: " + sceneID);
});
}
FibaroHC2Platform.prototype.subscribeUpdate = function(service, characteristic, onOff, propertyChanged) {
if (characteristic.UUID == (new Characteristic.PositionState()).UUID)
return;
Expand All @@ -482,6 +610,13 @@ FibaroHC2Platform.prototype.startPollingUpdate = function() {
if (updates.changes != undefined) {
updates.changes.map(function(s) {
if (s.value != undefined) {
// Set Security System to triggered if a Fibaro Alert is present
if (that.securitySystem == "enabled") {
if (s.fibaroAlarm == "true") {
that.scene(that.securitySystemScenes.SetAlarmTriggered);
that.securitySystemService.setCharacteristic(Characteristic.SecuritySystemCurrentState, Characteristic.SecuritySystemCurrentState.ALARM_TRIGGERED);
}
}
var value=parseInt(s.value);
if (isNaN(value))
value=(s.value === "true");
Expand Down
60 changes: 60 additions & 0 deletions lib/fibaro-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ function FibaroClient(host, username, password) {
this.password = password;
this.auth = "Basic " + new Buffer(this.username + ":" + this.password).toString("base64");
}
FibaroClient.prototype.getScenes = function() {
var that = this;
var p = new Promise(function(resolve, reject) {
var url = "http://"+that.host+"/api/scenes";
request.get({
url: url,
headers : {
"Authorization" : that.auth
},
json: true
}, function(err, response, json) {
if (!err && response.statusCode == 200)
resolve(json);
else
reject(err, response);
});
});
return p;
}
FibaroClient.prototype.getRooms = function() {
var that = this;
var p = new Promise(function(resolve, reject) {
Expand Down Expand Up @@ -91,6 +110,28 @@ FibaroClient.prototype.executeDeviceAction = function(ID, action, param) {
});
return p;
}
FibaroClient.prototype.executeScene = function(ID) {
var that = this;
var p = new Promise(function(resolve, reject) {
var url = "http://"+that.host+"/api/scenes/"+ID+"/action/start";
var body = null;
var method = "post";
request({
url: url,
body: body,
method: method,
headers: {
"Authorization" : that.auth
}
}, function(err, response) {
if (!err && (response.statusCode == 200 || response.statusCode == 202))
resolve(response);
else
reject(err, response);
});
});
return p;
}
FibaroClient.prototype.refreshStates = function(lastPoll) {
var that = this;
var p = new Promise(function(resolve, reject) {
Expand All @@ -110,6 +151,25 @@ FibaroClient.prototype.refreshStates = function(lastPoll) {
});
return p;
}
FibaroClient.prototype.getGlobalVariable = function(globalVariableID) {
var that = this;
var p = new Promise(function(resolve, reject) {
var url = "http://"+that.host+"/api/globalVariables/"+globalVariableID;
request.get({
url: url,
headers : {
"Authorization" : that.auth
},
json: true
}, function(err, response, json) {
if (!err && response.statusCode == 200)
resolve(json);
else
reject(err, response);
});
});
return p;
}

module.exports.createClient = function(host, username, password) {
return new FibaroClient(host, username, password);
Expand Down
30 changes: 0 additions & 30 deletions stressTestHC.js

This file was deleted.

0 comments on commit ba08fe6

Please sign in to comment.