mirror of
https://github.com/mtan93/homebridge.git
synced 2026-04-04 14:23:11 +01:00
Virtual device optimization
Managed Fibaro Virtual Devices push buttons with a single HomeKit Bridged Accessory with multiple Switches
This commit is contained in:
@@ -51,43 +51,36 @@ FibaroHC2Platform.prototype = {
|
|||||||
if (s.visible == true) {
|
if (s.visible == true) {
|
||||||
var accessory = null;
|
var accessory = null;
|
||||||
if (s.type == "com.fibaro.multilevelSwitch")
|
if (s.type == "com.fibaro.multilevelSwitch")
|
||||||
accessory = new FibaroAccessory(new Service.Lightbulb(s.name), [Characteristic.On, Characteristic.Brightness]);
|
accessory = new FibaroBridgedAccessory([{controlService: new Service.Lightbulb(s.name), characteristics: [Characteristic.On, Characteristic.Brightness]}]);
|
||||||
else if (s.type == "com.fibaro.FGRM222" || s.type == "com.fibaro.FGR221")
|
else if (s.type == "com.fibaro.FGRM222" || s.type == "com.fibaro.FGR221")
|
||||||
accessory = new FibaroAccessory(new Service.WindowCovering(s.name), [Characteristic.CurrentPosition, Characteristic.TargetPosition, Characteristic.PositionState]);
|
accessory = new FibaroBridgedAccessory([{controlService: new Service.WindowCovering(s.name), characteristics: [Characteristic.CurrentPosition, Characteristic.TargetPosition, Characteristic.PositionState]}]);
|
||||||
else if (s.type == "com.fibaro.binarySwitch" || s.type == "com.fibaro.developer.bxs.virtualBinarySwitch")
|
else if (s.type == "com.fibaro.binarySwitch" || s.type == "com.fibaro.developer.bxs.virtualBinarySwitch")
|
||||||
accessory = new FibaroAccessory(new Service.Switch(s.name), [Characteristic.On]);
|
accessory = new FibaroBridgedAccessory([{controlService: new Service.Switch(s.name), characteristics: [Characteristic.On]}]);
|
||||||
else if (s.type == "com.fibaro.FGMS001" || s.type == "com.fibaro.motionSensor")
|
else if (s.type == "com.fibaro.FGMS001" || s.type == "com.fibaro.motionSensor")
|
||||||
accessory = new FibaroAccessory(new Service.MotionSensor(s.name), [Characteristic.MotionDetected]);
|
accessory = new FibaroBridgedAccessory([{controlService: new Service.MotionSensor(s.name), characteristics: [Characteristic.MotionDetected]}]);
|
||||||
else if (s.type == "com.fibaro.temperatureSensor")
|
else if (s.type == "com.fibaro.temperatureSensor")
|
||||||
accessory = new FibaroAccessory(new Service.TemperatureSensor(s.name), [Characteristic.CurrentTemperature]);
|
accessory = new FibaroBridgedAccessory([{controlService: new Service.TemperatureSensor(s.name), characteristics: [Characteristic.CurrentTemperature]}]);
|
||||||
else if (s.type == "com.fibaro.doorSensor")
|
else if (s.type == "com.fibaro.doorSensor")
|
||||||
accessory = new FibaroAccessory(new Service.ContactSensor(s.name), [Characteristic.ContactSensorState]);
|
accessory = new FibaroBridgedAccessory([{controlService: new Service.ContactSensor(s.name), characteristics: [Characteristic.ContactSensorState]}]);
|
||||||
else if (s.type == "com.fibaro.lightSensor")
|
else if (s.type == "com.fibaro.lightSensor")
|
||||||
accessory = new FibaroAccessory(new Service.LightSensor(s.name), [Characteristic.CurrentAmbientLightLevel]);
|
accessory = new FibaroBridgedAccessory([{controlService: new Service.LightSensor(s.name), characteristics: [Characteristic.CurrentAmbientLightLevel]}]);
|
||||||
else if (s.type == "com.fibaro.FGWP101")
|
else if (s.type == "com.fibaro.FGWP101")
|
||||||
accessory = new FibaroAccessory(new Service.Outlet(s.name), [Characteristic.On, Characteristic.OutletInUse]);
|
accessory = new FibaroBridgedAccessory([{ controlService: new Service.Outlet(s.name), characteristics: [Characteristic.On, Characteristic.OutletInUse]}]);
|
||||||
else if (s.type == "virtual_device") {
|
else if (s.type == "virtual_device" && s.name.charAt(0) != "_") {
|
||||||
|
var services = [];
|
||||||
for (var r = 0; r < s.properties.rows.length; r++) {
|
for (var r = 0; r < s.properties.rows.length; r++) {
|
||||||
if (s.properties.rows[r].type == "button") {
|
if (s.properties.rows[r].type == "button") {
|
||||||
for (var e = 0; e < s.properties.rows[r].elements.length; e++) {
|
for (var e = 0; e < s.properties.rows[r].elements.length; e++) {
|
||||||
var name = s.properties.rows[r].elements[e].caption;
|
var service = {
|
||||||
var virtualButton = new FibaroAccessory(new Service.Switch(name), [Characteristic.On]);
|
controlService: new Service.Switch(s.properties.rows[r].elements[e].caption),
|
||||||
virtualButton.buttonId = s.properties.rows[r].elements[e].id;
|
characteristics: [Characteristic.On]
|
||||||
virtualButton.getServices = function() {
|
};
|
||||||
return that.getServices(this);
|
service.controlService.subtype = s.properties.rows[r].elements[e].id;
|
||||||
};
|
services.push(service);
|
||||||
virtualButton.platform = that;
|
|
||||||
virtualButton.remoteAccessory = s;
|
|
||||||
virtualButton.id = s.id;
|
|
||||||
virtualButton.name = name;
|
|
||||||
virtualButton.model = "Virtual Button";
|
|
||||||
virtualButton.manufacturer = "Fibaro";
|
|
||||||
virtualButton.serialNumber = "<unknown>";
|
|
||||||
foundAccessories.push(virtualButton);
|
|
||||||
that.log("Service name: " + virtualButton.controlService.displayName + ", Accessory name: " + virtualButton.name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
accessory = new FibaroBridgedAccessory(services);
|
||||||
}
|
}
|
||||||
if (accessory != null) {
|
if (accessory != null) {
|
||||||
accessory.getServices = function() {
|
accessory.getServices = function() {
|
||||||
@@ -171,20 +164,22 @@ FibaroHC2Platform.prototype = {
|
|||||||
.setCharacteristic(Characteristic.SerialNumber, homebridgeAccessory.serialNumber);
|
.setCharacteristic(Characteristic.SerialNumber, homebridgeAccessory.serialNumber);
|
||||||
return informationService;
|
return informationService;
|
||||||
},
|
},
|
||||||
bindCharacteristicEvents: function(characteristic, homebridgeAccessory) {
|
bindCharacteristicEvents: function(characteristic, service, homebridgeAccessory) {
|
||||||
var onOff = characteristic.props.format == "bool" ? true : false;
|
var onOff = characteristic.props.format == "bool" ? true : false;
|
||||||
var readOnly = true;
|
var readOnly = true;
|
||||||
for (var i = 0; i < characteristic.props.perms.length; i++)
|
for (var i = 0; i < characteristic.props.perms.length; i++)
|
||||||
if (characteristic.props.perms[i] == "pw")
|
if (characteristic.props.perms[i] == "pw")
|
||||||
readOnly = false;
|
readOnly = false;
|
||||||
var powerValue = (characteristic.UUID == "00000026-0000-1000-8000-0026BB765291") ? true : false;
|
var powerValue = (characteristic.UUID == "00000026-0000-1000-8000-0026BB765291") ? true : false;
|
||||||
subscribeUpdate(characteristic, homebridgeAccessory, onOff);
|
if (service.controlService.subtype != null) {
|
||||||
|
subscribeUpdate(characteristic, homebridgeAccessory, onOff);
|
||||||
|
}
|
||||||
if (!readOnly) {
|
if (!readOnly) {
|
||||||
characteristic
|
characteristic
|
||||||
.on('set', function(value, callback, context) {
|
.on('set', function(value, callback, context) {
|
||||||
if( context !== 'fromFibaro' && context !== 'fromSetValue') {
|
if( context !== 'fromFibaro' && context !== 'fromSetValue') {
|
||||||
if (homebridgeAccessory.buttonId != null) {
|
if (service.controlService.subtype != null) {
|
||||||
homebridgeAccessory.platform.command("pressButton", homebridgeAccessory.buttonId, homebridgeAccessory);
|
homebridgeAccessory.platform.command("pressButton", service.controlService.subtype, homebridgeAccessory);
|
||||||
// In order to behave like a push button reset the status to off
|
// In order to behave like a push button reset the status to off
|
||||||
setTimeout( function(){
|
setTimeout( function(){
|
||||||
characteristic.setValue(false, undefined, 'fromSetValue');
|
characteristic.setValue(false, undefined, 'fromSetValue');
|
||||||
@@ -199,7 +194,7 @@ FibaroHC2Platform.prototype = {
|
|||||||
}
|
}
|
||||||
characteristic
|
characteristic
|
||||||
.on('get', function(callback) {
|
.on('get', function(callback) {
|
||||||
if (homebridgeAccessory.buttonId != null) {
|
if (service.controlService.subtype != null) {
|
||||||
// a push button is normally off
|
// a push button is normally off
|
||||||
callback(undefined, false);
|
callback(undefined, false);
|
||||||
} else {
|
} else {
|
||||||
@@ -208,23 +203,28 @@ FibaroHC2Platform.prototype = {
|
|||||||
}.bind(this) );
|
}.bind(this) );
|
||||||
},
|
},
|
||||||
getServices: function(homebridgeAccessory) {
|
getServices: function(homebridgeAccessory) {
|
||||||
|
var services = [];
|
||||||
var informationService = homebridgeAccessory.platform.getInformationService(homebridgeAccessory);
|
var informationService = homebridgeAccessory.platform.getInformationService(homebridgeAccessory);
|
||||||
for (var i=0; i < homebridgeAccessory.characteristics.length; i++) {
|
services.push(informationService);
|
||||||
var characteristic = homebridgeAccessory.controlService.getCharacteristic(homebridgeAccessory.characteristics[i]);
|
for (var s = 0; s < homebridgeAccessory.services.length; s++) {
|
||||||
if (characteristic == undefined)
|
var service = homebridgeAccessory.services[s];
|
||||||
characteristic = homebridgeAccessory.controlService.addCharacteristic(homebridgeAccessory.characteristics[i]);
|
for (var i=0; i < service.characteristics.length; i++) {
|
||||||
homebridgeAccessory.platform.bindCharacteristicEvents(characteristic, homebridgeAccessory);
|
var characteristic = service.controlService.getCharacteristic(service.characteristics[i]);
|
||||||
}
|
if (characteristic == undefined)
|
||||||
|
characteristic = service.controlService.addCharacteristic(service.characteristics[i]);
|
||||||
return [informationService, homebridgeAccessory.controlService];
|
homebridgeAccessory.platform.bindCharacteristicEvents(characteristic, service, homebridgeAccessory);
|
||||||
|
}
|
||||||
|
services.push(service.controlService);
|
||||||
|
}
|
||||||
|
return services;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function FibaroAccessory(controlService, characteristics) {
|
function FibaroBridgedAccessory(services) {
|
||||||
this.controlService = controlService;
|
this.services = services;
|
||||||
this.characteristics = characteristics;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var lastPoll=0;
|
var lastPoll=0;
|
||||||
var pollingUpdateRunning = false;
|
var pollingUpdateRunning = false;
|
||||||
|
|
||||||
@@ -253,7 +253,7 @@ function startPollingUpdate( platform )
|
|||||||
var value=parseInt(s.value);
|
var value=parseInt(s.value);
|
||||||
if (isNaN(value))
|
if (isNaN(value))
|
||||||
value=(s.value === "true");
|
value=(s.value === "true");
|
||||||
for (i=0;i<updateSubscriptions.length; i++) {
|
for (var i=0;i<updateSubscriptions.length; i++) {
|
||||||
var subscription = updateSubscriptions[i];
|
var subscription = updateSubscriptions[i];
|
||||||
if (subscription.id == s.id) {
|
if (subscription.id == s.id) {
|
||||||
if (s.power != undefined && subscription.characteristic.UUID == "00000026-0000-1000-8000-0026BB765291") {
|
if (s.power != undefined && subscription.characteristic.UUID == "00000026-0000-1000-8000-0026BB765291") {
|
||||||
@@ -285,4 +285,3 @@ function subscribeUpdate(characteristic, accessory, onOff)
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports.platform = FibaroHC2Platform;
|
module.exports.platform = FibaroHC2Platform;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user