home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
69.75k stars 28.91k forks source link

TUYA Cloud doesn't always respond, leading to incorrect device state, and devices showing as unavailable. #115344

Open jonwaland opened 2 months ago

jonwaland commented 2 months ago

The problem

TUYA Cloud is doesn't always respond, leading to incorrect device state, and devices showing as unavailable.

What version of Home Assistant Core has the issue?

core-2024.4.2

What was the last working version of Home Assistant Core?

core-2024.2.1

What type of installation are you running?

Home Assistant Core

Integration causing the issue

tuya

Link to integration documentation on our website

https://www.home-assistant.io/integrations/tuya

Diagnostics information

config_entry-tuya-5f1d5cd1692d89477e6bd1422092e741.json

Example YAML snippet

n/a

Anything in the logs that might be useful for us?

2024-04-10 17:23:03.759 DEBUG (SyncWorker_52) [homeassistant.components.tuya] Sending commands for device 02000855807d3a4f4853: [{'code': <DPCode.SWITCH_1: 'switch_1'>, 'value': True}]
2024-04-10 17:23:06.203 DEBUG (Thread-2611 (_thread_main)) [homeassistant.components.tuya] Received update for device 02000855807d3a4f4853: {'switch_1': True, 'switch_2': True}
2024-04-10 17:24:43.365 DEBUG (SyncWorker_32) [homeassistant.components.tuya] Sending commands for device 02000855807d3a4f4853: [{'code': <DPCode.SWITCH_2: 'switch_2'>, 'value': True}]
2024-04-10 17:25:08.687 DEBUG (SyncWorker_59) [homeassistant.components.tuya] Sending commands for device 02000855807d3a4f4853: [{'code': <DPCode.SWITCH_2: 'switch_2'>, 'value': True}]
2024-04-10 17:25:09.672 DEBUG (Thread-2803 (_thread_main)) [homeassistant.components.tuya] Received update for device 02000855807d3a4f4853: {'switch_1': True, 'switch_2': True}
2024-04-10 17:25:13.867 DEBUG (SyncWorker_34) [homeassistant.components.tuya] Sending commands for device 02000855807d3a4f4853: [{'code': <DPCode.SWITCH_2: 'switch_2'>, 'value': False}]
2024-04-10 17:30:10.396 WARNING (MainThread) [homeassistant.helpers.entity] Update of sensor.current_uv_index is taking over 10 seconds

Additional information

debug logs attached above, whilst turning switches on and off via GUI, and via the actual switch. State changes from using the switch are frequently missed. Using the GUI the device is always toggled (eg cloud received the command), but doesn't always respond with a state update.

also see https://github.com/home-assistant/core/issues/114209 https://github.com/home-assistant/core/issues/114100 https://github.com/home-assistant/core/issues/114048 https://github.com/home-assistant/core/issues/113955

which may or may not be related

home-assistant[bot] commented 2 months ago

