Use reconfigure helpers in nam config flow (#128016)

This commit is contained in:
epenet 2024-10-09 15:40:47 +02:00 committed by GitHub
parent 3d1e57766a
commit 195398713b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -18,7 +18,7 @@ from nettigo_air_monitor import (
import voluptuous as vol import voluptuous as vol
from homeassistant.components import zeroconf from homeassistant.components import zeroconf
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -73,7 +73,6 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN):
VERSION = 1 VERSION = 1
_config: NamConfig _config: NamConfig
entry: ConfigEntry
host: str host: str
async def async_step_user( async def async_step_user(
@ -187,7 +186,6 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN):
self, entry_data: Mapping[str, Any] self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Handle configuration by re-auth.""" """Handle configuration by re-auth."""
self.entry = self._get_reauth_entry()
self.host = entry_data[CONF_HOST] self.host = entry_data[CONF_HOST]
self.context["title_placeholders"] = {"host": self.host} self.context["title_placeholders"] = {"host": self.host}
return await self.async_step_reauth_confirm() return await self.async_step_reauth_confirm()
@ -209,11 +207,9 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN):
): ):
return self.async_abort(reason="reauth_unsuccessful") return self.async_abort(reason="reauth_unsuccessful")
self.hass.config_entries.async_update_entry( return self.async_update_reload_and_abort(
self.entry, data={**user_input, CONF_HOST: self.host} self._get_reauth_entry(), data={**user_input, CONF_HOST: self.host}
) )
await self.hass.config_entries.async_reload(self.entry.entry_id)
return self.async_abort(reason="reauth_successful")
return self.async_show_form( return self.async_show_form(
step_id="reauth_confirm", step_id="reauth_confirm",
@ -226,8 +222,7 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN):
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Handle a reconfiguration flow initialized by the user.""" """Handle a reconfiguration flow initialized by the user."""
self.entry = self._get_reconfigure_entry() self.host = self._get_reconfigure_entry().data[CONF_HOST]
self.host = self.entry.data[CONF_HOST]
return await self.async_step_reconfigure_confirm() return await self.async_step_reconfigure_confirm()
@ -236,6 +231,7 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN):
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Handle a reconfiguration flow initialized by the user.""" """Handle a reconfiguration flow initialized by the user."""
errors = {} errors = {}
reconfigure_entry = self._get_reconfigure_entry()
if user_input is not None: if user_input is not None:
try: try:
@ -243,13 +239,12 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN):
except (ApiError, ClientConnectorError, TimeoutError): except (ApiError, ClientConnectorError, TimeoutError):
errors["base"] = "cannot_connect" errors["base"] = "cannot_connect"
else: else:
if format_mac(config.mac_address) != self.entry.unique_id: await self.async_set_unique_id(format_mac(config.mac_address))
return self.async_abort(reason="another_device") self._abort_if_unique_id_mismatch(reason="another_device")
data = {**self.entry.data, CONF_HOST: user_input[CONF_HOST]} return self.async_update_reload_and_abort(
self.hass.config_entries.async_update_entry(self.entry, data=data) reconfigure_entry, data_updates={CONF_HOST: user_input[CONF_HOST]}
await self.hass.config_entries.async_reload(self.entry.entry_id) )
return self.async_abort(reason="reconfigure_successful")
return self.async_show_form( return self.async_show_form(
step_id="reconfigure_confirm", step_id="reconfigure_confirm",
@ -258,6 +253,6 @@ class NAMFlowHandler(ConfigFlow, domain=DOMAIN):
vol.Required(CONF_HOST, default=self.host): str, vol.Required(CONF_HOST, default=self.host): str,
} }
), ),
description_placeholders={"device_name": self.entry.title}, description_placeholders={"device_name": reconfigure_entry.title},
errors=errors, errors=errors,
) )