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