Hey there @tuya, @zlinoliver, @frenck, mind taking a look at this issue as it has been labeled with an integration (tuya) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `tuya` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign tuya` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


tuya documentation tuya source (message by IssueLinks)

jonwaland commented 2 months ago

additional log, presumably related:

2024-04-10 18:15:08.600 ERROR (MainThread) [homeassistant.components.automation.light_turn_on_the_extra_light_in_powder_room_when_is_use] LIGHT: turn on the extra light in powder room when is use: Error executing script. Unexpected error for call_service at pos 1: network error:(-9999999) sign invalid
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 507, in _async_step
    await getattr(self, handler)()
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 736, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 699, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/core.py", line 2543, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/core.py", line 2580, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/components/homeassistant/__init__.py", line 144, in async_handle_turn_service
    await asyncio.gather(*tasks)
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/core.py", line 2543, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/core.py", line 2580, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/service.py", line 996, in entity_service_call
    raise result from None
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 1580, in async_request_call
    return await coro
           ^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/service.py", line 1043, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 1655, in async_turn_on
    await self.hass.async_add_executor_job(ft.partial(self.turn_on, **kwargs))
  File "/usr/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/components/tuya/switch.py", line 711, in turn_on
    self._send_command([{"code": self.entity_description.key, "value": True}])
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/components/tuya/base.py", line 275, in _send_command
    self.device_manager.send_commands(self.device.id, commands)
  File "/srv/homeassistant/lib/python3.12/site-packages/tuya_sharing/manager.py", line 93, in send_commands
    return self.device_repository.send_commands(device_id, commands)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/tuya_sharing/device.py", line 180, in send_commands
    self.api.post(f"/v1.1/m/thing/{device_id}/commands", None, {"commands": commands})
  File "/srv/homeassistant/lib/python3.12/site-packages/tuya_sharing/customerapi.py", line 192, in post
    return self.__request("POST", path, params, body)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/tuya_sharing/customerapi.py", line 121, in __request
    raise Exception(f"network error:({ret['code']}) {ret['msg']}")
Exception: network error:(-9999999) sign invalid
2024-04-10 18:15:08.613 ERROR (MainThread) [homeassistant.components.automation.light_turn_on_the_extra_light_in_powder_room_when_is_use] While executing automation automation.light_turn_on_the_extra_light_in_powder_room_when_is_use
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/components/automation/__init__.py", line 726, in async_trigger
    return await self.action_script.async_run(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 1713, in async_run
    return await asyncio.shield(create_eager_task(run.async_run()))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 457, in async_run
    await self._async_step(log_exceptions=False)
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 509, in _async_step
    self._handle_exception(
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 539, in _handle_exception
    raise exception
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 507, in _async_step
    await getattr(self, handler)()
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 736, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 699, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/core.py", line 2543, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/core.py", line 2580, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/components/homeassistant/__init__.py", line 144, in async_handle_turn_service
    await asyncio.gather(*tasks)
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/core.py", line 2543, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/core.py", line 2580, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/service.py", line 996, in entity_service_call
    raise result from None
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 1580, in async_request_call
    return await coro
           ^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/service.py", line 1043, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 1655, in async_turn_on
    await self.hass.async_add_executor_job(ft.partial(self.turn_on, **kwargs))
  File "/usr/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/components/tuya/switch.py", line 711, in turn_on
    self._send_command([{"code": self.entity_description.key, "value": True}])
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/components/tuya/base.py", line 275, in _send_command
    self.device_manager.send_commands(self.device.id, commands)
  File "/srv/homeassistant/lib/python3.12/site-packages/tuya_sharing/manager.py", line 93, in send_commands
    return self.device_repository.send_commands(device_id, commands)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/tuya_sharing/device.py", line 180, in send_commands
    self.api.post(f"/v1.1/m/thing/{device_id}/commands", None, {"commands": commands})
  File "/srv/homeassistant/lib/python3.12/site-packages/tuya_sharing/customerapi.py", line 192, in post
    return self.__request("POST", path, params, body)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/tuya_sharing/customerapi.py", line 121, in __request
    raise Exception(f"network error:({ret['code']}) {ret['msg']}")
Exception: network error:(-9999999) sign invalid
mlittle408 commented 2 months ago

Is there a status on this? I have to reload the integration every few hours to get it to respond again. I even downgraded Home Assistant Core 2024.3.1 but that didn't fix the problem.

jonwaland commented 2 months ago

I even downgraded Home Assistant Core 2024.3.1 but that didn't fix the problem.

its a problem with Tuya, not Home Assistant. Which is unfortunate, as that means we are dependent on https://github.com/tuya and https://github.com/zlinoliver (@zlinoliver) to give feedback at least.

jonwaland commented 1 month ago
2024-05-14 20:14:59.186 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140184982998848] Unexpected exception
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.12/site-packages/urllib3/connectionpool.py", line 715, in urlopen
    httplib_response = self._make_request(
                       ^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/urllib3/connectionpool.py", line 404, in _make_request
    self._validate_conn(conn)
  File "/srv/homeassistant/lib/python3.12/site-packages/urllib3/connectionpool.py", line 1058, in _validate_conn
    conn.connect()
  File "/srv/homeassistant/lib/python3.12/site-packages/urllib3/connection.py", line 419, in connect
    self.sock = ssl_wrap_socket(
                ^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/urllib3/util/ssl_.py", line 449, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/urllib3/util/ssl_.py", line 493, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/ssl.py", line 455, in wrap_socket
    return self.sslsocket_class._create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/ssl.py", line 1042, in _create
    self.do_handshake()
  File "/usr/lib/python3.12/ssl.py", line 1320, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLEOFError: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1000)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.12/site-packages/requests/adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/urllib3/connectionpool.py", line 799, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/urllib3/util/retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='apigw.tuyaeu.com', port=443): Max retries exceeded with url: /v1.1/m/thing/02000855807d3a4f4853/commands (Caused by SSLError(SSLEOFError(8, '[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1000)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/components/websocket_api/commands.py", line 239, in handle_call_service
    response = await hass.services.async_call(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/core.py", line 2738, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/core.py", line 2779, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/service.py", line 975, in entity_service_call
    single_response = await _handle_entity_call(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/service.py", line 1047, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/helpers/entity.py", line 1688, in async_turn_off
    await self.hass.async_add_executor_job(ft.partial(self.turn_off, **kwargs))
  File "/usr/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/components/tuya/switch.py", line 715, in turn_off
    self._send_command([{"code": self.entity_description.key, "value": False}])
  File "/srv/homeassistant/lib/python3.12/site-packages/homeassistant/components/tuya/base.py", line 275, in _send_command
    self.device_manager.send_commands(self.device.id, commands)
  File "/srv/homeassistant/lib/python3.12/site-packages/tuya_sharing/manager.py", line 93, in send_commands
    return self.device_repository.send_commands(device_id, commands)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/tuya_sharing/device.py", line 180, in send_commands
    self.api.post(f"/v1.1/m/thing/{device_id}/commands", None, {"commands": commands})
  File "/srv/homeassistant/lib/python3.12/site-packages/tuya_sharing/customerapi.py", line 192, in post
    return self.__request("POST", path, params, body)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/tuya_sharing/customerapi.py", line 107, in __request
    response = self.session.request(
               ^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.12/site-packages/requests/adapters.py", line 517, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='apigw.tuyaeu.com', port=443): Max retries exceeded with url: /v1.1/m/thing/02000855807d3a4f4853/commands (Caused by SSLError(SSLEOFError(8, '[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1000)')))
stamandr commented 1 month ago

Same problem here. I have to re-authenticate daily

jonwaland commented 1 month ago

@frenck - I know this isn't your problem - but do you have any leverage?

wesleyscaldwell commented 1 month ago

I'm having the same issue. For me it started after I had to re-authenticate using the the new tuya authentication method. Since then, when I turn on a light in the app. I have to wait 10 seconds, then click the button again. At that point it retrieves the correct status for the switch.

But if I'm not at the house, I have to reload the integration to get the statuses to be correct.

It also causes automations to stop working where the status of a switch is a condition in the automation.

It's not that it's not usable this way. But it is to the point that there doesn't seem to be much point to trying to make it work like this.

rightsaidfred99 commented 1 month ago

FWIW I had the network error:(-9999999) sign invalid error. I just reloaded the integration and it worked though. Hope it helps someone

jonwaland commented 1 month ago

the reload forces a resync. until the next error.

wesleyscaldwell commented 1 month ago

This not working forced me to finally work on getting localtuya working. I was sick of automations not working when the status wouldn't update.

It was a bit time consuming to get all the devices added and then automations updated. But it's working nicely now.

zonyl commented 1 month ago

Im getting the same error. I know it appears to be a cloud issue, but can we get the integration to handle this error and requeue it for retry? How do you get the secrets nowadays for localtuya?

jonwaland commented 1 month ago

I've just cut across to https://github.com/xZetsubou/hass-localtuya/

AUSPhoenix78 commented 1 week ago

Same issue here has anyone got a work around at this time ?

wesleyscaldwell commented 1 week ago

I setup localtuya and everything has been working as expected again.

Mike5357 commented 1 week ago

I also switched to Tuya Local a few days ago- Ditto that it was a little annoying having to switch over all my entities, but well worth it to finally have my devices reporting correct states again after several months. (Plus the added bonus of devices updating much faster now without relying on the cloud is very nice)