rnilssoncx / homebridge-pico

Expose Lutron Pico Remotes in Homebridge: https://github.com/nfarina/homebridge
MIT License
63 stars 6 forks source link

Double press not working on up and down buttons #31

Closed lmaced0 closed 2 years ago

lmaced0 commented 2 years ago

Pico: PJ2-3BRL

I have a 10 of these around the house and none of them work with double press on the up and down buttons, only single press and long press work. All other buttons work as expected.

When double pressing this is what I get on the console.

[9/26/2021, 12:15:01 PM] [Pico] [10.0.20.15] Bus Data: ~DEVICE,5,5,3
[9/26/2021, 12:15:01 PM] [Pico] [10.0.20.15] Bus Data: ~DEVICE,5,5,4
[9/26/2021, 12:15:02 PM] [Pico] Little House Pico - Up single press
[9/26/2021, 12:15:02 PM] [Pico] [10.0.20.15] Bus Data: ~DEVICE,5,5,3
[9/26/2021, 12:15:02 PM] [Pico] [10.0.20.15] Bus Data: ~DEVICE,5,5,4
[9/26/2021, 12:15:02 PM] [Pico] Little House Pico - Up single press

Clicktime is set to default (500). Tried tweaking with that number, but no luck. Trying to double press directly on the Lutron app works...

Is this expected?

rnilssoncx commented 2 years ago

I can reproduce this - it should not be doing this. The logs show that the pico is sending the right data...

I'll get this fixed in the next week.

ShaunRoberts543 commented 2 years ago

I am having the same issue with "PJ2-4B-XXX-L31". The two middle buttons will not accept a double press. First and fourth buttons will. Shows all presses in HomeKit.

lmaced0 commented 2 years ago

I can reproduce this - it should not be doing this. The logs show that the pico is sending the right data...

I'll get this fixed in the next week.

Is the fix harder than expected?

sethlong1 commented 2 years ago

Same problem here.

yungsters commented 2 years ago

I investigated this a bit and noticed that the root cause for this bug might be with how the Lutron Pico remote works. I tried instrumenting the where this.timer is assigned, cleared, and when the timeout expires.

_setTimer() {
  this.start = Date.now();
  this.timer = setTimeout(() => {
    this.log(`--- expired after ${Date.now() - this.start}ms`);
    this._finished(false);
  }, this.doubleClickTime);
}

_finished(doubleClick = false) {
  // …
  if (this.timer) {
    this.log(`--- finished after ${Date.now() - this.start}ms`);
    clearTimeout(this.timer);
    this.timer = undefined;
  }
  // …
}

I discovered that when I spam either "On" or "Off" on my PJ2-4B-WH-L01P, I can clear the timer in as little time as 150ms. (Without spamming, a more typical time between click events is 220ms.)

However, when I spam either "Up" or "Down" on the same remote, I can only clear the timer in as little time as 530ms. (Without spamming, a more typical time between click events is 600ms.)

So with the default homebridge-pico configuration of 500ms, the "Up" and "Down" will never trigger a double click gesture. If I configure homebridge-pico to use 1000ms, I can trigger "Up" and "Down" (at the cost of delaying single click gestures by another 500ms).

I am not certain, but my guess is that the delay for "Up" and "Down" exists to detect when someone may be holding the button down (so that Lutron knows to smoothly fade up or down the light brightness instead of firing consecutive discrete events which would make the brightness fade less smoothly).

One suggestion for how to workaround the Pico event delays is to change Click so that if doubleClickTime is a non-zero value, this.doubleClickTime is set to a minimum threshold of either ~300ms or ~700ms.

class Click {
  constructor(event, doubleClickTime, log, callback) {
    // …
    this.doubleClickTime =
      doubleClickTime === 0
        ? 0 // Disabled.
        : Math.max(
            doubleClickTime,
            this.button === '5' || this.button === '6'
              ? 700 // Minimum for "Up" or "Down" events.
              : 300 // Minimum for other button events.
          );
    // …

This would enable the default to still be 500ms (which is nice because it is short and reduces latency for most button types other than "Up" or "Down"), while still working as closely to the configured duration as possible for "Up" and "Down" buttons.

rnilssoncx commented 2 years ago

You are absolutely correct! The up and down are slower (thank goodness, I couldn't see how my code could operate differently for these two..). I believe this is because they can repeat when paired with a Lutron switch. I've added settings and defaults for this, just in case some other problems pop up later. So 5 and 6 will have 250ms added to their timers automatically. This can be adjusted. The logs will now show the timer value being set on each tracker.

Release will be out in a couple hours - just doing some final testing. It will also include repeating long presses. I think this will be very handle when using the Homebridge managed Shortcuts.