From 48df638f5d47f8182f9e82266b4e91a041ffd048 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Wed, 12 Apr 2023 23:36:51 +0100 Subject: [PATCH] Reduce startup time for System Bridge integration (#91171) --- .../components/system_bridge/__init__.py | 22 +++++++++++-------- .../components/system_bridge/config_flow.py | 2 +- .../components/system_bridge/coordinator.py | 5 +++-- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/system_bridge/__init__.py b/homeassistant/components/system_bridge/__init__.py index a8d3a4372caf7..05e607d56edb0 100644 --- a/homeassistant/components/system_bridge/__init__.py +++ b/homeassistant/components/system_bridge/__init__.py @@ -53,7 +53,10 @@ SERVICE_SEND_TEXT = "send_text" -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_setup_entry( + hass: HomeAssistant, + entry: ConfigEntry, +) -> bool: """Set up System Bridge from a config entry.""" # Check version before initialising @@ -64,11 +67,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: session=async_get_clientsession(hass), ) try: - if not await version.check_supported(): - raise ConfigEntryNotReady( - "You are not running a supported version of System Bridge. Please" - f" update to {SUPPORTED_VERSION} or higher." - ) + async with async_timeout.timeout(10): + if not await version.check_supported(): + raise ConfigEntryNotReady( + "You are not running a supported version of System Bridge. Please" + f" update to {SUPPORTED_VERSION} or higher." + ) except AuthenticationException as exception: _LOGGER.error("Authentication failed for %s: %s", entry.title, exception) raise ConfigEntryAuthFailed from exception @@ -87,7 +91,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: entry=entry, ) try: - async with async_timeout.timeout(30): + async with async_timeout.timeout(10): await coordinator.async_get_data(MODULES) except AuthenticationException as exception: _LOGGER.error("Authentication failed for %s: %s", entry.title, exception) @@ -105,8 +109,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: try: # Wait for initial data - async with async_timeout.timeout(30): - while not coordinator.is_ready(): + async with async_timeout.timeout(10): + while not coordinator.is_ready: _LOGGER.debug( "Waiting for initial data from %s (%s)", entry.title, diff --git a/homeassistant/components/system_bridge/config_flow.py b/homeassistant/components/system_bridge/config_flow.py index 46914c0ea152b..a73740e5dbdd5 100644 --- a/homeassistant/components/system_bridge/config_flow.py +++ b/homeassistant/components/system_bridge/config_flow.py @@ -55,7 +55,7 @@ async def _validate_input( data[CONF_API_KEY], ) try: - async with async_timeout.timeout(30): + async with async_timeout.timeout(15): await websocket_client.connect(session=async_get_clientsession(hass)) hass.async_create_task(websocket_client.listen()) response = await websocket_client.get_data(GetData(modules=["system"])) diff --git a/homeassistant/components/system_bridge/coordinator.py b/homeassistant/components/system_bridge/coordinator.py index 2810bcfac72a6..809e2a4fd50b2 100644 --- a/homeassistant/components/system_bridge/coordinator.py +++ b/homeassistant/components/system_bridge/coordinator.py @@ -82,6 +82,7 @@ def __init__( hass, LOGGER, name=DOMAIN, update_interval=timedelta(seconds=30) ) + @property def is_ready(self) -> bool: """Return if the data is ready.""" if self.data is None: @@ -157,7 +158,7 @@ async def _listen_for_data(self) -> None: self.last_update_success = False self.async_update_listeners() except (ConnectionClosedException, ConnectionResetError) as exception: - self.logger.info( + self.logger.debug( "Websocket connection closed for %s. Will retry: %s", self.title, exception, @@ -168,7 +169,7 @@ async def _listen_for_data(self) -> None: self.last_update_success = False self.async_update_listeners() except ConnectionErrorException as exception: - self.logger.warning( + self.logger.debug( "Connection error occurred for %s. Will retry: %s", self.title, exception,