rospogrigio / localtuya

local handling for Tuya devices
GNU General Public License v3.0
2.71k stars 530 forks source link

"Failed to set up" after machine reboot or HA restart while internet is not available #1659

Open artem-zinnatullin opened 5 months ago

artem-zinnatullin commented 5 months ago

Hi!

Almost every time after machine reboot HA boots normally, but localtuya crashes with Failed to set up and doesn't reload automatically causing ALL Tuya related automations to fail.

Environment

Steps to reproduce

  1. Reboot the machine sudo restart
  2. Wait for Home Assistant to boot
  3. Navigate to Integrations
  4. Observe localtuya Failed to set up

Or:

  1. Cut off the internet from the machine
  2. Restart HA
  3. Navigate to Integrations
  4. Observe localtuya Failed to set up
image

Provide Home Assistant traceback/logs

2024-02-03 12:19:10.024 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry localtuya for localtuya
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/urllib3/connection.py", line 174, in _new_conn
    conn = connection.create_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/urllib3/util/connection.py", line 72, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/socket.py", line 962, 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.11/site-packages/urllib3/connectionpool.py", line 715, in urlopen
    httplib_response = self._make_request(
                       ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 404, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 1058, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.11/site-packages/urllib3/connection.py", line 363, in connect
    self.sock = conn = self._new_conn()
                       ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/urllib3/connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0xffff82ab0190>: 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.11/site-packages/requests/adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 799, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/urllib3/util/retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='openapi.tuyaus.com', port=443): Max retries exceeded with url: /v1.0/token?grant_type=1 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0xffff82ab0190>: 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/src/homeassistant/homeassistant/config_entries.py", line 406, in async_setup
    result = await component.async_setup_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/__init__.py", line 255, in async_setup_entry
    res = await tuya_api.async_get_access_token()
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/cloud_api.py", line 104, in async_get_access_token
    resp = await self.async_make_request("GET", "/v1.0/token?grant_type=1")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/cloud_api.py", line 98, in async_make_request
    resp = await self._hass.async_add_executor_job(func)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/requests/api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/requests/adapters.py", line 519, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='openapi.tuyaus.com', port=443): Max retries exceeded with url: /v1.0/token?grant_type=1 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0xffff82ab0190>: Failed to establish a new connection: [Errno -3] Try again'))

Additional information

To fix the issue I have to manually navigate to Integrations -> localtuya -> Reload.

Few thoughts:

xtruss commented 5 months ago

Just to add to this my internet went down for 15 minutes, when it came back localtuya had failed to load and my devices were offline. HA hadn't been restarted so it was just a case of the internet dropping for me causing the issue. A re load of localtuya resolved the issue, but i put this on so it would work offline!!

wrowlands3 commented 4 months ago

I also have this issue. Localtuya "failed to set up" with no internet regardless of how many times I restart. I managed to make it by work by in configuration, reconfigure the cloud api access and selecting do not use cloud api. It now works smoothly, though will likely need cloud reconfiguring to easily add new devices.

This should really be something that doesn't prevent the integration from setting up or running at all though, I would have imagined it would be an easy thing to detect if there is no connection and skip the cloud api polling which is only required for easy adding of new devices.

Mannokia commented 1 month ago

I had a similar problem when adding a new device. But restarting the integration did not help. Is it possible to somehow solve this without removing the integration? I do not have a current backup copy.