ebaauw / homebridge-deconz

Homebridge plugin for deCONZ
Apache License 2.0
135 stars 7 forks source link

Switch error in automations #139

Closed Nastras closed 1 year ago

Nastras commented 1 year ago

Hello Erik,

I have noticed a very strange behavior and think it is a bug in the plugin. I would like to know what you think about it. I have two round Tradfri switches. With these I open my garage door. For this I have created a simple automation with the event:

Button pressed = open garage door

The error is when I restart homebridge-deconz my garage door is opened.

I have twice already unintentionally had an open garage door in the night until I noticed this behavior. After disabling the automation I was able to restart homebridge-deconz without the garage door opening.

It looks like when restarting homebridge-deconz the button of the switch in the plugin is somehow triggered and thus the automation is started.

Is this possible? Do you need my log file?

Best regards

ebaauw commented 1 year ago

Afaik, (re-)starting Homebridge deCONZ won't cause any programmable switch event. Do you see any "Programmable Switch Event" message in the log on startup?

ebaauw commented 1 year ago

Hm, Home flashes Single Press on multiple buttons for different stateless switches, when Homebridge deCONZ starts. Running Homebridge with HAP-NodeJS debugging shows:

[5/7/2023, 3:21:42 PM] [deCONZ] pi5: Hue tap dial switch: add accessory
[5/7/2023, 3:21:42 PM] [deCONZ] Hue tap dial switch: uiPort: set to 59186 (from 58998)
[5/7/2023, 3:21:42 PM] [deCONZ] Hue tap dial switch: Signify Netherlands B.V. RDM002 v2.59.25 (2 resources)
[5/7/2023, 3:21:42 PM] [deCONZ] Hue tap dial switch: 2 resources: /sensors/81, /sensors/82
[5/7/2023, 3:21:42 PM] [deCONZ] Hue tap dial switch: /sensors/81: capabilities: {"buttons":{"1":{"label":"1","events":5,"hasRepeat":true},"2":{"label":"2","events":5,"hasRepeat":true},"3":{"label":"3","events":5,"hasRepeat":true},"4":{"label":"4","events":5,"hasRepeat":true}},"namespace":0}
[5/7/2023, 3:21:42 PM] [deCONZ] Hue tap dial switch: /sensors/81: params: {"primaryService":true,"serviceName":"Label"}
  HAP-NodeJS:EventedHTTPServer:Connection [::ffff:192.168.76.36] Sending HAP event notifications [ { aid: 24, iid: 16, value: 0 } ] +995ms
  HAP-NodeJS:EventedHTTPServer:Connection [::ffff:192.168.76.36] Sending HAP event notifications [ { aid: 24, iid: 21, value: 0 } ] +0ms
  HAP-NodeJS:EventedHTTPServer:Connection [::ffff:192.168.76.36] Sending HAP event notifications [ { aid: 24, iid: 26, value: 0 } ] +1ms
  HAP-NodeJS:EventedHTTPServer:Connection [::ffff:192.168.76.36] Sending HAP event notifications [ { aid: 24, iid: 31, value: 0 } ] +0ms
[5/7/2023, 3:21:42 PM] [deCONZ] Hue tap dial switch: /sensors/82: capabilities: {"buttons":{"5":{"label":"Right Turn","events":1},"6":{"label":"Left Turn","events":1}},"namespace":0}
[5/7/2023, 3:21:42 PM] [deCONZ] Hue tap dial switch: /sensors/82: params: {"serviceName":"Label"}
  HAP-NodeJS:EventedHTTPServer:Connection [::ffff:192.168.76.36] Sending HAP event notifications [ { aid: 24, iid: 42, value: 0 } ] +2ms
  HAP-NodeJS:EventedHTTPServer:Connection [::ffff:192.168.76.36] Sending HAP event notifications [ { aid: 24, iid: 47, value: 0 } ] +0ms
[5/7/2023, 3:21:42 PM] [deCONZ] Hue tap dial switch: subscribe to /sensors/81
[5/7/2023, 3:21:42 PM] [deCONZ] Hue tap dial switch: subscribe to /sensors/82
[5/7/2023, 3:21:42 PM] [deCONZ] Hue tap dial switch: initialised

These are indeed the iids of the Programmable Switch Event characteristics of the 6 Stateless Programmable Switch services for the Hue tap dial switch. For reference, when actually pressing the buttons (turning the dial):

[5/7/2023, 3:28:48 PM] [deCONZ] Hue tap dial switch: /sensors/81: changed: {"lastseen":"2023-05-07T13:28Z"}
[5/7/2023, 3:28:48 PM] [deCONZ] Hue tap dial switch: /sensors/81: changed: {"state":{"buttonevent":1000,"eventduration":0,"lastupdated":"2023-05-07T13:28:48.134"}}
[5/7/2023, 3:28:48 PM] [deCONZ] Hue tap dial switch: /sensors/82: changed: {"lastseen":"2023-05-07T13:28Z"}
[5/7/2023, 3:28:48 PM] [deCONZ] Hue tap dial switch: /sensors/81: changed: {"state":{"buttonevent":1002,"eventduration":1,"lastupdated":"2023-05-07T13:28:48.254"}}
[5/7/2023, 3:28:48 PM] [deCONZ] Hue tap dial switch 1: Programmable Switch Event: Single Press
  HAP-NodeJS:EventedHTTPServer:Connection [::ffff:192.168.76.36] Sending HAP event notifications [ { aid: 24, iid: 16, value: 0 } ] +4m
