Skip to content

Commit

Permalink
Use ConfigEntry.runtime_data in Twitch (home-assistant#133337)
Browse files Browse the repository at this point in the history
* Use `ConfigEntry.runtime_data` in Twitch

* Process code review

* Process code review
  • Loading branch information
jpbede authored Dec 16, 2024
1 parent f2674f3 commit d78a24b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
14 changes: 6 additions & 8 deletions homeassistant/components/twitch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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
Expand All @@ -17,11 +16,11 @@
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,
Expand All @@ -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)
11 changes: 9 additions & 2 deletions homeassistant/components/twitch/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

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."""
Expand Down Expand Up @@ -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
Expand All @@ -58,6 +64,7 @@ def __init__(
LOGGER,
name=DOMAIN,
update_interval=timedelta(minutes=5),
config_entry=entry,
)
self.session = session

Expand Down
9 changes: 3 additions & 6 deletions homeassistant/components/twitch/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
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"
Expand All @@ -34,11 +31,11 @@

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
Expand Down
2 changes: 1 addition & 1 deletion tests/components/twitch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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
Expand Down
2 changes: 1 addition & 1 deletion tests/components/twitch/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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
Expand Down

0 comments on commit d78a24b

Please sign in to comment.