Skip to content

Commit

Permalink
Skip sub-device reconnect if it's offline
Browse files Browse the repository at this point in the history
  • Loading branch information
xZetsubou committed Jul 10, 2024
1 parent a70d2a0 commit a34e316
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions custom_components/localtuya/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def __init__(
self._fake_gateway = fake_gateway
self._gateway: TuyaDevice = None
self.sub_devices: dict[str, TuyaDevice] = {}
self.sub_device_online = True

self._status = {}
# Sleep timer, a device that reports the status every x seconds then goes into sleep.
Expand Down Expand Up @@ -519,6 +520,10 @@ async def _async_reconnect(self):
attempts = 0
while True and not self._is_closing:
# for sub-devices, if the gateway isn't connected then no need for reconnect.
if self.is_subdevice and not self.sub_device_online:
self.warning(f"Sub deivce is offline")
await asyncio.sleep(10)
continue
if self._gateway and (
not self._gateway.connected or self._gateway.is_connecting
):
Expand Down
4 changes: 4 additions & 0 deletions custom_components/localtuya/core/pytuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ class TuyaListener(ABC):
"""Listener interface for Tuya device changes."""

sub_devices: dict[str, Self]
sub_device_online = False

@abstractmethod
def status_updated(self, status):
Expand Down Expand Up @@ -849,6 +850,9 @@ async def _action():
if cid not in on_devs:
self.debug(f"Sub-device disconnected: {cid}")
device.disconnected("Device is offline")
setattr(device, "sub_device_online", False)
else:
setattr(device, "sub_device_online", True)
except asyncio.CancelledError:
pass

Expand Down

0 comments on commit a34e316

Please sign in to comment.