Skip to content

Commit

Permalink
Reconnect API if connection is lost - Update __init__.py (#209)
Browse files Browse the repository at this point in the history
Added auto-connect to api if the internet connection or api is lost.
  • Loading branch information
BasThoP authored Sep 2, 2024
1 parent 0c89d72 commit 7a63a14
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions custom_components/gardena_smart_system/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7a63a14

Please sign in to comment.