Clean up twitch setup (#110849)

* Clean up twitch setup

* Clean up twitch setup
This commit is contained in:
Joost Lekkerkerker 2024-02-18 13:16:22 +01:00 committed by GitHub
parent babb436512
commit e2ab44903c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 6 deletions

View File

@ -1,14 +1,17 @@
"""The Twitch component.""" """The Twitch component."""
from __future__ import annotations from __future__ import annotations
from typing import cast
from aiohttp.client_exceptions import ClientError, ClientResponseError from aiohttp.client_exceptions import ClientError, ClientResponseError
from twitchAPI.twitch import Twitch from twitchAPI.twitch import Twitch
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_CLIENT_ID, CONF_TOKEN from homeassistant.const import CONF_ACCESS_TOKEN, CONF_TOKEN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.config_entry_oauth2_flow import ( from homeassistant.helpers.config_entry_oauth2_flow import (
LocalOAuth2Implementation,
OAuth2Session, OAuth2Session,
async_get_config_entry_implementation, async_get_config_entry_implementation,
) )
@ -18,7 +21,10 @@ from .const import DOMAIN, OAUTH_SCOPES, PLATFORMS
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Twitch from a config entry.""" """Set up Twitch from a config entry."""
implementation = await async_get_config_entry_implementation(hass, entry) implementation = cast(
LocalOAuth2Implementation,
await async_get_config_entry_implementation(hass, entry),
)
session = OAuth2Session(hass, entry, implementation) session = OAuth2Session(hass, entry, implementation)
try: try:
await session.async_ensure_token_valid() await session.async_ensure_token_valid()
@ -31,10 +37,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
except ClientError as err: except ClientError as err:
raise ConfigEntryNotReady from err raise ConfigEntryNotReady from err
app_id = implementation.__dict__[CONF_CLIENT_ID]
access_token = entry.data[CONF_TOKEN][CONF_ACCESS_TOKEN] access_token = entry.data[CONF_TOKEN][CONF_ACCESS_TOKEN]
client = await Twitch( client = await Twitch(
app_id=app_id, app_id=implementation.client_id,
authenticate_app=False, authenticate_app=False,
) )
client.auto_refresh_auth = False client.auto_refresh_auth = False

View File

@ -3,7 +3,7 @@ from __future__ import annotations
from collections.abc import Mapping from collections.abc import Mapping
import logging import logging
from typing import Any 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
@ -14,6 +14,7 @@ from homeassistant.const import CONF_ACCESS_TOKEN, CONF_CLIENT_ID, CONF_TOKEN
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN
from homeassistant.data_entry_flow import AbortFlow, FlowResult from homeassistant.data_entry_flow import AbortFlow, FlowResult
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.issue_registry import IssueSeverity, async_create_issue from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from .const import CONF_CHANNELS, CONF_REFRESH_TOKEN, DOMAIN, LOGGER, OAUTH_SCOPES from .const import CONF_CHANNELS, CONF_REFRESH_TOKEN, DOMAIN, LOGGER, OAUTH_SCOPES
@ -47,9 +48,13 @@ class OAuth2FlowHandler(
data: dict[str, Any], data: dict[str, Any],
) -> FlowResult: ) -> FlowResult:
"""Handle the initial step.""" """Handle the initial step."""
implementation = cast(
LocalOAuth2Implementation,
self.flow_impl,
)
client = await Twitch( client = await Twitch(
app_id=self.flow_impl.__dict__[CONF_CLIENT_ID], app_id=implementation.client_id,
authenticate_app=False, authenticate_app=False,
) )
client.auto_refresh_auth = False client.auto_refresh_auth = False