From 4332309a449fbdb6abc76dea16abdd0f3f7074db Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 3 Oct 2024 22:40:51 +0200 Subject: [PATCH] Document get_reauth_entry and get_reconfigure_entry helpers (#2347) --- docs/config_entries_config_flow_handler.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/docs/config_entries_config_flow_handler.md b/docs/config_entries_config_flow_handler.md index 9f17ae98..52fc4cd3 100644 --- a/docs/config_entries_config_flow_handler.md +++ b/docs/config_entries_config_flow_handler.md @@ -291,6 +291,10 @@ class ExampleConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): ) ``` +Please note that checking whether you are in a reconfigure flow can be done using `if self.source == SOURCE_RECONFIGURE`. +It is also possible to access the corresponding config entry using `self._get_reconfigure_entry()`. + + ## Reauthentication Gracefully handling authentication errors such as invalid, expired, or revoked tokens is needed to advance on the [Integration Quality Scale](integration_quality_scale_index.md). This example of how to add reauth to the OAuth flow created by `script.scaffold` following the pattern in [Building a Python library](api_lib_auth.md#oauth2). @@ -328,15 +332,10 @@ class OAuth2FlowHandler( ): """Config flow to handle OAuth2 authentication.""" - reauth_entry: ConfigEntry | None = None - async def async_step_reauth( self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Perform reauth upon an API authentication error.""" - self.reauth_entry = self.hass.config_entries.async_get_entry( - self.context["entry_id"] - ) return await self.async_step_reauth_confirm() async def async_step_reauth_confirm( @@ -352,9 +351,9 @@ class OAuth2FlowHandler( async def async_oauth_create_entry(self, data: dict) -> dict: """Create an oauth config entry or update existing entry for reauth.""" - if self.reauth_entry: + if self.source == SOURCE_REAUTH: return self.async_update_reload_and_abort( - self.reauth_entry, + self._get_reauth_entry(), data=data, ) return await super().async_oauth_create_entry(data) @@ -388,6 +387,9 @@ Authentication failures (such as a revoked oauth token) can be a little tricky t Automated tests should verify that the reauth flow updates the existing config entry and does not create additional entries. +Please note that checking whether you are in a reauthentication flow can be done using `if self.source == SOURCE_REAUTH`. +It is also possible to access the corresponding config entry using `self._get_reauth_entry()`. + ## Testing your config flow Integrations with a config flow require full test coverage of all code in `config_flow.py` to be accepted into core. [Test your code](development_testing.md#running-a-limited-test-suite) includes more details on how to generate a coverage report.