Use new reauth helpers in twitch (#128767)

This commit is contained in:
epenet 2024-10-19 14:26:49 +02:00 committed by GitHub
parent 7fc4a65868
commit b34ca9a521
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,7 +9,7 @@ from typing import Any, cast
from twitchAPI.helper import first from twitchAPI.helper import first
from twitchAPI.twitch import Twitch 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.const import CONF_ACCESS_TOKEN, CONF_TOKEN
from homeassistant.helpers import config_entry_oauth2_flow from homeassistant.helpers import config_entry_oauth2_flow
from homeassistant.helpers.config_entry_oauth2_flow import LocalOAuth2Implementation from homeassistant.helpers.config_entry_oauth2_flow import LocalOAuth2Implementation
@ -23,7 +23,6 @@ class OAuth2FlowHandler(
"""Config flow to handle Twitch OAuth2 authentication.""" """Config flow to handle Twitch OAuth2 authentication."""
DOMAIN = DOMAIN DOMAIN = DOMAIN
reauth_entry: ConfigEntry | None = None
def __init__(self) -> None: def __init__(self) -> None:
"""Initialize flow.""" """Initialize flow."""
@ -63,8 +62,8 @@ class OAuth2FlowHandler(
user_id = user.id 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() self._abort_if_unique_id_configured()
channels = [ channels = [
@ -76,11 +75,16 @@ class OAuth2FlowHandler(
title=user.display_name, data=data, options={CONF_CHANNELS: channels} title=user.display_name, data=data, options={CONF_CHANNELS: channels}
) )
if self.reauth_entry.unique_id == user_id: reauth_entry = self._get_reauth_entry()
new_channels = self.reauth_entry.options[CONF_CHANNELS] self._abort_if_unique_id_mismatch(
reason="wrong_account",
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 # Since we could not get all channels at import, we do it at the reauth
# immediately after. # immediately after.
if "imported" in self.reauth_entry.data: if "imported" in reauth_entry.data:
channels = [ channels = [
channel.broadcaster_login channel.broadcaster_login
async for channel in await client.get_followed_channels(user_id) async for channel in await client.get_followed_channels(user_id)
@ -88,26 +92,16 @@ class OAuth2FlowHandler(
options = list(set(channels) - set(new_channels)) options = list(set(channels) - set(new_channels))
new_channels = [*new_channels, *options] new_channels = [*new_channels, *options]
self.hass.config_entries.async_update_entry( return self.async_update_reload_and_abort(
self.reauth_entry, reauth_entry,
data=data, data=data,
options={CONF_CHANNELS: new_channels}, 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(
reason="wrong_account",
description_placeholders={"title": self.reauth_entry.title},
)
async def async_step_reauth( async def async_step_reauth(
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.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( async def async_step_reauth_confirm(