mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Use ConfigEntry.runtime_data
in Twitch (#133337)
* Use `ConfigEntry.runtime_data` in Twitch * Process code review * Process code review
This commit is contained in:
parent
f2674f3262
commit
d78a24ba33
@ -7,7 +7,6 @@ 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_TOKEN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
@ -17,11 +16,11 @@ from homeassistant.helpers.config_entry_oauth2_flow import (
|
||||
async_get_config_entry_implementation,
|
||||
)
|
||||
|
||||
from .const import DOMAIN, OAUTH_SCOPES, PLATFORMS
|
||||
from .coordinator import TwitchCoordinator
|
||||
from .const import OAUTH_SCOPES, PLATFORMS
|
||||
from .coordinator import TwitchConfigEntry, TwitchCoordinator
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: TwitchConfigEntry) -> bool:
|
||||
"""Set up Twitch from a config entry."""
|
||||
implementation = cast(
|
||||
LocalOAuth2Implementation,
|
||||
@ -47,18 +46,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
client.auto_refresh_auth = False
|
||||
await client.set_user_authentication(access_token, scope=OAUTH_SCOPES)
|
||||
|
||||
coordinator = TwitchCoordinator(hass, client, session)
|
||||
|
||||
coordinator = TwitchCoordinator(hass, client, session, entry)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: TwitchConfigEntry) -> bool:
|
||||
"""Unload Twitch config entry."""
|
||||
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
@ -15,6 +15,8 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
|
||||
|
||||
from .const import CONF_CHANNELS, DOMAIN, LOGGER, OAUTH_SCOPES
|
||||
|
||||
type TwitchConfigEntry = ConfigEntry[TwitchCoordinator]
|
||||
|
||||
|
||||
def chunk_list(lst: list, chunk_size: int) -> list[list]:
|
||||
"""Split a list into chunks of chunk_size."""
|
||||
@ -44,12 +46,16 @@ class TwitchUpdate:
|
||||
class TwitchCoordinator(DataUpdateCoordinator[dict[str, TwitchUpdate]]):
|
||||
"""Class to manage fetching Twitch data."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: TwitchConfigEntry
|
||||
users: list[TwitchUser]
|
||||
current_user: TwitchUser
|
||||
|
||||
def __init__(
|
||||
self, hass: HomeAssistant, twitch: Twitch, session: OAuth2Session
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
twitch: Twitch,
|
||||
session: OAuth2Session,
|
||||
entry: TwitchConfigEntry,
|
||||
) -> None:
|
||||
"""Initialize the coordinator."""
|
||||
self.twitch = twitch
|
||||
@ -58,6 +64,7 @@ class TwitchCoordinator(DataUpdateCoordinator[dict[str, TwitchUpdate]]):
|
||||
LOGGER,
|
||||
name=DOMAIN,
|
||||
update_interval=timedelta(minutes=5),
|
||||
config_entry=entry,
|
||||
)
|
||||
self.session = session
|
||||
|
||||
|
@ -5,15 +5,12 @@ from __future__ import annotations
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from . import TwitchCoordinator
|
||||
from .const import DOMAIN
|
||||
from .coordinator import TwitchUpdate
|
||||
from .coordinator import TwitchConfigEntry, TwitchCoordinator, TwitchUpdate
|
||||
|
||||
ATTR_GAME = "game"
|
||||
ATTR_TITLE = "title"
|
||||
@ -34,11 +31,11 @@ PARALLEL_UPDATES = 1
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: TwitchConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Initialize entries."""
|
||||
coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
TwitchSensor(coordinator, channel_id) for channel_id in coordinator.data
|
||||
|
@ -5,7 +5,7 @@ from typing import Any, Generic, TypeVar
|
||||
|
||||
from twitchAPI.object.base import TwitchObject
|
||||
|
||||
from homeassistant.components.twitch import DOMAIN
|
||||
from homeassistant.components.twitch.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry, load_json_array_fixture
|
||||
|
@ -7,7 +7,7 @@ from dateutil.tz import tzutc
|
||||
from twitchAPI.object.api import FollowedChannel, Stream, UserSubscription
|
||||
from twitchAPI.type import TwitchResourceNotFound
|
||||
|
||||
from homeassistant.components.twitch import DOMAIN
|
||||
from homeassistant.components.twitch.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import TwitchIterObject, get_generator_from_data, setup_integration
|
||||
|
Loading…
x
Reference in New Issue
Block a user