Browse Source

device: close connection on any exception when receiving.

This should hopefully help to avoid hung sockets in cases where the device
disappears off the network, and speed up reconnection.
Issue #1804, #1013
Jason Rumney 1 year ago
parent
commit
c07269e710
1 changed files with 6 additions and 0 deletions
  1. 6 0
      custom_components/tuya_local/device.py

+ 6 - 0
custom_components/tuya_local/device.py

@@ -247,6 +247,9 @@ class TuyaLocalDevice(object):
             _LOGGER.exception(
             _LOGGER.exception(
                 "%s receive loop terminated by exception %s", self.name, t
                 "%s receive loop terminated by exception %s", self.name, t
             )
             )
+            self._api.set_socketPersistent(False)
+            if self._api.parent:
+                self._api.parent.set_socketPersistent(False)
 
 
     @property
     @property
     def should_poll(self):
     def should_poll(self):
@@ -355,6 +358,9 @@ class TuyaLocalDevice(object):
                     type(t),
                     type(t),
                     t,
                     t,
                 )
                 )
+                self._api.set_socketPersistent(False)
+                if self._api.parent:
+                    self._api.parent.set_socketPersistent(False)
                 await asyncio.sleep(5)
                 await asyncio.sleep(5)
 
 
         # Close the persistent connection when exiting the loop
         # Close the persistent connection when exiting the loop