Skip to content

Commit

Permalink
events_active only when time_connection_lost>120s
Browse files Browse the repository at this point in the history
  • Loading branch information
starkillerOG committed Nov 30, 2024
1 parent b4d706f commit ff973db
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions reolink_aio/baichuan/baichuan.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def __init__(
self._keepalive_interval: float = KEEP_ALLIVE_INTERVAL
self._time_reestablish: float = 0
self._time_keepalive_increase: float = 0
self._time_connection_lost: float = 0
self._ext_callback: dict[int | None, dict[int | None, dict[str, Callable[[], None]]]] = {}

# http_cmd functions, set by the http_cmd decorator
Expand Down Expand Up @@ -283,19 +284,17 @@ def _close_callback(self) -> None:
if self.http_api is not None and self.http_api._updating:
_LOGGER.debug("Baichuan host %s: lost event subscription during firmware reboot", self._host)
return
now = time_now()
if self._protocol is not None:
now = time_now()
time_since_recv = now - self._protocol.time_recv
time_since_reestablish = now - self._time_reestablish
else:
now = 0
time_since_recv = 0
time_since_reestablish = 0
if time_since_reestablish > 150: # limit the amount of reconnects to prevent fast loops
if now - self._time_reestablish > 150: # limit the amount of reconnects to prevent fast loops
self._time_reestablish = now
self._loop.create_task(self._reestablish_connection(time_since_recv))
return

self._time_connection_lost = now
_LOGGER.error("Baichuan host %s: lost event subscription after %.2f s", self._host, time_since_recv)

async def _reestablish_connection(self, time_since_recv: float) -> None:
Expand All @@ -306,6 +305,7 @@ async def _reestablish_connection(self, time_since_recv: float) -> None:
except Exception as err:
_LOGGER.error("Baichuan host %s: lost event subscription after %.2f s and failed to reestablished connection", self._host, time_since_recv)
_LOGGER.debug("Baichuan host %s: failed to reestablished connection: %s", self._host, str(err))
self._time_connection_lost = time_start
else:
_LOGGER.debug("Baichuan host %s: lost event subscription after %.2f s, but reestablished connection immediately", self._host, time_since_recv)
if time_now() - time_start < 5:
Expand Down Expand Up @@ -735,7 +735,7 @@ async def SetDingDongCfg(self, **kwargs) -> None:

@property
def events_active(self) -> bool:
return self._events_active
return self._events_active and time_now() - self._time_connection_lost > 120

@property
def day_night_state(self) -> str | None:
Expand Down

0 comments on commit ff973db

Please sign in to comment.