PlusPlus-ua / ha_tuya_ble

Home Assistant support for Tuya BLE devices
MIT License
160 stars 102 forks source link

ha_tuya_ble prevents HA from fully booting if a configured tuya device is missing/cannot be connected #39

Open zahical opened 1 year ago

zahical commented 1 year ago

The specific device for which I've seen this behavior is Fingerbot. I've already had the device successfully configured and running before that. Then, for unrelated reasons, I've decided to restart HA, however the device was not in range, and HA is now stuck "Waiting on integrations to complete setup: tuya_ble" and "tuya_ble" itself seem to be running an endless connection retry loop. 'Restart HA' (from the restart menu) also doesn't help.

(btw, much thanks for the great integration; having to use "the cloud" to manage a BLE device has always felt really strange to me; thanks to your efforts, now we can avoid this madness)

Versions: HA Core: 2023.5.3 HA OS: 10.1 (running on rpi3-64) tuya_ble: 0.1.7 (commit c11a1c5481dd7263182d42135d8416e4ea06f523), installed by direct git clone.

Error callstack:

2023-06-29 20:23:23.062 WARNING (MainThread) [homeassistant.bootstrap] Waiting on integrations to complete setup: tuya_ble
2023-06-29 20:23:41.410 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] XX:XX:XX:XX:XX:XX: device not found, not in range, or poor RSSI: None
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/bleak/backends/bluezdbus/client.py", line 204, in connect
    reply = await self._bus.call(
  File "/usr/local/lib/python3.10/site-packages/dbus_fast/aio/message_bus.py", line 371, in call
    await future
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/bleak_retry_connector/__init__.py", line 344, in establish_connection
    await client.connect(
  File "/usr/src/homeassistant/homeassistant/components/bluetooth/wrappers.py", line 272, in connect
    connected = await super().connect(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/bleak/__init__.py", line 531, in connect
    return await self._backend.connect(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/bleak/backends/bluezdbus/client.py", line 141, in connect
    async with async_timeout(timeout):
  File "/usr/local/lib/python3.10/site-packages/async_timeout/__init__.py", line 129, in __aexit__
    self._do_exit(exc_type)
  File "/usr/local/lib/python3.10/site-packages/async_timeout/__init__.py", line 212, in _do_exit
    raise asyncio.TimeoutError
asyncio.exceptions.TimeoutError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/config/custom_components/tuya_ble/tuya_ble/tuya_ble.py", line 586, in _ensure_connected
    client = await establish_connection(
  File "/usr/local/lib/python3.10/site-packages/bleak_retry_connector/__init__.py", line 361, in establish_connection
    _raise_if_needed(name, device.address, exc)
  File "/usr/local/lib/python3.10/site-packages/bleak_retry_connector/__init__.py", line 310, in _raise_if_needed
    raise BleakNotFoundError(msg) from exc
bleak_retry_connector.BleakNotFoundError: XX:XX:XX:XX:XX:XX - XX:XX:XX:XX:XX:XX: Failed to connect:
zahical commented 1 year ago

Looking at the code: https://github.com/PlusPlus-ua/ha_tuya_ble/blob/c11a1c5481dd7263182d42135d8416e4ea06f523/custom_components/tuya_ble/tuya_ble/tuya_ble.py#L571

it seems the reconnect loop is limited to 100 attempts, so HA will probably boot at the end.

However, at least, on my set-up, a single retry takes ~ 120 seconds, e.g.

2023-06-29 20:31:47.803 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] XX:XX:XX:XX:XX:XX: device not found
...
2023-06-29 20:33:08.866 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] XX:XX:XX:XX:XX:XX: device not found
...
2023-06-29 20:34:29.934 ERROR (MainThread) [custom_components.tuya_ble.tuya_ble.tuya_ble] XX:XX:XX:XX:XX:XX: device not found

so it will take a lot of time (3+ hours).

forabi commented 1 year ago

I have noticed the same too. I wonder if we can make the whole connection lazy, i.e. postpone until after startup has completed

PlusPlus-ua commented 1 year ago

Hi,

You are right, connection itself should be postponed, I'll change it.

PlusPlus-ua commented 12 months ago

Please try new build, the problem must be solved.

00punisher commented 11 months ago

Just installed this component yesterday on HA 2023.6.3 and same issue. I reboot HA while the Fingerbot off and it cannot finish until I switch on the Fingerbot at least within a reasonable timeframe. Thanks!