Skip to content

Commit

Permalink
Fix low-power devices woesn't goes unavailable when time exceed devic…
Browse files Browse the repository at this point in the history
…e sleep.
  • Loading branch information
xZetsubou committed Mar 18, 2024
1 parent f8ac92e commit 5eefe9c
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions custom_components/localtuya/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ def is_sleep(self):
"""Return whether the device is sleep or not."""
device_sleep = self._device_config.sleep_time
last_update = int(time.time()) - self._last_update_time
return last_update < device_sleep if device_sleep > 0 else False
is_sleep = last_update < device_sleep

return device_sleep > 0 and is_sleep

async def get_gateway(self):
"""Return the gateway device of this sub device."""
Expand Down Expand Up @@ -378,6 +380,9 @@ async def abort_connect(self):
await self._interface.close()
self._interface = None

if not self.is_sleep:
self._shutdown_entities()

async def check_connection(self):
"""Ensure that the device is not still connecting; if it is, wait for it."""
if not self.connected and self._connect_task:
Expand Down Expand Up @@ -493,6 +498,16 @@ def fire_event(event, data: dict):
data = {"dp": dpid_trigger, "value": dpid_value}
fire_event(event, data)

def _shutdown_entities(self, now=None):
"""Shutdown device entities"""
self._shutdown_entities_delay = None
if self.is_sleep:
return
if not self.connected:
self.debug(f"Disconnected: waiting for discovery broadcast", force=True)
signal = f"localtuya_{self._device_config.id}"
async_dispatcher_send(self._hass, signal, None)

@callback
def status_updated(self, status: dict):
"""Device updated status."""
Expand All @@ -510,16 +525,6 @@ def disconnected(self):
"""Device disconnected."""
sleep_time = self._device_config.sleep_time

def shutdown_entities(now=None):
"""Shutdown device entities"""
self._shutdown_entities_delay = None
if self.is_sleep:
return
if not self.connected:
self.debug(f"Disconnected: waiting for discovery broadcast", force=True)
signal = f"localtuya_{self._device_config.id}"
async_dispatcher_send(self._hass, signal, None)

if self._unsub_interval is not None:
self._unsub_interval()
self._unsub_interval = None
Expand All @@ -540,7 +545,7 @@ def shutdown_entities(now=None):
self._hass.add_job(self.async_connect())
if not self._is_closing:
self._shutdown_entities_delay = async_call_later(
self._hass, sleep_time + 3, shutdown_entities
self._hass, sleep_time + 3, self._shutdown_entities
)


Expand Down

0 comments on commit 5eefe9c

Please sign in to comment.