[5/7/2023, 3:29:00 PM] [deCONZ] Hue tap dial switch: /sensors/81: changed: {"state":{"buttonevent":2000,"eventduration":0,"lastupdated":"2023-05-07T13:29:00.069"}}
[5/7/2023, 3:29:00 PM] [deCONZ] Hue tap dial switch: /sensors/81: changed: {"state":{"buttonevent":2002,"eventduration":1,"lastupdated":"2023-05-07T13:29:00.249"}}
[5/7/2023, 3:29:00 PM] [deCONZ] Hue tap dial switch 2: Programmable Switch Event: Single Press
  HAP-NodeJS:EventedHTTPServer:Connection [::ffff:192.168.76.36] Sending HAP event notifications [ { aid: 24, iid: 21, value: 0 } ] +12s
[5/7/2023, 3:29:01 PM] [deCONZ] Hue tap dial switch: /sensors/81: changed: {"state":{"buttonevent":3000,"eventduration":0,"lastupdated":"2023-05-07T13:29:01.416"}}
[5/7/2023, 3:29:01 PM] [deCONZ] Hue tap dial switch: /sensors/81: changed: {"state":{"buttonevent":3002,"eventduration":1,"lastupdated":"2023-05-07T13:29:01.569"}}
[5/7/2023, 3:29:01 PM] [deCONZ] Hue tap dial switch 3: Programmable Switch Event: Single Press
  HAP-NodeJS:EventedHTTPServer:Connection [::ffff:192.168.76.36] Sending HAP event notifications [ { aid: 24, iid: 26, value: 0 } ] +1s
[5/7/2023, 3:29:02 PM] [deCONZ] Hue tap dial switch: /sensors/81: changed: {"state":{"buttonevent":4000,"eventduration":0,"lastupdated":"2023-05-07T13:29:02.277"}}
[5/7/2023, 3:29:02 PM] [deCONZ] Hue tap dial switch: /sensors/81: changed: {"state":{"buttonevent":4002,"eventduration":1,"lastupdated":"2023-05-07T13:29:02.423"}}
[5/7/2023, 3:29:02 PM] [deCONZ] Hue tap dial switch 4: Programmable Switch Event: Single Press
  HAP-NodeJS:EventedHTTPServer:Connection [::ffff:192.168.76.36] Sending HAP event notifications [ { aid: 24, iid: 31, value: 0 } ] +860ms
[5/7/2023, 3:29:04 PM] [deCONZ] Hue tap dial switch: /sensors/82: changed: {"state":{"expectedrotation":30,"lastupdated":"2023-05-07T13:29:04.445","rotaryevent":1}}
[5/7/2023, 3:29:04 PM] [deCONZ] Hue tap dial switch Right Turn: Programmable Switch Event: Single Press
  HAP-NodeJS:EventedHTTPServer:Connection [::ffff:192.168.76.36] Sending HAP event notifications [ { aid: 24, iid: 42, value: 0 } ] +2s
[5/7/2023, 3:29:04 PM] [deCONZ] Hue tap dial switch: /sensors/82: changed: {"state":{"expectedrotation":136,"lastupdated":"2023-05-07T13:29:04.851","rotaryevent":2}}
[5/7/2023, 3:29:04 PM] [deCONZ] Hue tap dial switch Right Turn: Programmable Switch Event: Single Press
  HAP-NodeJS:EventedHTTPServer:Connection [::ffff:192.168.76.36] Sending HAP event notifications [ { aid: 24, iid: 42, value: 0 } ] +405ms
[5/7/2023, 3:29:05 PM] [deCONZ] Hue tap dial switch: /sensors/82: changed: {"state":{"expectedrotation":-45,"lastupdated":"2023-05-07T13:29:05.951","rotaryevent":1}}
[5/7/2023, 3:29:05 PM] [deCONZ] Hue tap dial switch Left Turn: Programmable Switch Event: Single Press
  HAP-NodeJS:EventedHTTPServer:Connection [::ffff:192.168.76.36] Sending HAP event notifications [ { aid: 24, iid: 47, value: 0 } ] +1s
[5/7/2023, 3:29:12 PM] [deCONZ] Hue tap dial switch: /sensors/81: polled: {"config":{"battery":100,"on":true,"reachable":true},"etag":"84f052a24766edb8a7071149b4fad6ab","lastannounced":"20

So HAP-NodeJS sends button events for all buttons on startup. It seems to be related to restoring the accessory from cachedAccessories. Not sure if this is caused by Homebridge deCONZ, need to test some more.

Nastras commented 1 year ago

Hey thanks for the quick feedback. Good to hear that it is indeed a bug. I hope it can be fixed as the behavior is a real safety risk 😅.

ebaauw commented 1 year ago

It's actually caused by homebridge-lib, when re-creating the characteristic delegate for a Programmable Switch Event that's restored from cachedAccessories: https://github.com/ebaauw/homebridge-lib/blob/c27d70f070eefb1b5993440bcc946b31867532de/lib/CharacteristicDelegate.js#L162-L168

This code restores the characteristic value from cached value (by homebridge-lib) in the accessory context. Not sure what I was thinking when I wrote that, as the characteristic value is already restored from the serialised characteristic (by Homebridge). Maybe this should happen only, when the value is first initialised from params.value. Anyway, adding a condition !this._notifyOnly to the second ifprevents the update of the Programmable Switch Event value, and the corresponding notification by HAP-NodeJS.

ebaauw commented 1 year ago

Should be fixed in v1.1.15.

Nastras commented 1 year ago

Installed and tested with very good! Problem fixed. Thanks!!!