ebaauw / homebridge-nb

Homebridge plugin for Nuki Bridge
Apache License 2.0
51 stars 3 forks source link

lock state not updating properly #132

Closed switey closed 5 months ago

switey commented 5 months ago

my setup is Nuki smart lock 4.0, Nuki Bridge and Nuki opener homekit hub is Apple TV 4K 2nd gen

after unlocking the door lock it remains in "unlocked" state indefinitely even though the lock is auto-locked after a minute restarting homebridge or just home bridge-nb childbridge sometimes fixes the issue

ebaauw commented 5 months ago

Does the lock state change in the Nuki app?

Does the lock state change after you select Identify on the lock accessory in HomeKit? This causes Homebridge NB to ask the Nuki bridge to contact the smart lock and update its cache of the lock state.

Do you see any errors or warnings in the Homebridge log?

Please set Log Level on the Nuki bridge accessory to 2, run Homebridge in DEBUG mode, and capture and attach the Homebridge log file, from restarting the child bridge, through unlocking, waiting for the auto-lock and waiting a couple of minutes.

switey commented 5 months ago

hi, thank you for a swift reply to be honest I don't really see 'identify' button in HomeKit Nuki app consistently shows lock state, doesn't matter if lock is auto (un)locked, or changes state via manual (un)lock through Nuki app or homekit as for homebridge log file, I will try to reproduce the error, for now lock state is updated properly (the bug doesnt happen all the time) can you confirm port number that should be used in homebridge nb settings for bridge communication?

ebaauw commented 5 months ago

You might need to use Eve or another decent HomeKit app to find Identify; Apple's Home app doesn't support all of HomeKit.

Homebridge NB calls the API of the Nuki bridge. My bridge exposes this API on port 8080, which, I think, is the default. Not even sure if that can be changed. It is returned by nb discover.

In addition, Homebridge NB provides a web server for callbacks from the Nuki bridge. By default this uses a random port (>= 1024), but you can change this under the Advanced settings. Homebridge NB issues a log message on startup, which shows the port: "listening on http://0.0.0.0:45969/notify".

switey commented 5 months ago

here's and excerpt of homebridge log Here's what happened: about 5:10 PM I left the house using lock'n'go which is set to lock the door after 20 seconds the attached log fragment shows that homebridge nb still thinks the door is unlocked at 5:41 PM, it changed back to 'locked' when I re-entered the house at around 5:48 PM and Nuki re-locked after auto-opening.

[4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: request 147: GET /callback/list

[4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: request 147: 200 OK [4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: subscription: > {"id":0,"url":"http://192.168.0.8:62330/notify/689A091E"} [4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: request 148: GET /info [4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: request 148: 200 OK [4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: bridge: {"bridgeType":1,"ids":> {"hardwareId":1013065235,"serverId":1754925342},"versions":> {"firmwareVersion":"2.17.0","wifiFirmwareVersion":"2.4.0"},"uptime":293370,"currentTime":"2024-04-02T15:42:02+00:00","wlanConnected":true,"serverConnected":true,"scanResults":> [{"deviceType":2,"nukiId":930999314,"name":"Nuki_Opener_377DEC12","rssi":-68,"paired":true},> {"deviceType":4,"nukiId":1015571181,"name":"Nuki_3C8862ED","rssi":-54,"paired":true}]} [4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: Last Updated: set to "Tue Apr 02 2024 17:42:02" (from "Tue Apr 02 2024 17:41:02") [4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: request 149: GET /list [4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: request 149: 200 OK [4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: device: > {"deviceType":4,"nukiId":1015571181,"name":"Augustowska","firmwareVersion":"4.1.8","lastKnownState":{"mode":2,"state":6,"stateName":"unlocked (lock 'n' go)","batteryCritical":false,"batteryCharging":false,"batteryChargeState":100,"timestamp":"2024-04-02T15:08:33+00:00"}} [4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: device: > {"deviceType":2,"nukiId":930999314,"name":"Domofon","firmwareVersion":"1.10.1","lastKnownState":> {"mode":3,"state":1,"stateName":"online","batteryCritical":false,"ringactionTimestamp":"2024-04-02T12:49:39+00:00","ringactionState":false,"timestamp":"2024-04-02T12:49:49+00:00"}}

ebaauw commented 5 months ago

The Nuki bridge reports it as unlocked (with lock 'n' go enabled):

[4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: request 149: GET /list
[4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: request 149: 200 OK
[4/2/2024, 5:41:57 PM] [Nuki] Nuki_Bridge_689A091E: device: > {"deviceType":4,"nukiId":1015571181,"name":"Augustowska","firmwareVersion":"4.1.8","lastKnownState":{"mode":2,"state":6,"stateName":"unlocked (lock 'n' go)","batteryCritical":false,"batteryCharging":false,"batteryChargeState":100,"timestamp":"2024-04-02T15:08:33+00:00"}}

Note the timestamp of the last known state (by the Nuki bridge): it hasn't updated since 15:08 (UTC). I've never used lock 'n' go myself, but I would expect the lock to report state 1, locked, after it auto locks. If it does, it doesn't seem to inform the Nuki bridge.

As I asked before, could you please force the Nuki bridge to update the last known state, by issuing Identify on the HomeKit accessory (as exposed by Homebridge NB), or by issuing nb lockState 1015571181 4 from the command line. That should at least update the timestamp, if not the state. You can run nb list to confirm, or wait for Homebridge NB to poll the Nuki bridge.

ebaauw commented 5 months ago

Just tried myself (on my SmartLock 4): press twice to activate lock 'n' go with the door open. On closing the door (I have a door sensor), the SmartLock locks. The HomeKit accessory, as exposed by the lock itself over Matter, switches state immediately. The Nuki bridge (and, consequently, the accessory as exposed by Homebridge NB) continues to report unlocked. On Identify, the bridge refreshes its cached state and now reports locked, causing Homebridge NB to update the accessory.

I would quality this as a Nuki bug, probably in the Nuki smart lock firmware (it should inform the bridge of the new state).

switey commented 5 months ago

that's a shame any way you can work around it? the funny thing is, my lock 4.0 doesn't work with HomeKit over matter natively, it's been buggy since I got it. people over at Nuki seem helpless. I've found your plugin to make the lock work somehow with my setup

ebaauw commented 5 months ago

Could you try beta v1.4.17-1?

This version forces the Nuki bridge to refresh the smart lock's state, when it seems stuck on: unlocked (lock 'n' go), locking, unlocking, or unlatching.

I daren't do a regular force-refresh as this would drain the smart lock's battery.

switey commented 5 months ago

hi! thanks again for your cooperation. I'm one test in aaaaand it worked! will continue to test it with lock'n'go and report back in a day or two. this is my default way of leaving the house so I'm really happy with the progress cheers

ebaauw commented 5 months ago

The beta seemed to be running fine in my house; I published v1.4.17.

switey commented 5 months ago

Now the lock seems to be stuck on regular unlock