Feilner / homebridge-plc

Homebridge plugin for Siemens Step7 and compatible PLCs
MIT License
12 stars 3 forks source link

Added invert configuration option for OccupancySensors, MotionSensors and ContactSensors. #24

Closed sebfia closed 3 years ago

sebfia commented 3 years ago

Sometimes simple 'bit' sensors are being tracked the other way round within S7 and there is no chance to change the functions that write DB values. Therefore a configuration option of simply swapping the bit would be great. This pull request adds this feature for OccupancySensors, MotionSensors and ContactSensors.

sebfia commented 3 years ago

Btw. I have tested my PR in real life.

sebfia commented 3 years ago

You are right. As I am only using the poll functionality I completely overlooked to change the push code as well. I will implement your suggestions in the evening today!

Am 02.01.2021 um 06:04 schrieb Feilner notifications@github.com:

 @Feilner requested changes on this pull request.

The PUSH handling also needs to be adapted. I didn't managed to suggest the change on lines where you have not edited.

Line 1070-1071

    this.log.debug( "[" + this.name + "] Push OccupancyDetected:" + String(this.modFunctionGet(parseInt(value))) + "<-" + String(value));
    this.service.getCharacteristic(Characteristic.OccupancyDetected).updateValue(this.modFunctionGet(parseInt(value)));

and line 1081-1082

    this.log.debug( "[" + this.name + "] Push MotionDetected:" + String(this.modFunctionGet(parseInt(value))) + "<-" + String(value));
    this.service.getCharacteristic(Characteristic.MotionDetected).updateValue(this.modFunctionGet(parseInt(value)));

In index.js:

@@ -1617,6 +1635,10 @@ GenericPLCAccessory.prototype = { return 100-value; },

  • invert_bit: function(value) {
  • return 1 - value; As in theory true is anything which is not zero I would prefer this.

⬇️ Suggested change

  • return 1 - value;
  • return (value ? 0 : 1); In index.js:

@@ -522,11 +522,17 @@ function GenericPLCAccessory(platform, config, accessoryNumber) { this.service = new Service.OccupancySensor(this.name); this.accessory.addService(this.service);

  • this.modBitGet = this.plain To be in line with the naming of other accessories and adding the missing ;

⬇️ Suggested change

  • this.modBitGet = this.plain
  • this.modFunctionGet= this.plain; In index.js:

@@ -522,11 +522,17 @@ function GenericPLCAccessory(platform, config, accessoryNumber) { this.service = new Service.OccupancySensor(this.name); this.accessory.addService(this.service);

  • this.modBitGet = this.plain
  • if ('invert' in config && config.invert) {
  • this.modBitGet = this.invert_bit; ⬇️ Suggested change
  • this.modBitGet = this.invert_bit;
  • this.modFunctionGet= this.invert_bit; In index.js:
 this.service.getCharacteristic(Characteristic.OccupancyDetected)

.on('get', function(callback) {this.getBit(callback, config.db, Math.floor(config.get_OccupancyDetected), Math.floor((config.get_OccupancyDetected*10)%10),

  • "get OccupancyDetected"
  • "get OccupancyDetected",
  • this.modBitGet ⬇️ Suggested change
  • this.modBitGet
  • this.modFunctionGet In index.js:

@@ -536,11 +542,17 @@ function GenericPLCAccessory(platform, config, accessoryNumber) { this.service = new Service.MotionSensor(this.name); this.accessory.addService(this.service);

  • this.modBitGet = this.plain ⬇️ Suggested change
  • this.modBitGet = this.plain
  • this.modFunctionGet= this.plain; In index.js:

@@ -536,11 +542,17 @@ function GenericPLCAccessory(platform, config, accessoryNumber) { this.service = new Service.MotionSensor(this.name); this.accessory.addService(this.service);

  • this.modBitGet = this.plain
  • if ('invert' in config && config.invert) {
  • this.modBitGet = this.invert_bit; ⬇️ Suggested change
  • this.modBitGet = this.invert_bit;
  • this.modFunctionGet= this.invert_bit; In index.js:
 this.service.getCharacteristic(Characteristic.MotionDetected)

.on('get', function(callback) {this.getBit(callback, config.db, Math.floor(config.get_MotionDetected), Math.floor((config.get_MotionDetected*10)%10),

  • "get MotionDetected"
  • "get MotionDetected",
  • this.modBitGet ⬇️ Suggested change
  • this.modBitGet
  • this.modFunctionGet — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.
sebfia commented 3 years ago

Changes made as requested

Feilner commented 3 years ago

Thanks for your pull request.

sebfia commented 3 years ago

Thank you for merging!