Use new reauth helpers in overkiz (#128666)

* Use reauth_confirm in overkiz

* Just use new helpers
This commit is contained in:
epenet 2024-10-18 16:32:37 +02:00 committed by GitHub
parent 8c4b076746
commit d6703b20d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,7 +24,7 @@ from pyoverkiz.utils import generate_local_server, is_overkiz_gateway
import voluptuous as vol import voluptuous as vol
from homeassistant.components import dhcp, zeroconf 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 ( from homeassistant.const import (
CONF_HOST, CONF_HOST,
CONF_PASSWORD, CONF_PASSWORD,
@ -47,7 +47,6 @@ class OverkizConfigFlow(ConfigFlow, domain=DOMAIN):
VERSION = 1 VERSION = 1
_reauth_entry: ConfigEntry | None = None
_api_type: APIType = APIType.CLOUD _api_type: APIType = APIType.CLOUD
_user: str | None = None _user: str | None = None
_server: str = DEFAULT_SERVER _server: str = DEFAULT_SERVER
@ -174,27 +173,13 @@ class OverkizConfigFlow(ConfigFlow, domain=DOMAIN):
errors["base"] = "unknown" errors["base"] = "unknown"
LOGGER.exception("Unknown error") LOGGER.exception("Unknown error")
else: else:
if self._reauth_entry: if self.source == SOURCE_REAUTH:
if self._reauth_entry.unique_id != self.unique_id: self._abort_if_unique_id_mismatch(reason="reauth_wrong_account")
return self.async_abort(reason="reauth_wrong_account")
# Update existing entry during reauth return self.async_update_reload_and_abort(
self.hass.config_entries.async_update_entry( self._get_reauth_entry(), data_updates=user_input
self._reauth_entry,
data={
**self._reauth_entry.data,
**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 # Create new entry
self._abort_if_unique_id_configured() self._abort_if_unique_id_configured()
@ -257,27 +242,13 @@ class OverkizConfigFlow(ConfigFlow, domain=DOMAIN):
errors["base"] = "unknown" errors["base"] = "unknown"
LOGGER.exception("Unknown error") LOGGER.exception("Unknown error")
else: else:
if self._reauth_entry: if self.source == SOURCE_REAUTH:
if self._reauth_entry.unique_id != self.unique_id: self._abort_if_unique_id_mismatch(reason="reauth_wrong_account")
return self.async_abort(reason="reauth_wrong_account")
# Update existing entry during reauth return self.async_update_reload_and_abort(
self.hass.config_entries.async_update_entry( self._get_reauth_entry(), data_updates=user_input
self._reauth_entry,
data={
**self._reauth_entry.data,
**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 # Create new entry
self._abort_if_unique_id_configured() self._abort_if_unique_id_configured()
@ -346,22 +317,15 @@ class OverkizConfigFlow(ConfigFlow, domain=DOMAIN):
self, entry_data: Mapping[str, Any] self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Handle reauth.""" """Handle reauth."""
self._reauth_entry = cast(
ConfigEntry,
self.hass.config_entries.async_get_entry(self.context["entry_id"]),
)
# overkiz entries always have unique IDs # overkiz entries always have unique IDs
self.context["title_placeholders"] = { self.context["title_placeholders"] = {"gateway_id": cast(str, self.unique_id)}
"gateway_id": cast(str, self._reauth_entry.unique_id)
}
self._user = self._reauth_entry.data[CONF_USERNAME] self._user = entry_data[CONF_USERNAME]
self._server = self._reauth_entry.data[CONF_HUB] self._server = entry_data[CONF_HUB]
self._api_type = self._reauth_entry.data.get(CONF_API_TYPE, APIType.CLOUD) self._api_type = entry_data.get(CONF_API_TYPE, APIType.CLOUD)
if self._api_type == APIType.LOCAL: 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)) return await self.async_step_user(dict(entry_data))