From 3d8cf4b98f718fe5c3c5448ae05bd8a2a5dd1fe8 Mon Sep 17 00:00:00 2001 From: SupaFlav <52390958+SupaFlav@users.noreply.github.com> Date: Fri, 8 Sep 2023 19:56:12 -0400 Subject: [PATCH] Add 5x reconnect attempts on poll --- custom_components/bluetti_bt/coordinator.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/custom_components/bluetti_bt/coordinator.py b/custom_components/bluetti_bt/coordinator.py index 2169ce8..06bda77 100644 --- a/custom_components/bluetti_bt/coordinator.py +++ b/custom_components/bluetti_bt/coordinator.py @@ -191,8 +191,18 @@ class PollingCoordinator(DataUpdateCoordinator): async with async_timeout.timeout(30): # Reconnect if not connected - if not self.client.is_connected: - await self.client.connect() + max_retries = 5 + for attempt in range(1,max_retries + 1): + try: + if not self.client.is_connected: + await self.client.connect() + break + except Exception as e: + if attempt == max_retries: + raise # pass exception on max_retries attempt + else: + self.logger.debug(f"Connect unsucessful (attempt {attempt}): {e}. Retrying...") + await asyncio.sleep(2) # Attach notifier if needed if not self.has_notifier: