From 7a63a143b958016a5a0cea3bfeb1f03ceb51cb5b Mon Sep 17 00:00:00 2001 From: BasThoP <94369925+BasThoP@users.noreply.github.com> Date: Mon, 2 Sep 2024 22:09:44 +0200 Subject: [PATCH] Reconnect API if connection is lost - Update __init__.py (#209) Added auto-connect to api if the internet connection or api is lost. --- .../gardena_smart_system/__init__.py | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/custom_components/gardena_smart_system/__init__.py b/custom_components/gardena_smart_system/__init__.py index 1c8f810..6b2b289 100644 --- a/custom_components/gardena_smart_system/__init__.py +++ b/custom_components/gardena_smart_system/__init__.py @@ -44,18 +44,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: client_id=entry.data[CONF_CLIENT_ID], client_secret=entry.data[CONF_CLIENT_SECRET], ) - - try: - await gardena_system.start() - except AccessDeniedError as ex: - _LOGGER.error('Got Access Denied Error when setting up Gardena Smart System: %s', ex) - return False - except InvalidClientError as ex: - _LOGGER.error('Got Invalid Client Error when setting up Gardena Smart System: %s', ex) - return False - except MissingTokenError as ex: - _LOGGER.error('Got Missing Token Error when setting up Gardena Smart System: %s', ex) - return False + while True: + try: + await gardena_system.start() + break # If connection is successful, return True + except ConnectionError: + await asyncio.sleep(60) # Wait for 60 seconds before trying to reconnect + except AccessDeniedError as ex: + _LOGGER.error('Got Access Denied Error when setting up Gardena Smart System: %s', ex) + return False + except InvalidClientError as ex: + _LOGGER.error('Got Invalid Client Error when setting up Gardena Smart System: %s', ex) + return False + except MissingTokenError as ex: + _LOGGER.error('Got Missing Token Error when setting up Gardena Smart System: %s', ex) + return False hass.data[DOMAIN][GARDENA_SYSTEM] = gardena_system