dauden1184 / RaspiNukiBridge

Simple Nuki Bridge implementation using asyncio
GNU General Public License v3.0
48 stars 19 forks source link

can't connect to v2 and v3 #14

Closed t0bse closed 1 year ago

t0bse commented 2 years ago

Raspberry Pi 3B tested Nuki v2 and v3

v2:

2022-09-10 15:28:39.562|I|__main__.py:252|Generatig keys for Nuki 54xxxx
2022-09-10 15:28:39.564|I|__main__.py:256|bridge_public_key: 545b2868fb64e24056ef0456ce0134e1d740142ab10bd66bdc9d2bcac1661618
2022-09-10 15:28:39.564|I|__main__.py:257|bridge_private_key: 0d03e2eb626e69212ff00e24d23896af634af751d4a168e6eade2d6fe6d35c81
2022-09-10 15:28:39.567|I|nuki.py:173|Stop scanning
2022-09-10 15:28:39.568|I|nuki.py:543|Nuki connecting
2022-09-10 15:28:48.422|I|nuki.py:247|Device type: DeviceType.SMARTLOCK_1_2
2022-09-10 15:28:48.510|I|nuki.py:555|Connected
2022-09-10 15:28:48.555|E|nuki.py:429|Error 16
2022-09-10 15:28:48.556|I|nuki.py:564|Nuki disconnecting
2022-09-10 15:28:50.815|I|nuki.py:169|Start scanning
2022-09-10 15:28:52.881|I|nuki.py:185|Nuki: 54:xxxxx, RSSI: -81 AdvertisementData(local_name='Nuki_23F9DAB4', manufacturer_data={76: b'\x02\x15\xa9.\xe2\x00U\x01\x11\xe4\x91l\x08\x00 \x0c\x9af#\xf9\xda\xb4\xc5'}, service_uuids=['0000003e-0000-1000-8000-0026bb765291', '00000044-0000-1000-8000-0026bb765291', '00000045-0000-1000-8000-0026bb765291', '00000055-0000-1000-8000-0026bb765291', '00000096-0000-1000-8000-0026bb765291', '000000a2-0000-1000-8000-0026bb765291', '00001800-0000-1000-8000-00805f9b34fb', '0000180a-0000-1000-8000-00805f9b34fb', 'a92ee100-5501-11e4-916c-0800200c9a66', 'a92ee200-5501-11e4-916c-0800200c9a66'])
2022-09-10 15:28:52.882|I|nuki.py:572|Updating nuki state
Task exception was never retrieved
future: <Task finished name='Task-16' coro=<NukiManager._detected_ibeacon() done, defined at /home/pi/RaspiNukiBridge/./nuki.py:179> exception=TypeError("unsupported operand type(s) for +: 'NoneType' and 'bytes'")>
Traceback (most recent call last):
  File "/home/pi/RaspiNukiBridge/./nuki.py", line 197, in _detected_ibeacon
    await nuki.update_state()
  File "/home/pi/RaspiNukiBridge/./nuki.py", line 575, in update_state
    cmd = self._encrypt_command(NukiCommand.REQUEST_DATA.value, payload)
  File "/home/pi/RaspiNukiBridge/./nuki.py", line 273, in _encrypt_command
    unencrypted = self.auth_id + self._prepare_command(cmd_code, payload)[:-2]
TypeError: unsupported operand type(s) for +: 'NoneType' and 'bytes'

v3:

2022-09-10 15:25:59.607|I|__main__.py:252|Generatig keys for Nuki 54:xxxxx
2022-09-10 15:25:59.608|I|__main__.py:256|bridge_public_key: ccf5f46fa0815eadf8dc545e3b864f82b65fd0aa7a5bc62f8f8db567674aaf09
2022-09-10 15:25:59.608|I|__main__.py:257|bridge_private_key: 25d9eb5e75cc57d117fb2984d498f33325e8846c6042939c36987395cd482ed5
2022-09-10 15:25:59.611|I|nuki.py:173|Stop scanning
2022-09-10 15:25:59.612|I|nuki.py:543|Nuki connecting
Task exception was never retrieved
future: <Task finished name='Task-1' coro=<Nuki.pair() done, defined at /home/pi/RaspiNukiBridge/./nuki.py:612> exception=BleakDBusError('org.bluez.Error.Failed', 'Software caused connection abort')>
Traceback (most recent call last):
  File "/home/pi/RaspiNukiBridge/./nuki.py", line 617, in pair
    await self.connect()
  File "/home/pi/RaspiNukiBridge/./nuki.py", line 544, in connect
    await self._client.connect()
  File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/bluezdbus/client.py", line 278, in connect
    assert_reply(reply)
  File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/bluezdbus/utils.py", line 23, in assert_reply
    raise BleakDBusError(reply.error_name, reply.body)
