Files
homebridge/platforms/FibaroHC2.js
ilcato 32877abc98 Complete refactoring and adoption of new API
Added support for LightSensor
2015-09-18 17:33:36 +02:00

311 lines
11 KiB
JavaScript

// Fibaro Home Center 2 Platform Shim for HomeBridge
//
// Remember to add platform to config.json. Example:
// "platforms": [
// {
// "platform": "FibaroHC2",
// "name": "FibaroHC2",
// "host": "PUT IP ADDRESS OF YOUR HC2 HERE",
// "username": "PUT USERNAME OF YOUR HC2 HERE",
// "password": "PUT PASSWORD OF YOUR HC2 HERE"
// }
// ],
//
// 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.
var types = require("HAP-NodeJS/accessories/types.js");
var Service = require("HAP-NodeJS").Service;
var Characteristic = require("HAP-NodeJS").Characteristic;
var request = require("request");
function FibaroHC2Platform(log, config){
this.log = log;
this.host = config["host"];
this.username = config["username"];
this.password = config["password"];
this.auth = "Basic " + new Buffer(this.username + ":" + this.password).toString("base64");
this.url = "http://"+this.host+"/api/devices";
startPollingUpdate( this );
}
FibaroHC2Platform.prototype = {
accessories: function(callback) {
this.log("Fetching Fibaro Home Center devices...");
var that = this;
var foundAccessories = [];
request.get({
url: this.url,
headers : {
"Authorization" : this.auth
},
json: true
}, function(err, response, json) {
if (!err && response.statusCode == 200) {
if (json != undefined) {
json.map(function(s) {
that.log("Found: " + s.type);
if (s.visible == true) {
var accessory = null;
if (s.type == "com.fibaro.multilevelSwitch")
accessory = new FibaroDimmerAccessory(that, s);
else if (s.type == "com.fibaro.FGRM222" || s.type == "com.fibaro.FGR221")
accessory = new FibaroRollerShutterAccessory(that, s);
else if (s.type == "com.fibaro.binarySwitch" || s.type == "com.fibaro.developer.bxs.virtualBinarySwitch")
accessory = new FibaroBinarySwitchAccessory(that, s);
else if (s.type == "com.fibaro.FGMS001" || s.type == "com.fibaro.motionSensor")
accessory = new FibaroMotionSensorAccessory(that, s);
else if (s.type == "com.fibaro.temperatureSensor")
accessory = new FibaroTemperatureSensorAccessory(that, s);
else if (s.type == "com.fibaro.doorSensor")
accessory = new FibaroDoorSensorAccessory(that, s);
else if (s.type == "com.fibaro.lightSensor")
accessory = new FibaroLightSensorAccessory(that, s);
if (accessory != null) {
accessory.getServices = function() {
return that.getServices(accessory);
};
foundAccessories.push(accessory);
}
}
})
}
callback(foundAccessories);
} else {
that.log("There was a problem connecting with FibaroHC2.");
}
});
},
command: function(c,value, that) {
var url = "http://"+this.host+"/api/devices/"+that.id+"/action/"+c;
var body = value != undefined ? JSON.stringify({
"args": [ value ]
}) : null;
var method = "post";
request({
url: url,
body: body,
method: method,
headers: {
"Authorization" : this.auth
},
}, function(err, response) {
if (err) {
that.platform.log("There was a problem sending command " + c + " to" + that.name);
that.platform.log(url);
} else {
that.platform.log(that.name + " sent command " + c);
that.platform.log(url);
}
});
},
getAccessoryValue: function(callback, returnBoolean, that) {
var url = "http://"+that.platform.host+"/api/devices/"+that.id+"/properties/value";
request.get({
headers : {
"Authorization" : that.platform.auth
},
json: true,
url: url
}, function(err, response, json) {
that.platform.log(url);
if (!err && response.statusCode == 200) {
if (returnBoolean)
callback(undefined, json.value == 0 ? 0 : 1);
else
callback(undefined, json.value);
} else {
that.platform.log("There was a problem getting value from" + that.id);
}
})
},
getInformationService: function(homebridgeAccessory) {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, homebridgeAccessory.name)
.setCharacteristic(Characteristic.Manufacturer, homebridgeAccessory.manufacturer)
.setCharacteristic(Characteristic.Model, homebridgeAccessory.model)
.setCharacteristic(Characteristic.SerialNumber, homebridgeAccessory.serialNumber);
return informationService;
},
bindCharacteristicEvents: function(characteristic, homebridgeAccessory) {
var onOff = characteristic.format == "bool" ? true : false;
var readOnly = characteristic.writable == undefined || characteristic.writable == false ? true : false;
subscribeUpdate(characteristic, homebridgeAccessory, onOff);
if (!readOnly) {
characteristic
.on('set', function(value, callback, context) {
if( context !== 'fromFibaro' ) {
if (onOff)
homebridgeAccessory.platform.command(value == 0 ? "turnOff": "turnOn", null, homebridgeAccessory);
else
homebridgeAccessory.platform.command("setValue", value, homebridgeAccessory);
}
callback();
}.bind(this) );
}
characteristic
.on('get', function(callback) {
homebridgeAccessory.platform.getAccessoryValue(callback, onOff, homebridgeAccessory);
}.bind(this) );
},
getServices: function(homebridgeAccessory) {
var informationService = homebridgeAccessory.platform.getInformationService(homebridgeAccessory);
for (var i=0; i < homebridgeAccessory.characteristics.length; i++) {
var characteristic = homebridgeAccessory.controlService.getCharacteristic(homebridgeAccessory.characteristics[i]);
if (characteristic == undefined)
characteristic = homebridgeAccessory.controlService.addCharacteristic(homebridgeAccessory.characteristics[i]);
homebridgeAccessory.platform.bindCharacteristicEvents(characteristic, homebridgeAccessory);
}
return [informationService, homebridgeAccessory.controlService];
}
}
function FibaroDimmerAccessory(platform, s) {
this.platform = platform;
this.remoteAccessory = s;
this.id = s.id;
this.name = s.name;
this.model = s.type;
this.manufacturer = "Fibaro";
this.serialNumber = "<unknown>";
this.controlService = new Service.Lightbulb(this.name);
this.characteristics = [Characteristic.On, Characteristic.Brightness];
}
function FibaroRollerShutterAccessory(platform, s) {
this.platform = platform;
this.remoteAccessory = s;
this.id = s.id;
this.name = s.name;
this.model = s.type;
this.manufacturer = "Fibaro";
this.serialNumber = "<unknown>";
this.controlService = new Service.WindowCovering(this.name);
this.characteristics = [Characteristic.CurrentPosition, Characteristic.TargetPosition, Characteristic.PositionState];
}
function FibaroBinarySwitchAccessory(platform, s) {
this.platform = platform;
this.remoteAccessory = s;
this.id = s.id;
this.name = s.name;
this.model = s.type;
this.manufacturer = "Fibaro";
this.serialNumber = "<unknown>";
this.controlService = new Service.Switch(this.name);
this.characteristics = [Characteristic.On];
}
function FibaroMotionSensorAccessory(platform, s) {
this.platform = platform;
this.remoteAccessory = s;
this.id = s.id;
this.name = s.name;
this.model = "Motion Sensor";
this.manufacturer = "Fibaro";
this.serialNumber = "<unknown>";
this.controlService = new Service.MotionSensor(this.name);
this.characteristics = [Characteristic.MotionDetected];
}
function FibaroTemperatureSensorAccessory(platform, s) {
this.platform = platform;
this.remoteAccessory = s;
this.id = s.id;
this.name = s.name;
this.model = s.type;
this.manufacturer = "Fibaro";
this.serialNumber = "<unknown>";
this.controlService = new Service.TemperatureSensor(this.name);
this.characteristics = [Characteristic.CurrentTemperature];
}
function FibaroDoorSensorAccessory(platform, s) {
this.platform = platform;
this.remoteAccessory = s;
this.id = s.id;
this.name = s.name;
this.model = s.type;
this.manufacturer = "Fibaro";
this.serialNumber = "<unknown>";
this.controlService = new Service.ContactSensor(this.name);
this.characteristics = [Characteristic.ContactSensorState];
}
function FibaroLightSensorAccessory(platform, s) {
this.platform = platform;
this.remoteAccessory = s;
this.id = s.id;
this.name = s.name;
this.model = s.type;
this.manufacturer = "Fibaro";
this.serialNumber = "<unknown>";
this.controlService = new Service.LightSensor(this.name);
this.characteristics = [Characteristic.CurrentAmbientLightLevel];
}
var lastPoll=0;
var pollingUpdateRunning = false;
function startPollingUpdate( platform )
{
if( pollingUpdateRunning )
return;
pollingUpdateRunning = true;
var updateUrl = "http://"+platform.host+"/api/refreshStates?last="+lastPoll;
request.get({
url: updateUrl,
headers : {
"Authorization" : platform.auth
},
json: true
}, function(err, response, json) {
if (!err && response.statusCode == 200) {
if (json != undefined) {
lastPoll = json.last;
if (json.changes != undefined) {
json.changes.map(function(s) {
if (s.value != undefined) {
var value=parseInt(s.value);
if (isNaN(value))
value=(s.value === "true");
for (i=0;i<updateSubscriptions.length; i++) {
var subscription = updateSubscriptions[i];
if (subscription.id == s.id) {
if ((subscription.onOff && typeof(value) == "boolean") || !subscription.onOff)
subscription.characteristic.setValue(value, undefined, 'fromFibaro');
else
subscription.characteristic.setValue(value == 0 ? false : true, undefined, 'fromFibaro');
}
}
}
})
}
}
} else {
platform.log("There was a problem connecting with FibaroHC2.");
}
pollingUpdateRunning = false;
setTimeout( function(){startPollingUpdate(platform)}, 2000 );
});
}
var updateSubscriptions = [];
function subscribeUpdate(characteristic, accessory, onOff)
{
// TODO: optimized management of updateSubscription data structure (no array with sequential access)
updateSubscriptions.push({ 'id': accessory.id, 'characteristic': characteristic, 'accessory': accessory, 'onOff': onOff });
}
module.exports.platform = FibaroHC2Platform;