1 """Config flow for Twitch."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
7 from typing
import Any, cast
9 from twitchAPI.helper
import first
10 from twitchAPI.twitch
import Twitch
17 from .const
import CONF_CHANNELS, DOMAIN, LOGGER, OAUTH_SCOPES
21 config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
23 """Config flow to handle Twitch OAuth2 authentication."""
28 """Initialize flow."""
30 self.data: dict[str, Any] = {}
39 """Extra data that needs to be appended to the authorize url."""
40 return {
"scope":
" ".join([scope.value
for scope
in OAUTH_SCOPES])}
45 ) -> ConfigFlowResult:
46 """Handle the initial step."""
47 implementation = cast(
48 LocalOAuth2Implementation,
53 app_id=implementation.client_id,
54 authenticate_app=
False,
56 client.auto_refresh_auth =
False
57 await client.set_user_authentication(
58 data[CONF_TOKEN][CONF_ACCESS_TOKEN], scope=OAUTH_SCOPES
60 user = await first(client.get_users())
65 await self.async_set_unique_id(user_id)
66 if self.source != SOURCE_REAUTH:
67 self._abort_if_unique_id_configured()
70 channel.broadcaster_login
71 async
for channel
in await client.get_followed_channels(user_id)
74 return self.async_create_entry(
75 title=user.display_name, data=data, options={CONF_CHANNELS: channels}
78 reauth_entry = self._get_reauth_entry()
79 self._abort_if_unique_id_mismatch(
80 reason=
"wrong_account",
81 description_placeholders={
82 "title": reauth_entry.title,
83 "username":
str(reauth_entry.unique_id),
87 new_channels = reauth_entry.options[CONF_CHANNELS]
90 if "imported" in reauth_entry.data:
92 channel.broadcaster_login
93 async
for channel
in await client.get_followed_channels(user_id)
95 options =
list(set(channels) - set(new_channels))
96 new_channels = [*new_channels, *options]
98 return self.async_update_reload_and_abort(
101 options={CONF_CHANNELS: new_channels},
105 self, entry_data: Mapping[str, Any]
106 ) -> ConfigFlowResult:
107 """Perform reauth upon an API authentication error."""
111 self, user_input: dict[str, Any] |
None =
None
112 ) -> ConfigFlowResult:
113 """Confirm reauth dialog."""
114 if user_input
is None:
115 return self.async_show_form(step_id=
"reauth_confirm")
116 return await self.async_step_user()
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
dict[str, Any] extra_authorize_data(self)
ConfigFlowResult async_oauth_create_entry(self, dict[str, Any] data)
logging.Logger logger(self)