Skip to content

Commit

Permalink
improved connection issue handling at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
msp1974 committed Apr 30, 2020
1 parent 329194d commit 8ae2f92
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions custom_components/wiser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,42 +126,45 @@ async def async_setup_entry(hass, config_entry):
config_entry.data[CONF_PASSWORD],
)

await data.async_connect()

@callback
def retryWiserHubSetup():
hass.async_create_task(wiserHubSetup())

async def wiserHubSetup():
_LOGGER.info("Initiating WiserHub connection")
try:
if await data.async_update():
if data.wiserhub.getDevices is None:
_LOGGER.error("No Wiser devices found to set up")
return False

hass.data[DOMAIN] = data

for platform in WISER_PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(
config_entry, platform
if await data.async_connect():
if await data.async_update():
if data.wiserhub.getDevices is None:
_LOGGER.error("No Wiser devices found to set up")
return False

hass.data[DOMAIN] = data

for platform in WISER_PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(
config_entry, platform
)
)
)

_LOGGER.info("Wiser Component Setup Completed")
return True
else:
await scheduleWiserHubSetup()
return True
_LOGGER.info("Wiser Component Setup Completed")
await data.async_update_device_registry()
return True
else:
await scheduleWiserHubSetup()
return True
except (asyncio.TimeoutError):
await scheduleWiserHubSetup()
return True
except WiserHubTimeoutException:
await scheduleWiserHubSetup()
return True
except Exception:
await scheduleWiserHubSetup()
return True

async def scheduleWiserHubSetup(interval=30):
async def scheduleWiserHubSetup(interval=10):
_LOGGER.error(
"Unable to connect to the Wiser Hub, retrying in {} seconds".format(
interval
Expand All @@ -170,8 +173,7 @@ async def scheduleWiserHubSetup(interval=30):
hass.loop.call_later(interval, retryWiserHubSetup)
return

hass.async_create_task(wiserHubSetup())
await data.async_update_device_registry()
await wiserHubSetup()
return True


Expand Down Expand Up @@ -230,6 +232,7 @@ async def async_connect(self):
self.wiserhub = await self._hass.async_add_executor_job(
partial(wiserHub, self.ip, self.secret)
)
return True

@callback
def do_hub_update(self):
Expand Down Expand Up @@ -264,7 +267,7 @@ async def async_update(self, no_throttle: bool = False):
dispatcher_send(self._hass, "WiserHubUpdateMessage")
return True
else:
_LOGGER.error("**Unable to update from wiser hub**")
_LOGGER.error("Unable to update from wiser hub")
return False
except json.decoder.JSONDecodeError as JSONex:
_LOGGER.error(
Expand All @@ -273,15 +276,11 @@ async def async_update(self, no_throttle: bool = False):
)
return False
except WiserHubTimeoutException as ex:
_LOGGER.error(
"***Failed to get update from Wiser hub due to timeout error***"
)
_LOGGER.error("Unable to update from Wiser hub due to timeout error")
_LOGGER.debug("Error is {}".format(ex))
return False
except Exception as ex:
_LOGGER.error(
"***Failed to get update from Wiser hub due to unknown error***"
)
_LOGGER.error("Unable to update from Wiser hub due to unknown error")
_LOGGER.debug("Error is {}".format(ex))
return False

Expand Down

0 comments on commit 8ae2f92

Please sign in to comment.