Skip to content

Commit

Permalink
Protect async_setup_entry loop with a try/catch for each instance, ot…
Browse files Browse the repository at this point in the history
…herwise compounded chance of failure is very high (fixes #175)
  • Loading branch information
xen2 authored and nao-pon committed Apr 1, 2024
1 parent 5167aac commit 465b3bc
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions custom_components/echonetlite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,22 @@ def unload_config_entry():

echonetlite = ECHONETConnector(instance, hass, entry)
try:
await echonetlite.async_update()
hass.data[DOMAIN][entry.entry_id].append(
{"instance": instance, "echonetlite": echonetlite}
)
# Since there is a small chance of failure, perform a few retry for each instance
# (otherwise, assuming 50 instances and 1% failure rate, setup would suceed in (1-0.01)^50 = 60% cases only)
for retry in range(1, 4):
try:
await echonetlite.async_update()
hass.data[DOMAIN][entry.entry_id].append(
{"instance": instance, "echonetlite": echonetlite}
)
break
except TimeoutError as ex:
_LOGGER.debug(
f"Setting up ECHONET Instance host {host} timed out. Retry {retry} of 3"
)
# if multiple error in a row, forward exception to outer loop
if retry == 3:
raise
except (TimeoutError, asyncio.CancelledError) as ex:
_LOGGER.debug(f"Connection error while connecting to {host}: {ex}")
raise ConfigEntryNotReady(
Expand Down

0 comments on commit 465b3bc

Please sign in to comment.