From d6703b20d3d8766c464419b480d088c9b6fe8bad Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:32:37 +0200 Subject: [PATCH] Use new reauth helpers in overkiz (#128666) * Use reauth_confirm in overkiz * Just use new helpers --- .../components/overkiz/config_flow.py | 64 ++++--------------- 1 file changed, 14 insertions(+), 50 deletions(-) diff --git a/homeassistant/components/overkiz/config_flow.py b/homeassistant/components/overkiz/config_flow.py index 4b88cd4a3e8..471a13d0de2 100644 --- a/homeassistant/components/overkiz/config_flow.py +++ b/homeassistant/components/overkiz/config_flow.py @@ -24,7 +24,7 @@ from pyoverkiz.utils import generate_local_server, is_overkiz_gateway import voluptuous as vol from homeassistant.components import dhcp, zeroconf -from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult +from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult from homeassistant.const import ( CONF_HOST, CONF_PASSWORD, @@ -47,7 +47,6 @@ class OverkizConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - _reauth_entry: ConfigEntry | None = None _api_type: APIType = APIType.CLOUD _user: str | None = None _server: str = DEFAULT_SERVER @@ -174,27 +173,13 @@ class OverkizConfigFlow(ConfigFlow, domain=DOMAIN): errors["base"] = "unknown" LOGGER.exception("Unknown error") else: - if self._reauth_entry: - if self._reauth_entry.unique_id != self.unique_id: - return self.async_abort(reason="reauth_wrong_account") + if self.source == SOURCE_REAUTH: + self._abort_if_unique_id_mismatch(reason="reauth_wrong_account") - # Update existing entry during reauth - self.hass.config_entries.async_update_entry( - self._reauth_entry, - data={ - **self._reauth_entry.data, - **user_input, - }, + return self.async_update_reload_and_abort( + self._get_reauth_entry(), data_updates=user_input ) - self.hass.async_create_task( - self.hass.config_entries.async_reload( - self._reauth_entry.entry_id - ) - ) - - return self.async_abort(reason="reauth_successful") - # Create new entry self._abort_if_unique_id_configured() @@ -257,27 +242,13 @@ class OverkizConfigFlow(ConfigFlow, domain=DOMAIN): errors["base"] = "unknown" LOGGER.exception("Unknown error") else: - if self._reauth_entry: - if self._reauth_entry.unique_id != self.unique_id: - return self.async_abort(reason="reauth_wrong_account") + if self.source == SOURCE_REAUTH: + self._abort_if_unique_id_mismatch(reason="reauth_wrong_account") - # Update existing entry during reauth - self.hass.config_entries.async_update_entry( - self._reauth_entry, - data={ - **self._reauth_entry.data, - **user_input, - }, + return self.async_update_reload_and_abort( + self._get_reauth_entry(), data_updates=user_input ) - self.hass.async_create_task( - self.hass.config_entries.async_reload( - self._reauth_entry.entry_id - ) - ) - - return self.async_abort(reason="reauth_successful") - # Create new entry self._abort_if_unique_id_configured() @@ -346,22 +317,15 @@ class OverkizConfigFlow(ConfigFlow, domain=DOMAIN): self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Handle reauth.""" - self._reauth_entry = cast( - ConfigEntry, - self.hass.config_entries.async_get_entry(self.context["entry_id"]), - ) - # overkiz entries always have unique IDs - self.context["title_placeholders"] = { - "gateway_id": cast(str, self._reauth_entry.unique_id) - } + self.context["title_placeholders"] = {"gateway_id": cast(str, self.unique_id)} - self._user = self._reauth_entry.data[CONF_USERNAME] - self._server = self._reauth_entry.data[CONF_HUB] - self._api_type = self._reauth_entry.data.get(CONF_API_TYPE, APIType.CLOUD) + self._user = entry_data[CONF_USERNAME] + self._server = entry_data[CONF_HUB] + self._api_type = entry_data.get(CONF_API_TYPE, APIType.CLOUD) if self._api_type == APIType.LOCAL: - self._host = self._reauth_entry.data[CONF_HOST] + self._host = entry_data[CONF_HOST] return await self.async_step_user(dict(entry_data))