bleak.exc.BleakDBusError: [org.bluez.Error.Failed] Software caused connection abort
^CTraceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/pi/RaspiNukiBridge/./__main__.py", line 268, in <module>
    loop.run_forever()
  File "/usr/lib/python3.9/asyncio/base_events.py", line 596, in run_forever
    self._run_once()
  File "/usr/lib/python3.9/asyncio/base_events.py", line 1854, in _run_once
    event_list = self._selector.select(timeout)
  File "/usr/lib/python3.9/selectors.py", line 469, in select
    fd_event_list = self._selector.poll(timeout, max_ev)
Jayknightfr commented 2 years ago

You need to put the nuki in pairing mode before

t0bse commented 2 years ago

You need to put the nuki in pairing mode before

i know. did that with both for a few times. pairing mode is active

Jayknightfr commented 2 years ago

OK so I had the same issue as you and it's not because of the pairing mode (I had that too, my bad) but because you're using python 3.9. You have to use to 3.7

t0bse commented 2 years ago

connecting works with 3.7, but i get the "bluez" error.

pi@raspberrypi:~/RaspiNukiBridge $ python . --unlock
2022-10-01 14:49:05.254|I|nuki.py:586|Unlocking
2022-10-01 14:49:05.256|I|nuki.py:173|Stop scanning
2022-10-01 14:49:05.256|I|nuki.py:543|Nuki connecting
2022-10-01 14:49:15.948|E|nuki.py:523|Error: <class 'bleak.exc.BleakDBusError'> [org.bluez.Error.Failed] Software caused connection abort
Traceback (most recent call last):
  File "/home/pi/RaspiNukiBridge/./nuki.py", line 517, in _send_data
    await self.connect()
  File "/home/pi/RaspiNukiBridge/./nuki.py", line 544, in connect
    await self._client.connect()
  File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/bluezdbus/client.py", line 278, in connect
    assert_reply(reply)
  File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/bluezdbus/utils.py", line 23, in assert_reply
    raise BleakDBusError(reply.error_name, reply.body)
bleak.exc.BleakDBusError: [org.bluez.Error.Failed] Software caused connection abort
2022-10-01 14:49:16.959|I|nuki.py:173|Stop scanning
2022-10-01 14:49:16.960|I|nuki.py:543|Nuki connecting
^CTraceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/pi/RaspiNukiBridge/./__main__.py", line 274, in <module>
    asyncio.run(device.unlock())
  File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 629, in run_until_complete
    self.run_forever()
  File "/usr/lib/python3.9/asyncio/base_events.py", line 596, in run_forever
    self._run_once()
  File "/usr/lib/python3.9/asyncio/base_events.py", line 1854, in _run_once
    event_list = self._selector.select(timeout)
  File "/usr/lib/python3.9/selectors.py", line 469, in select
    fd_event_list = self._selector.poll(timeout, max_ev)
KeyboardInterrupt

and after downgrading bluez to 5.50-1.2~deb10u2 armhf it's still not working:

