nfarina / homebridge-dummy

Dummy switches for Homebridge: https://github.com/nfarina/homebridge
277 stars 83 forks source link

Delay reset #82

Closed mkz212 closed 1 year ago

mkz212 commented 1 year ago

Hi. Delay reset should be on every switch change. E.G. I have switch that is ON and have automation that turn it ON (to start delay from begining and extend time).

So this function should be:

DummySwitch.prototype._setOn = function(on, callback) {

  var delay = this.random ? randomize(this.time) : this.time;
  var msg = "Setting switch to " + on
  if (this.random && !this.stateful) {
      if (on && !this.reverse || !on && this.reverse) {
        msg = msg + " (random delay " + delay + "ms)"
      }
  }
  if( ! this.disableLogging ) {
      this.log(msg);
  }

  if (this.resettable) {
    clearTimeout(this.timer);
  }

  if (on && !this.reverse && !this.stateful) {  
    this.timer = setTimeout(function() {
      this._service.setCharacteristic(Characteristic.On, false);
    }.bind(this), delay);
  } else if (!on && this.reverse && !this.stateful) {   
    this.timer = setTimeout(function() {
      this._service.setCharacteristic(Characteristic.On, true);
    }.bind(this), delay);
  }

  if (this.stateful) {
    this.storage.setItemSync(this.name, on);
  }

  callback();
}

and description of Resettable should be:

"description": "The timer will reset each time the switch's state is changed, from on to off, from off to on, and also starting the switch when is already on and disabling the switch when is already off."

mkz212 commented 1 year ago

included in #84