1 """Define a class to manage fetching Twitch data."""
3 from dataclasses
import dataclass
4 from datetime
import datetime, timedelta
6 from twitchAPI.helper
import first
7 from twitchAPI.object.api
import FollowedChannel, Stream, TwitchUser, UserSubscription
8 from twitchAPI.twitch
import Twitch
9 from twitchAPI.type
import TwitchAPIException, TwitchResourceNotFound
16 from .const
import CONF_CHANNELS, DOMAIN, LOGGER, OAUTH_SCOPES
19 def chunk_list(lst: list, chunk_size: int) -> list[list]:
20 """Split a list into chunks of chunk_size."""
21 return [lst[i : i + chunk_size]
for i
in range(0, len(lst), chunk_size)]
26 """Class for holding Twitch data."""
33 started_at: datetime |
None
34 stream_picture: str |
None
36 subscribed: bool |
None
37 subscription_gifted: bool |
None
38 subscription_tier: int |
None
40 following_since: datetime |
None
45 """Class to manage fetching Twitch data."""
47 config_entry: ConfigEntry
48 users: list[TwitchUser]
49 current_user: TwitchUser
52 self, hass: HomeAssistant, twitch: Twitch, session: OAuth2Session
54 """Initialize the coordinator."""
65 channels = self.
config_entryconfig_entry.options[CONF_CHANNELS]
69 self.
usersusers.extend(
70 [channel async
for channel
in self.
twitchtwitch.get_users(logins=chunk)]
72 if not (user := await first(self.
twitchtwitch.get_users())):
77 await self.
sessionsession.async_ensure_token_valid()
78 await self.
twitchtwitch.set_user_authentication(
79 self.
sessionsession.token[
"access_token"],
81 self.
sessionsession.token[
"refresh_token"],
84 data: dict[str, TwitchUpdate] = {}
85 streams: dict[str, Stream] = {
87 async
for s
in self.
twitchtwitch.get_followed_streams(
91 follows: dict[str, FollowedChannel] = {
93 async
for f
in await self.
twitchtwitch.get_followed_channels(
97 for channel
in self.
usersusers:
98 followers = await self.
twitchtwitch.get_channel_followers(channel.id)
99 stream = streams.get(channel.id)
100 follow = follows.get(channel.id)
101 sub: UserSubscription |
None =
None
103 sub = await self.
twitchtwitch.check_user_subscription(
104 user_id=self.
current_usercurrent_user.id, broadcaster_id=channel.id
106 except TwitchResourceNotFound:
107 LOGGER.debug(
"User is not subscribed to %s", channel.display_name)
108 except TwitchAPIException
as exc:
109 LOGGER.error(
"Error response on check_user_subscription: %s", exc)
112 channel.display_name,
115 stream.game_name
if stream
else None,
116 stream.title
if stream
else None,
117 stream.started_at
if stream
else None,
118 stream.thumbnail_url
if stream
else None,
119 channel.profile_image_url,
121 sub.is_gift
if sub
else None,
122 {
"1000": 1,
"2000": 2,
"3000": 3}.
get(sub.tier)
if sub
else None,
124 follow.followed_at
if follow
else None,
125 stream.viewer_count
if stream
else None,
None __init__(self, HomeAssistant hass, Twitch twitch, OAuth2Session session)
dict[str, TwitchUpdate] _async_update_data(self)
web.Response get(self, web.Request request, str config_key)
list[list] chunk_list(list lst, int chunk_size)