Use reauth/reconfigure helpers in tedee config flow (#128025)

* Use reauth/reconfigure helpers in tedee config flow

* Also cleanup unnecessary reconfigure_confirm
This commit is contained in:
epenet 2024-10-14 18:01:47 +02:00 committed by GitHub
parent f9dfc64c6f
commit fdda0cc9cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 28 deletions

View File

@ -17,7 +17,6 @@ from homeassistant.components.webhook import async_generate_id as webhook_genera
from homeassistant.config_entries import ( from homeassistant.config_entries import (
SOURCE_REAUTH, SOURCE_REAUTH,
SOURCE_RECONFIGURE, SOURCE_RECONFIGURE,
ConfigEntry,
ConfigFlow, ConfigFlow,
ConfigFlowResult, ConfigFlowResult,
) )
@ -35,9 +34,6 @@ class TedeeConfigFlow(ConfigFlow, domain=DOMAIN):
VERSION = 1 VERSION = 1
MINOR_VERSION = 2 MINOR_VERSION = 2
reauth_entry: ConfigEntry
reconfigure_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
) -> ConfigFlowResult: ) -> ConfigFlowResult:
@ -46,7 +42,7 @@ class TedeeConfigFlow(ConfigFlow, domain=DOMAIN):
if user_input is not None: if user_input is not None:
if self.source == SOURCE_REAUTH: if self.source == SOURCE_REAUTH:
host = self.reauth_entry.data[CONF_HOST] host = self._get_reauth_entry().data[CONF_HOST]
else: else:
host = user_input[CONF_HOST] host = user_input[CONF_HOST]
local_access_token = user_input[CONF_LOCAL_ACCESS_TOKEN] local_access_token = user_input[CONF_LOCAL_ACCESS_TOKEN]
@ -65,19 +61,17 @@ class TedeeConfigFlow(ConfigFlow, domain=DOMAIN):
_LOGGER.error("Error during local bridge discovery: %s", exc) _LOGGER.error("Error during local bridge discovery: %s", exc)
errors["base"] = "cannot_connect" errors["base"] = "cannot_connect"
else: else:
await self.async_set_unique_id(local_bridge.serial)
if self.source == SOURCE_REAUTH: if self.source == SOURCE_REAUTH:
self._abort_if_unique_id_mismatch()
return self.async_update_reload_and_abort( return self.async_update_reload_and_abort(
self.reauth_entry, self._get_reauth_entry(), data_updates=user_input
data={**self.reauth_entry.data, **user_input},
reason="reauth_successful",
) )
if self.source == SOURCE_RECONFIGURE: if self.source == SOURCE_RECONFIGURE:
self._abort_if_unique_id_mismatch()
return self.async_update_reload_and_abort( return self.async_update_reload_and_abort(
self.reconfigure_entry, self._get_reconfigure_entry(), data_updates=user_input
data={**self.reconfigure_entry.data, **user_input},
reason="reconfigure_successful",
) )
await self.async_set_unique_id(local_bridge.serial)
self._abort_if_unique_id_configured() self._abort_if_unique_id_configured()
return self.async_create_entry( return self.async_create_entry(
title=NAME, title=NAME,
@ -103,7 +97,6 @@ class TedeeConfigFlow(ConfigFlow, domain=DOMAIN):
self, entry_data: Mapping[str, Any] self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Perform reauth upon an API authentication error.""" """Perform reauth upon an API authentication error."""
self.reauth_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(
@ -117,7 +110,9 @@ class TedeeConfigFlow(ConfigFlow, domain=DOMAIN):
{ {
vol.Required( vol.Required(
CONF_LOCAL_ACCESS_TOKEN, CONF_LOCAL_ACCESS_TOKEN,
default=self.reauth_entry.data[CONF_LOCAL_ACCESS_TOKEN], default=self._get_reauth_entry().data[
CONF_LOCAL_ACCESS_TOKEN
],
): str, ): str,
} }
), ),
@ -128,26 +123,18 @@ class TedeeConfigFlow(ConfigFlow, domain=DOMAIN):
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Perform a reconfiguration.""" """Perform a reconfiguration."""
self.reconfigure_entry = self._get_reconfigure_entry()
return await self.async_step_reconfigure_confirm()
async def async_step_reconfigure_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Add reconfigure step to allow to reconfigure a config entry."""
if not user_input: if not user_input:
reconfigure_entry = self._get_reconfigure_entry()
return self.async_show_form( return self.async_show_form(
step_id="reconfigure_confirm", step_id="reconfigure",
data_schema=vol.Schema( data_schema=vol.Schema(
{ {
vol.Required( vol.Required(
CONF_HOST, default=self.reconfigure_entry.data[CONF_HOST] CONF_HOST, default=reconfigure_entry.data[CONF_HOST]
): str, ): str,
vol.Required( vol.Required(
CONF_LOCAL_ACCESS_TOKEN, CONF_LOCAL_ACCESS_TOKEN,
default=self.reconfigure_entry.data[ default=reconfigure_entry.data[CONF_LOCAL_ACCESS_TOKEN],
CONF_LOCAL_ACCESS_TOKEN
],
): str, ): str,
} }
), ),

View File

@ -22,7 +22,7 @@
"local_access_token": "[%key:component::tedee::config::step::user::data_description::local_access_token%]" "local_access_token": "[%key:component::tedee::config::step::user::data_description::local_access_token%]"
} }
}, },
"reconfigure_confirm": { "reconfigure": {
"title": "Reconfigure Tedee", "title": "Reconfigure Tedee",
"description": "Update the settings of this integration.", "description": "Update the settings of this integration.",
"data": { "data": {

View File

@ -144,7 +144,7 @@ async def test_reconfigure_flow(
reconfigure_result = await mock_config_entry.start_reconfigure_flow(hass) reconfigure_result = await mock_config_entry.start_reconfigure_flow(hass)
assert reconfigure_result["type"] is FlowResultType.FORM assert reconfigure_result["type"] is FlowResultType.FORM
assert reconfigure_result["step_id"] == "reconfigure_confirm" assert reconfigure_result["step_id"] == "reconfigure"
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
reconfigure_result["flow_id"], reconfigure_result["flow_id"],