Update OAuth reauth with simpler example (#1483)

This commit is contained in:
Allen Porter 2022-10-01 23:13:17 -07:00 committed by GitHub
parent 90bcb7f3f4
commit 23fa78ae52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -267,8 +267,13 @@ class OAuth2FlowHandler(
): ):
"""Config flow to handle OAuth2 authentication.""" """Config flow to handle OAuth2 authentication."""
reauth_entry: ConfigEntry | None = None
async def async_step_reauth(self, user_input=None): async def async_step_reauth(self, user_input=None):
"""Perform reauth upon an API authentication error.""" """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() return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm(self, user_input=None): async def async_step_reauth_confirm(self, user_input=None):
@ -282,12 +287,9 @@ class OAuth2FlowHandler(
async def async_oauth_create_entry(self, data: dict) -> dict: async def async_oauth_create_entry(self, data: dict) -> dict:
"""Create an oauth config entry or update existing entry for reauth.""" """Create an oauth config entry or update existing entry for reauth."""
# TODO: This example supports only a single config entry. Consider if self._reauth_entry:
# any special handling needed for multiple config entries. self.hass.config_entries.async_update_entry(self.reauth_entry, data=data)
existing_entry = await self.async_set_unique_id(DOMAIN) await self.hass.config_entries.async_reload(self.reauth_entry.entry_id)
if existing_entry:
self.hass.config_entries.async_update_entry(existing_entry, data=data)
await self.hass.config_entries.async_reload(existing_entry.entry_id)
return self.async_abort(reason="reauth_successful") return self.async_abort(reason="reauth_successful")
return await super().async_oauth_create_entry(data) return await super().async_oauth_create_entry(data)
``` ```