Use reauth/reconfigure helpers in shelly config flow (#128019)

This commit is contained in:
epenet 2024-10-09 21:05:45 +02:00 committed by GitHub
parent 9d7f0e77f1
commit fbec61662b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -146,7 +146,6 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
port: int = DEFAULT_HTTP_PORT port: int = DEFAULT_HTTP_PORT
info: dict[str, Any] = {} info: dict[str, Any] = {}
device_info: dict[str, Any] = {} device_info: dict[str, Any] = {}
entry: ConfigEntry
async def async_step_user( async def async_step_user(
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
@ -356,7 +355,6 @@ class ShellyConfigFlow(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()
return await self.async_step_reauth_confirm() return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm( async def async_step_reauth_confirm(
@ -364,8 +362,9 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Dialog that informs the user that reauth is required.""" """Dialog that informs the user that reauth is required."""
errors: dict[str, str] = {} errors: dict[str, str] = {}
host = self.entry.data[CONF_HOST] reauth_entry = self._get_reauth_entry()
port = get_http_port(self.entry.data) host = reauth_entry.data[CONF_HOST]
port = get_http_port(reauth_entry.data)
if user_input is not None: if user_input is not None:
try: try:
@ -373,7 +372,7 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
except (DeviceConnectionError, InvalidAuthError): except (DeviceConnectionError, InvalidAuthError):
return self.async_abort(reason="reauth_unsuccessful") return self.async_abort(reason="reauth_unsuccessful")
if get_device_entry_gen(self.entry) != 1: if get_device_entry_gen(reauth_entry) != 1:
user_input[CONF_USERNAME] = "admin" user_input[CONF_USERNAME] = "admin"
try: try:
await validate_input(self.hass, host, port, info, user_input) await validate_input(self.hass, host, port, info, user_input)
@ -381,10 +380,10 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
return self.async_abort(reason="reauth_unsuccessful") return self.async_abort(reason="reauth_unsuccessful")
return self.async_update_reload_and_abort( return self.async_update_reload_and_abort(
self.entry, data={**self.entry.data, **user_input} reauth_entry, data_updates=user_input
) )
if get_device_entry_gen(self.entry) in BLOCK_GENERATIONS: if get_device_entry_gen(reauth_entry) in BLOCK_GENERATIONS:
schema = { schema = {
vol.Required(CONF_USERNAME): str, vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str, vol.Required(CONF_PASSWORD): str,
@ -402,9 +401,9 @@ class ShellyConfigFlow(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() entry_data = self._get_reconfigure_entry().data
self.host = self.entry.data[CONF_HOST] self.host = entry_data[CONF_HOST]
self.port = self.entry.data.get(CONF_PORT, DEFAULT_HTTP_PORT) self.port = entry_data.get(CONF_PORT, DEFAULT_HTTP_PORT)
return await self.async_step_reconfigure_confirm() return await self.async_step_reconfigure_confirm()
@ -413,6 +412,7 @@ class ShellyConfigFlow(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:
host = user_input[CONF_HOST] host = user_input[CONF_HOST]
@ -424,13 +424,13 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
except CustomPortNotSupported: except CustomPortNotSupported:
errors["base"] = "custom_port_not_supported" errors["base"] = "custom_port_not_supported"
else: else:
if info[CONF_MAC] != self.entry.unique_id: await self.async_set_unique_id(info[CONF_MAC])
return self.async_abort(reason="another_device") self._abort_if_unique_id_mismatch(reason="another_device")
data = {**self.entry.data, CONF_HOST: host, CONF_PORT: port} return self.async_update_reload_and_abort(
self.hass.config_entries.async_update_entry(self.entry, data=data) reconfigure_entry,
await self.hass.config_entries.async_reload(self.entry.entry_id) data_updates={CONF_HOST: host, CONF_PORT: port},
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",
@ -440,7 +440,7 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
vol.Required(CONF_PORT, default=self.port): vol.Coerce(int), vol.Required(CONF_PORT, default=self.port): vol.Coerce(int),
} }
), ),
description_placeholders={"device_name": self.entry.title}, description_placeholders={"device_name": reconfigure_entry.title},
errors=errors, errors=errors,
) )