dauden1184 / RaspiNukiBridge

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

running it in HASSIO #5

Closed ftarolli closed 1 year ago

ftarolli commented 2 years ago

I'm wondering how to make it run in a HASSIO core (Alpine) config....

So far I've been able to install all needed, pair the lock and get it working but then I broke everything trying to run the service as u mentioned but through openrc instead.

Now I'm stopped here and can0t find how to go ahead...

PS: I'm definitely not an expert on any linux distro.... still much to learn

Traceback (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 "/root/RaspiNukiBridge/./__main__.py", line 261, in <module>
    web_server.start()
  File "/root/RaspiNukiBridge/./__main__.py", line 54, in start
    web.run_app(app, host=self._host, port=self._port)
  File "/usr/lib/python3.9/site-packages/aiohttp/web.py", line 514, in run_app
    loop.run_until_complete(main_task)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/usr/lib/python3.9/site-packages/aiohttp/web.py", line 321, in _run_app
    await runner.setup()
  File "/usr/lib/python3.9/site-packages/aiohttp/web_runner.py", line 279, in setup
    self._server = await self._make_server()
  File "/usr/lib/python3.9/site-packages/aiohttp/web_runner.py", line 375, in _make_server
    await self._app.startup()
  File "/usr/lib/python3.9/site-packages/aiohttp/web_app.py", line 417, in startup
    await self.on_startup.send(self)
  File "/usr/lib/python3.9/site-packages/aiosignal/__init__.py", line 36, in send
    await receiver(*args, **kwargs)  # type: ignore
  File "/root/RaspiNukiBridge/./__main__.py", line 88, in _startup
    await self.nuki_manager.start_scanning()
  File "/root/RaspiNukiBridge/./nuki.py", line 135, in start_scanning
    await self._scanner.start()
  File "/usr/lib/python3.9/site-packages/bleak/backends/bluezdbus/scanner.py", line 88, in start
    self._bus = await MessageBus(bus_type=BusType.SYSTEM).connect()
  File "/usr/lib/python3.9/site-packages/dbus_next/aio/message_bus.py", line 122, in __init__
    super().__init__(bus_address, bus_type, ProxyObject)
  File "/usr/lib/python3.9/site-packages/dbus_next/message_bus.py", line 85, in __init__
    self._setup_socket()
  File "/usr/lib/python3.9/site-packages/dbus_next/message_bus.py", line 575, in _setup_socket
    raise err
  File "/usr/lib/python3.9/site-packages/dbus_next/message_bus.py", line 548, in _setup_socket
    self._sock.connect(filename)
FileNotFoundError: [Errno 2] No such file or directory
dauden1184 commented 2 years ago

Is it still working fine if you run the software normally? Can you please share the script used to autostart it?

ftarolli commented 2 years ago

Not at all. But I understood yesterday I was following the wrong way… HASSOS is using docker containers so I’ll move to learn how to build an ADDON from scratch to run your code with all dependencies needed and come back to u in a while during test phase maybe. I’m learning everything on the job…

your code was perfectly running before I messed up with openrc but anyway an update of HASSOS yesterday overwrote everything I did —> need to build and addon into a container

f1ren commented 2 years ago

@ftarolli here's a very basic proof of concept: https://github.com/f1ren/RaspiNukiBridge/ Tested on my Home Assistant (RPi 4) and it works.

However, would love some help with it:

  1. Need to simplify setup and configuration. Very complicated right now. I've followed this tutorial. There's a chapter there about Add-On Configuration. So, we should probably implement something of that sort.
  2. There are too many exceptions and Home Assistant is showing errors. The lock status in Home Assistant is broken.
ftarolli commented 2 years ago

@f1ren I've finished today my own version and it's under testing. https://github.com/ftarolli/NukiBridgeAddon

I modified the main.py and the run.sh to do automatic configuration after you entered the lock Mac address.

working fine with my NUKI3.0 on a PI4 out of several exception as u mentioned that are not however stopping the service to work right now.

Like this:

Task exception was never retrieved future: <Task finished name='Task-13' coro=<Nuki._notification_handler() done, defined at /opt/nuki.py:309> exception=BleakDBusError('org.freedesktop.DBus.Error.NoReply', 'Message recipient disconnected from message bus without replying')> Traceback (most recent call last): File "/opt/nuki.py", line 337, in _notification_handler await self.disconnect() File "/opt/nuki.py", line 438, in disconnect await self.manager.start_scanning() File "/opt/nuki.py", line 135, in start_scanning await self._scanner.start() File "/usr/local/lib/python3.9/site-packages/bleak/backends/bluezdbus/scanner.py", line 133, in start assert_reply(reply) File "/usr/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.NoReply] Message recipient disconnected from message bus without replying

or this:

Task exception was never retrieved future: <Task finished name='Task-21' coro=<NukiManager._detected_ibeacon() done, defined at /opt/nuki.py:144> exception=KeyError(76)> Traceback (most recent call last): File "/opt/nuki.py", line 146, in _detected_ibeacon manufacturer_data = advertisement_data.manufacturer_data[76] KeyError: 76

@dauden1184 any suggestion?

f1ren commented 2 years ago

@ftarolli I hope @dauden1184 can be more helpful, but here are my temporary solutions:

  1. Regarding BleakDBusError in start_scanning, you can check my exponential-backoff solution. It succeeds on second attempt.
  2. On KeyError: 76 I return quietly.
ftarolli commented 2 years ago

did the changes u suggested and uploaded to git. I'll have a try tomorrow. btw, since 1 day now the add-on is working fine without issues while I'm instead annoyed by this log...

2022-04-26 22:19:41.022|I|web_log.py:206|172.30.32.1 [26/Apr/2022:20:19:41 +0000] "GET /list?ts=2022-04-26T20:19:41Z&rnr=xxxxxx&hash=xxxxxxxxxxxxxxxxxxxx HTTP/1.1" 200 543 "-" "python-requests/2.27.1"

I'm wondering if modifying the verbose mode requiring a minimum arg of 0 as below would be the solution

if args.verbose == 0: logger.setLevel(level=logging.INFO) logging.getLogger("aiohttp").setLevel(level=logging.INFO) #ERROR logging.getLogger("bleak").setLevel(level=logging.INFO) #ERROR elif args.verbose == 1: logger.setLevel(level=logging.ERROR) #DUBUG logging.getLogger("aiohttp").setLevel(level=logging.ERROR) #INFO logging.getLogger("bleak").setLevel(level=logging.ERROR) #INFO elif args.verbose == 2: logger.setLevel(level=logging.DEBUG) logging.getLogger("aiohttp").setLevel(level=logging.DEBUG) logging.getLogger("bleak").setLevel(level=logging.DEBUG)

dauden1184 commented 2 years ago

or this:

Task exception was never retrieved future: <Task finished name='Task-21' coro=<NukiManager._detected_ibeacon() done, defined at /opt/nuki.py:144> exception=KeyError(76)> Traceback (most recent call last): File "/opt/nuki.py", line 146, in _detected_ibeacon manufacturer_data = advertisement_data.manufacturer_data[76] KeyError: 76

@dauden1184 any suggestion?

What nuki device are you using? Is it a lock? what version?

dauden1184 commented 2 years ago

did the changes u suggested and uploaded to git. I'll have a try tomorrow. btw, since 1 day now the add-on is working fine without issues while I'm instead annoyed by this log...

2022-04-26 22:19:41.022|I|web_log.py:206|172.30.32.1 [26/Apr/2022:20:19:41 +0000] "GET /list?ts=2022-04-26T20:19:41Z&rnr=xxxxxx&hash=xxxxxxxxxxxxxxxxxxxx HTTP/1.1" 200 543 "-" "python-requests/2.27.1"

I'm wondering if modifying the verbose mode requiring a minimum arg of 0 as below would be the solution

if args.verbose == 0: logger.setLevel(level=logging.INFO) logging.getLogger("aiohttp").setLevel(level=logging.INFO) #ERROR logging.getLogger("bleak").setLevel(level=logging.INFO) #ERROR elif args.verbose == 1: logger.setLevel(level=logging.ERROR) #DUBUG logging.getLogger("aiohttp").setLevel(level=logging.ERROR) #INFO logging.getLogger("bleak").setLevel(level=logging.ERROR) #INFO elif args.verbose == 2: logger.setLevel(level=logging.DEBUG) logging.getLogger("aiohttp").setLevel(level=logging.DEBUG) logging.getLogger("bleak").setLevel(level=logging.DEBUG)

If the flag --verbose is not specified, by default the value is 0. Is that what you meant?

ftarolli commented 2 years ago

I'm using a PI4 with HASSIO. Core 2022.4.7 Supervisor 2022.04.0 Nuki lock 3.0

ABOUT VERBOSE: I saw it. In fact I was thinking to changed the first line of the "if" sentence to require an argument (0/1/2) to log something otherwise proceed smoothly...

What do I think?

f1ren commented 2 years ago

My Home Assitant Addon is quite stable now. It even finds the MAC address for you :)

