Closed ZephireNZ closed 2 years ago
Just dug into the code to see if I can figure out a way to prevent this from happening.
Aside from resolving the actual Tuya server issues, the code can be updated to be more resilient.
In this case, it's the MQTT client that is failing to re-connect after 2 hours.
In openmq the client is recreated. Normally this is fine. However in the call to self._get_mqtt_config()
the API server fails to connect, and the error that is throw ripples all the way up to the run()
loop. This loop doesn't handle the error, and so the MQTT server eventually expires and the loop is no longer replacing it.
I think this loop should have a try/except added so that it can handle any errors thrown, and continue trying to refresh the MQTT server until it is successful. Given that it's an API server, probably best to have exponential backoff so as to not hammer the server with requests when it is down.
Amazing work, here's hoping your PR gets accepted soon.
Here's another related issue: https://github.com/home-assistant/core/issues/63052
@zlinoliver Hello, will we get a new release with this fix soon?
Same issue on my side since months. I hope there will be a new release soon. Thanks.
Same thing. After some time being offline localtuya stops working.
The logs shows it tried to contact tuya servers, and that is where it dies. 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 0x75d377d07fa0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))
I am getting a error where the API intermittently will fail to connect.
From what I can tell, the error causes the Tuya integration in Home Assistant to stop tracking device state. I have two devices, a light and a heat pump, and both are "frozen" in time after the error occurs. A reboot will usually fix the issue (presumably at the time of the reboot, the API is responsive again).
I can see multiple issues on Home Assistant Core which seem could be related: https://github.com/home-assistant/core/issues/63957 https://github.com/home-assistant/core/issues/64077 https://github.com/home-assistant/core/issues/65099 https://github.com/home-assistant/core/issues/65524 https://github.com/home-assistant/core/issues/65634 https://github.com/home-assistant/core/issues/65758
I think one solution would be to use exponential backoff to retry the API. Based on the log below, it's looks to be trying a few times and then stops tracking state entirely.
Here's the most recent log from today, that shows the issue. Home Assistant shows no state change from 16:06 onwards.