From e949344dd95ae052d8fd5fa25abc205c1652efb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 16 May 2023 20:34:03 +0300 Subject: [PATCH] Huawei LTE connection management cleanups (#85929) * Disconnect rather than just logout at end of config flow Neither the connection or the requests session will be reused, so there's no reason just to logout. Do all of the connection closure so we get all of huawei-lte-api's cleanups explicitly done. * Name connect functions consistently, analoguous to disconnect --- homeassistant/components/huawei_lte/__init__.py | 4 ++-- .../components/huawei_lte/config_flow.py | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/huawei_lte/__init__.py b/homeassistant/components/huawei_lte/__init__.py index 5e5b2c8dc94..95197dcbb49 100644 --- a/homeassistant/components/huawei_lte/__init__.py +++ b/homeassistant/components/huawei_lte/__init__.py @@ -326,7 +326,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Huawei LTE component from config entry.""" url = entry.data[CONF_URL] - def get_connection() -> Connection: + def _connect() -> Connection: """Set up a connection.""" if entry.options.get(CONF_UNAUTHENTICATED_MODE): _LOGGER.debug("Connecting in unauthenticated mode, reduced feature set") @@ -341,7 +341,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return connection try: - connection = await hass.async_add_executor_job(get_connection) + connection = await hass.async_add_executor_job(_connect) except LoginErrorInvalidCredentialsException as ex: raise ConfigEntryAuthFailed from ex except Timeout as ex: diff --git a/homeassistant/components/huawei_lte/config_flow.py b/homeassistant/components/huawei_lte/config_flow.py index d4aa170f70d..f6c3b69ddeb 100644 --- a/homeassistant/components/huawei_lte/config_flow.py +++ b/homeassistant/components/huawei_lte/config_flow.py @@ -111,7 +111,7 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): errors=errors or {}, ) - async def _try_connect( + async def _connect( self, user_input: dict[str, Any], errors: dict[str, str] ) -> Connection | None: """Try connecting with given data.""" @@ -149,11 +149,11 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): return conn @staticmethod - def _logout(conn: Connection) -> None: + def _disconnect(conn: Connection) -> None: try: - conn.user_session.user.logout() # type: ignore[union-attr] + conn.close() except Exception: # pylint: disable=broad-except - _LOGGER.debug("Could not logout", exc_info=True) + _LOGGER.debug("Disconnect error", exc_info=True) async def async_step_user( self, user_input: dict[str, Any] | None = None @@ -197,7 +197,7 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): wlan_settings = {} return device_info, wlan_settings - conn = await self._try_connect(user_input, errors) + conn = await self._connect(user_input, errors) if errors: return await self._async_show_user_form( user_input=user_input, errors=errors @@ -207,7 +207,7 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): info, wlan_settings = await self.hass.async_add_executor_job( get_device_info, conn ) - await self.hass.async_add_executor_job(self._logout, conn) + await self.hass.async_add_executor_job(self._disconnect, conn) user_input.update( { @@ -298,9 +298,9 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): new_data = {**entry.data, **user_input} errors: dict[str, str] = {} - conn = await self._try_connect(new_data, errors) + conn = await self._connect(new_data, errors) if conn: - await self.hass.async_add_executor_job(self._logout, conn) + await self.hass.async_add_executor_job(self._disconnect, conn) if errors: return await self._async_show_reauth_form( user_input=user_input, errors=errors