2022-10-01 14:44:36.040|I|nuki.py:586|Unlocking
2022-10-01 14:44:36.042|I|nuki.py:173|Stop scanning
2022-10-01 14:44:36.043|I|nuki.py:543|Nuki connecting
2022-10-01 14:44:36.071|E|nuki.py:523|Error: <class 'bleak.exc.BleakDBusError'> [org.freedesktop.DBus.Error.AccessDenied] Rejected send message, 2 matched rules; type="method_call", sender=":1.34" (uid=1000 pid=1200 comm="python3.9 . --unlock ") interface="org.freedesktop.DBus.ObjectManager" member="GetManagedObjects" error name="(unset)" requested_reply="0" destination="org.bluez" (uid=0 pid=509 comm="/usr/lib/bluetooth/bluetoothd ")
Traceback (most recent call last):
  File "/home/pi/RaspiNukiBridge/./nuki.py", line 517, in _send_data
    await self.connect()
  File "/home/pi/RaspiNukiBridge/./nuki.py", line 544, in connect
    await self._client.connect()
  File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/bluezdbus/client.py", line 112, in connect
    device = await BleakScannerBlueZDBus.find_device_by_address(
  File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/scanner.py", line 220, in find_device_by_address
    return await cls.find_device_by_filter(
  File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/scanner.py", line 249, in find_device_by_filter
    async with cls(detection_callback=apply_filter, **kwargs):
  File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/scanner.py", line 95, in __aenter__
    await self.start()
  File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/bluezdbus/scanner.py", line 133, in start
    assert_reply(reply)
  File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/bluezdbus/utils.py", line 23, in assert_reply
    raise BleakDBusError(reply.error_name, reply.body)
bleak.exc.BleakDBusError: [org.freedesktop.DBus.Error.AccessDenied] Rejected send message, 2 matched rules; type="method_call", sender=":1.34" (uid=1000 pid=1200 comm="python3.9 . --unlock ") interface="org.freedesktop.DBus.ObjectManager" member="GetManagedObjects" error name="(unset)" requested_reply="0" destination="org.bluez" (uid=0 pid=509 comm="/usr/lib/bluetooth/bluetoothd ")
Jayknightfr commented 2 years ago

Logs say you're still using python 3.9

Le sam. 1 oct. 2022, 15:45, t0bse @.***> a écrit :

connecting works with 3.7, but i get the "bluez" error. and after downgrading it's still not working:

``2022-10-01 14:44:36.040|I|nuki.py:586|Unlocking 2022-10-01 14:44:36.042|I|nuki.py:173|Stop scanning 2022-10-01 14:44:36.043|I|nuki.py:543|Nuki connecting 2022-10-01 14:44:36.071|E|nuki.py:523|Error: <class 'bleak.exc.BleakDBusError'> [org.freedesktop.DBus.Error.AccessDenied] Rejected send message, 2 matched rules; type="method_call", sender=":1.34" (uid=1000 pid=1200 comm="python3.9 . --unlock ") interface="org.freedesktop.DBus.ObjectManager" member="GetManagedObjects" error name="(unset)" requested_reply="0" destination="org.bluez" (uid=0 pid=509 comm="/usr/lib/bluetooth/bluetoothd ") Traceback (most recent call last): File "/home/pi/RaspiNukiBridge/./nuki.py", line 517, in _send_data await self.connect() File "/home/pi/RaspiNukiBridge/./nuki.py", line 544, in connect await self._client.connect() File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/bluezdbus/client.py", line 112, in connect device = await BleakScannerBlueZDBus.find_device_by_address( File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/scanner.py", line 220, in find_device_by_address return await cls.find_device_by_filter( File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/scanner.py", line 249, in find_device_by_filter async with cls(detection_callback=apply_filter, *kwargs): File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/scanner.py", line 95, in aenter* await self.start() File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/bluezdbus/scanner.py", line 133, in start assert_reply(reply) File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/bluezdbus/utils.py", line 23, in assert_reply raise BleakDBusError(reply.error_name, reply.body) bleak.exc.BleakDBusError: [org.freedesktop.DBus.Error.AccessDenied] Rejected send message, 2 matched rules; type="method_call", sender=":1.34" (uid=1000 pid=1200 comm="python3.9 . --unlock ") interface="org.freedesktop.DBus.ObjectManager" member="GetManagedObjects" error name="(unset)" requested_reply="0" destination="org.bluez" (uid=0 pid=509 comm="/usr/lib/bluetooth/bluetoothd ")

— Reply to this email directly, view it on GitHub https://github.com/dauden1184/RaspiNukiBridge/issues/14#issuecomment-1264369877, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEVCK3YC7WHY6K5VMXWPJXLWBA6A3ANCNFSM6AAAAAAQJLWSSI . You are receiving this because you commented.Message ID: @.***>

t0bse commented 2 years ago

Logs say you're still using python 3.9 Le sam. 1 oct. 2022, 15:45, t0bse @.***> a écrit :

oops sorry thank you. but python 3.7... and bluez Version: 5.50-1.2~deb10u2

pi@raspberrypi:~/RaspiNukiBridge $ python . --lock
2022-10-01 19:04:14.733|I|nuki.py:579|Locking nuki
2022-10-01 19:04:14.735|I|nuki.py:173|Stop scanning
2022-10-01 19:04:14.735|I|nuki.py:543|Nuki connecting
2022-10-01 19:04:14.767|E|nuki.py:523|Error: <class 'bleak.exc.BleakDBusError'> [org.freedesktop.DBus.Error.AccessDenied] Rejected send message, 2 matched rules; type="method_call", sender=":1.69" (uid=1000 pid=48268 comm="python . --lock ") interface="org.freedesktop.DBus.ObjectManager" member="GetManagedObjects" error name="(unset)" requested_reply="0" destination="org.bluez" (uid=0 pid=48089 comm="/usr/lib/bluetooth/bluetoothd ")
Traceback (most recent call last):
  File "./nuki.py", line 517, in _send_data
    await self.connect()
  File "./nuki.py", line 544, in connect
    await self._client.connect()
  File "/usr/local/lib/python3.7/site-packages/bleak/backends/bluezdbus/client.py", line 113, in connect
    self.address, timeout=timeout, adapter=self._adapter
  File "/usr/local/lib/python3.7/site-packages/bleak/backends/scanner.py", line 223, in find_device_by_address
    **kwargs,
  File "/usr/local/lib/python3.7/site-packages/bleak/backends/scanner.py", line 249, in find_device_by_filter
    async with cls(detection_callback=apply_filter, **kwargs):
  File "/usr/local/lib/python3.7/site-packages/bleak/backends/scanner.py", line 95, in __aenter__
    await self.start()
  File "/usr/local/lib/python3.7/site-packages/bleak/backends/bluezdbus/scanner.py", line 133, in start
    assert_reply(reply)
  File "/usr/local/lib/python3.7/site-packages/bleak/backends/bluezdbus/utils.py", line 23, in assert_reply
    raise BleakDBusError(reply.error_name, reply.body)
bleak.exc.BleakDBusError: [org.freedesktop.DBus.Error.AccessDenied] Rejected send message, 2 matched rules; type="method_call", sender=":1.69" (uid=1000 pid=48268 comm="python . --lock ") interface="org.freedesktop.DBus.ObjectManager" member="GetManagedObjects" error name="(unset)" requested_reply="0" destination="org.bluez" (uid=0 pid=48089 comm="/usr/lib/bluetooth/bluetoothd ")

with SUDO python . --unlock:

pi@raspberrypi:~/RaspiNukiBridge $ sudo python .  --unlock
2022-10-01 19:16:20.485|I|nuki.py:586|Unlocking
2022-10-01 19:16:20.486|I|nuki.py:173|Stop scanning
2022-10-01 19:16:20.487|I|nuki.py:543|Nuki connecting
2022-10-01 19:16:23.162|E|nuki.py:523|Error: <class 'bleak.exc.BleakDBusError'> [org.bluez.Error.Failed] Software caused connection abort
Traceback (most recent call last):
  File "./nuki.py", line 517, in _send_data
    await self.connect()
  File "./nuki.py", line 544, in connect
    await self._client.connect()
  File "/usr/local/lib/python3.7/site-packages/bleak/backends/bluezdbus/client.py", line 278, in connect
    assert_reply(reply)
  File "/usr/local/lib/python3.7/site-packages/bleak/backends/bluezdbus/utils.py", line 23, in assert_reply
    raise BleakDBusError(reply.error_name, reply.body)
bleak.exc.BleakDBusError: [org.bluez.Error.Failed] Software caused connection abort

but this works ... ??

Bildschirmfoto 2022-10-01 um 20 40 51
Jayknightfr commented 2 years ago

If you scan for devices via Bluetoothctl do you find your nuki?

`#bluetoothctl

scan on

devices`

Le sam. 1 oct. 2022, 20:06, t0bse @.***> a écrit :

Logs say you're still using python 3.9 Le sam. 1 oct. 2022, 15:45, t0bse @.***> a écrit : … <#m2038364590532723894>

oops sorry thank you. but python 3.7... and bluez Version: 5.50-1.2~deb10u2

@.***:~/RaspiNukiBridge $ python . --lock

2022-10-01 19:04:14.733|I|nuki.py:579|Locking nuki

2022-10-01 19:04:14.735|I|nuki.py:173|Stop scanning

2022-10-01 19:04:14.735|I|nuki.py:543|Nuki connecting

2022-10-01 19:04:14.767|E|nuki.py:523|Error: <class 'bleak.exc.BleakDBusError'> [org.freedesktop.DBus.Error.AccessDenied] Rejected send message, 2 matched rules; type="method_call", sender=":1.69" (uid=1000 pid=48268 comm="python . --lock ") interface="org.freedesktop.DBus.ObjectManager" member="GetManagedObjects" error name="(unset)" requested_reply="0" destination="org.bluez" (uid=0 pid=48089 comm="/usr/lib/bluetooth/bluetoothd ")

Traceback (most recent call last):

File "./nuki.py", line 517, in _send_data

await self.connect()

File "./nuki.py", line 544, in connect

await self._client.connect()

File "/usr/local/lib/python3.7/site-packages/bleak/backends/bluezdbus/client.py", line 113, in connect

self.address, timeout=timeout, adapter=self._adapter

File "/usr/local/lib/python3.7/site-packages/bleak/backends/scanner.py", line 223, in find_device_by_address

**kwargs,

File "/usr/local/lib/python3.7/site-packages/bleak/backends/scanner.py", line 249, in find_device_by_filter

async with cls(detection_callback=apply_filter, **kwargs):

File "/usr/local/lib/python3.7/site-packages/bleak/backends/scanner.py", line 95, in aenter

await self.start()

File "/usr/local/lib/python3.7/site-packages/bleak/backends/bluezdbus/scanner.py", line 133, in start

assert_reply(reply)

File "/usr/local/lib/python3.7/site-packages/bleak/backends/bluezdbus/utils.py", line 23, in assert_reply

raise BleakDBusError(reply.error_name, reply.body)

bleak.exc.BleakDBusError: [org.freedesktop.DBus.Error.AccessDenied] Rejected send message, 2 matched rules; type="method_call", sender=":1.69" (uid=1000 pid=48268 comm="python . --lock ") interface="org.freedesktop.DBus.ObjectManager" member="GetManagedObjects" error name="(unset)" requested_reply="0" destination="org.bluez" (uid=0 pid=48089 comm="/usr/lib/bluetooth/bluetoothd ")

— Reply to this email directly, view it on GitHub https://github.com/dauden1184/RaspiNukiBridge/issues/14#issuecomment-1264440900, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEVCK3YTBDIQSMIGT2BRUULWBB4RHANCNFSM6AAAAAAQJLWSSI . You are receiving this because you commented.Message ID: @.***>

t0bse commented 2 years ago

If you scan for devices via Bluetoothctl do you find your nuki? #bluetoothctl #scan on #devices Le sam. 1 oct. 2022, 20:06, t0bse @.***> a écrit :

yes. maybe the bluetooth connection is not the best? (RSSI: -80) it worked for the first time now, without changing anything... sometimes this is happening only, after "connected" it finishes:

pi@raspberrypi:~/RaspiNukiBridge $ sudo python . --lock
2022-10-01 19:47:11.700|I|nuki.py:579|Locking nuki
2022-10-01 19:47:11.701|I|nuki.py:173|Stop scanning
2022-10-01 19:47:11.701|I|nuki.py:543|Nuki connecting
2022-10-01 19:47:16.666|I|nuki.py:247|Device type: DeviceType.SMARTLOCK_1_2
2022-10-01 19:47:20.739|I|nuki.py:555|Connected

i ran it a second time with 5 replies, and on the third reply, it worked. next try, with locking the door, is not working.

but i also set the timeout to 30 seconds and the "software caused connection abort" error is showing every 5 seconds

Jayknightfr commented 2 years ago

Yeah i think it's due to the timeout. In real conditions you don't normally open and close and open it via the bridge so if it works every 30s i think it's fine.

On my end after a reboot of my pi i also had the BleakDBusError too.

I managed to fix it by running the service with the root user, and therefore changing the User in the systemd service :

`[Unit] Description=Nuki bridge After=network-online.target

[Service] Type=simple Restart=always RestartSec=1 User=root WorkingDirectory=/home/pi/RaspiNukiBridge/ ExecStart=python3 .

[Install] WantedBy=multi-user.target`

Glad yours is working :D