kvj / hass_nuki_ng

Better support for Nuki devices in the Home Assistant
MIT License
185 stars 36 forks source link

Android Bridge Support #46

Closed ahaverty closed 2 years ago

ahaverty commented 2 years ago

Hi there, is this integration intended to work with the Nuki Android Bridge app? I'm getting Error communicating with Nuki bridge when trying to add the New Nuki device.

I'm using the Android Bridge App, and have double checked the IP address and tried multiple different tokens.

I may get a hardware bridge in the future, but was hoping to cheap out to test things out with an old android device in the meantime.

Thank you!

ahaverty commented 2 years ago

Looking at logs, if I'm reading this right, it seems to be hardcoded to port 80, which I unfortunately can't use in the android bridge app, as android seems to restrict ports below 1024. Does that sound correct? (I know I'm exposing the secret here, it's just for testing at the moment, I will change it).

Logger: custom_components.nuki_ng.config_flow
Source: custom_components/nuki_ng/nuki.py:68 
Integration: Nuki Lock ([documentation](https://github.com/kvj/hass_nuki_ng), [issues](https://github.com/kvj/hass_nuki_ng/issues)) 
First occurred: 8:34:28 PM (4 occurrences) 
Last logged: 8:47:32 PM

Failed to get list of devices from bridge: HTTPConnectionPool(host='http', port=80): Max retries exceeded with url: //192.168.86.48:8080/list?token=j37sk2p0f7qkx6s (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0xffff74a21a30>: Failed to establish a new connection: [Errno -3] Try again'))
Failed to get list of devices from bridge: HTTPConnectionPool(host='http', port=80): Max retries exceeded with url: //192.168.86.48:8080/list?token=SdfSg35Hvkg23 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0xffff74a31910>: Failed to establish a new connection: [Errno -3] Try again'))
Failed to get list of devices from bridge: HTTPConnectionPool(host='http', port=80): Max retries exceeded with url: //192.168.86.48:9876/list?token=SdfSg35Hvkg23 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0xffff759062e0>: Failed to establish a new connection: [Errno -3] Try again'))
Failed to get list of devices from bridge: HTTPConnectionPool(host='http', port=80): Max retries exceeded with url: //192.168.86.48:9876/:8080/list?token=SdfSg35Hvkg23 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0xffff74bdfcd0>: Failed to establish a new connection: [Errno -3] Try again'))
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "/usr/local/lib/python3.9/site-packages/urllib3/util/connection.py", line 73, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/local/lib/python3.9/socket.py", line 954, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Try again

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 394, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 239, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/usr/local/lib/python3.9/http/client.py", line 1279, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/lib/python3.9/http/client.py", line 1325, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.9/http/client.py", line 1274, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.9/http/client.py", line 1034, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.9/http/client.py", line 974, in send
    self.connect()
  File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 205, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0xffff74a21a30>: Failed to establish a new connection: [Errno -3] Try again

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "/usr/local/lib/python3.9/site-packages/urllib3/util/retry.py", line 574, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='http', port=80): Max retries exceeded with url: //192.168.86.48:8080/list?token=j37sk2p0f7qkx6s (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0xffff74a21a30>: Failed to establish a new connection: [Errno -3] Try again'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/config/custom_components/nuki_ng/config_flow.py", line 33, in find_nuki_devices
    response = await nuki.bridge_list()
  File "/config/custom_components/nuki_ng/nuki.py", line 68, in bridge_list
    return await self.async_json(lambda r: r.get(self.bridge_url("/list")))
  File "/config/custom_components/nuki_ng/nuki.py", line 39, in async_json
    response = await self.hass.async_add_executor_job(lambda: cb(requests))
  File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/nuki_ng/nuki.py", line 39, in <lambda>
    response = await self.hass.async_add_executor_job(lambda: cb(requests))
  File "/config/custom_components/nuki_ng/nuki.py", line 68, in <lambda>
    return await self.async_json(lambda r: r.get(self.bridge_url("/list")))
  File "/usr/local/lib/python3.9/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='http', port=80): Max retries exceeded with url: //192.168.86.48:8080/list?token=j37sk2p0f7qkx6s (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0xffff74a21a30>: Failed to establish a new connection: [Errno -3] Try again'))
ahaverty commented 2 years ago

Ah, the above error may be from when I was attempting with http:// prepending the bridge URL.

Testing again, I can see it appears to be getting further, but failing with an index error.

Failed to get list of devices from bridge: list index out of range
Traceback (most recent call last):
  File "/config/custom_components/nuki_ng/config_flow.py", line 35, in find_nuki_devices
    title = response[0]["name"]
IndexError: list index out of range

This may be stupid, but I'm currently just setting up the bridge in advance of actually receiving my Nuki lock delivery. I just want to have the integration ready to go because I'm impatient 😄

Perhaps the error is just generic enough, and it actually is trying to tell me that it can't find any devices to setup?

I'll try again hopefully tomorrow once the lock arrives 👌

I'll leave this open in case any other eager HA users hit a similar issue in the future.

ahaverty commented 2 years ago

Yeh, just reviewing the config code, it appears any error here would report as an invalid bridge token issue. Seems hopeful for android bridge support so, I'll try to remember to report back tomorrow with what I find once I've got a lock attached to the bridge app.

kvj commented 2 years ago

Hey, it looks like the response is empty (array), as no hardware has been registered yet. I don't really think your setup should work.

Additionally, the value of the bridge URL should be either host, or host:port, without the http:// prefix

ahaverty commented 2 years ago

Thanks @kvj, So I've since installed the lock, and gotten the android bridge working via this integration. I'm able to lock and unlock via home assistant, and can see the status change. However, if someone makes a change to the lock via the Nuki app, or if the lock is manually locked/unlocked by hand, the status in home assistant does not change.

I went to try setup the Nuki Web account to see if that would give me more reliable status updates, but I'm blocked by the Nuki app asking me to link a physical bridge.

I'm presuming things have changed since Nuki have decommissioned the android bridge app, so I've just ordered a physical bridge.

Maybe I've gone wrong somewhere with the android bridge and this integration, but it appears that it's not fully supported. It's a shame Nuki decommissioned the bridge app, as I liked the idea of being green and reusing an unused android phone. I'll close this issue, maybe it'll help someone in future decide on android bridge apk vs buying a physical bridge. Thanks for the great integration, I'm looking forward to getting it 100% with the new bridge 🙌