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."""
from __future__ import annotations
from typing import cast
from aiohttp.client_exceptions import ClientError, ClientResponseError
from twitchAPI.twitch import Twitch
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.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.config_entry_oauth2_flow import (
LocalOAuth2Implementation,
OAuth2Session,
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:
"""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)
try:
await session.async_ensure_token_valid()
@ -31,10 +37,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
except ClientError as err:
raise ConfigEntryNotReady from err
app_id = implementation.__dict__[CONF_CLIENT_ID]
access_token = entry.data[CONF_TOKEN][CONF_ACCESS_TOKEN]
client = await Twitch(
app_id=app_id,
app_id=implementation.client_id,
authenticate_app=False,
)
client.auto_refresh_auth = False

View File

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