diff --git a/homeassistant/components/twitch/config_flow.py b/homeassistant/components/twitch/config_flow.py index 7f006f194f5..dbaef59c236 100644 --- a/homeassistant/components/twitch/config_flow.py +++ b/homeassistant/components/twitch/config_flow.py @@ -9,7 +9,7 @@ from typing import Any, cast from twitchAPI.helper import first from twitchAPI.twitch import Twitch -from homeassistant.config_entries import ConfigEntry, ConfigFlowResult +from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult from homeassistant.const import CONF_ACCESS_TOKEN, CONF_TOKEN from homeassistant.helpers import config_entry_oauth2_flow from homeassistant.helpers.config_entry_oauth2_flow import LocalOAuth2Implementation @@ -23,7 +23,6 @@ class OAuth2FlowHandler( """Config flow to handle Twitch OAuth2 authentication.""" DOMAIN = DOMAIN - reauth_entry: ConfigEntry | None = None def __init__(self) -> None: """Initialize flow.""" @@ -63,8 +62,8 @@ class OAuth2FlowHandler( user_id = user.id - if not self.reauth_entry: - await self.async_set_unique_id(user_id) + await self.async_set_unique_id(user_id) + if self.source != SOURCE_REAUTH: self._abort_if_unique_id_configured() channels = [ @@ -76,38 +75,33 @@ class OAuth2FlowHandler( title=user.display_name, data=data, options={CONF_CHANNELS: channels} ) - if self.reauth_entry.unique_id == user_id: - new_channels = self.reauth_entry.options[CONF_CHANNELS] - # Since we could not get all channels at import, we do it at the reauth - # immediately after. - if "imported" in self.reauth_entry.data: - channels = [ - channel.broadcaster_login - async for channel in await client.get_followed_channels(user_id) - ] - options = list(set(channels) - set(new_channels)) - new_channels = [*new_channels, *options] - - self.hass.config_entries.async_update_entry( - self.reauth_entry, - data=data, - options={CONF_CHANNELS: new_channels}, - ) - await self.hass.config_entries.async_reload(self.reauth_entry.entry_id) - return self.async_abort(reason="reauth_successful") - - return self.async_abort( + reauth_entry = self._get_reauth_entry() + self._abort_if_unique_id_mismatch( reason="wrong_account", - description_placeholders={"title": self.reauth_entry.title}, + description_placeholders={"title": reauth_entry.title}, + ) + + new_channels = reauth_entry.options[CONF_CHANNELS] + # Since we could not get all channels at import, we do it at the reauth + # immediately after. + if "imported" in reauth_entry.data: + channels = [ + channel.broadcaster_login + async for channel in await client.get_followed_channels(user_id) + ] + options = list(set(channels) - set(new_channels)) + new_channels = [*new_channels, *options] + + return self.async_update_reload_and_abort( + reauth_entry, + data=data, + options={CONF_CHANNELS: new_channels}, ) 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(