dauden1184 commented 2 years ago

Will merge the stuff in the next days, sorry but i was on vacation :sweat_smile:

sensei73 commented 2 years ago

My Home Assitant Addon is quite stable now. It even finds the MAC address for you :)

I tried your add-on in hassio, but it doesn't work ==> 022-05-17 14:38:24.987|I|nuki.py:193|Stop scanning 2022-05-17 14:38:24.987|I|nuki.py:200|Error while stop scanning. Scan was probably not started. 2022-05-17 14:38:24.987|E|nuki.py:201|'NoneType' object has no attribute 'call' Traceback (most recent call last): File "/opt/./nuki.py", line 195, in stop_scanning await self._scanner.stop() File "/usr/local/lib/python3.9/site-packages/bleak/backends/bluezdbus/scanner.py", line 169, in stop reply = await self._bus.call( AttributeError: 'NoneType' object has no attribute 'call' 2022-05-17 14:38:24.990|I|nuki.py:591|Nuki connecting

Ps: sorry to hijack this discussion, @f1ren, I cannot post this error in your git!

f1ren commented 2 years ago

@sensei73 this log actually looks ok. What does it print next?

sensei73 commented 2 years ago

@f1ren This is the full log

https://pastebin.com/raw/4TUpY7GP

f1ren commented 2 years ago

@sensei73 which setup are you using? Which RPi model and HA installation method?