mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Split get users into chunks of 100 for Twitch sensors (#101211)
* Split get users into chunks of 100 * Move to own function
This commit is contained in:
parent
93033e037d
commit
98ca71fc96
@ -52,6 +52,11 @@ STATE_OFFLINE = "offline"
|
|||||||
STATE_STREAMING = "streaming"
|
STATE_STREAMING = "streaming"
|
||||||
|
|
||||||
|
|
||||||
|
def chunk_list(lst: list, chunk_size: int) -> list[list]:
|
||||||
|
"""Split a list into chunks of chunk_size."""
|
||||||
|
return [lst[i : i + chunk_size] for i in range(0, len(lst), chunk_size)]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
async def async_setup_platform(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config: ConfigType,
|
config: ConfigType,
|
||||||
@ -94,13 +99,20 @@ async def async_setup_entry(
|
|||||||
"""Initialize entries."""
|
"""Initialize entries."""
|
||||||
client = hass.data[DOMAIN][entry.entry_id]
|
client = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
async_add_entities(
|
channels = entry.options[CONF_CHANNELS]
|
||||||
[
|
|
||||||
TwitchSensor(channel, client)
|
entities: list[TwitchSensor] = []
|
||||||
async for channel in client.get_users(logins=entry.options[CONF_CHANNELS])
|
|
||||||
],
|
# Split channels into chunks of 100 to avoid hitting the rate limit
|
||||||
True,
|
for chunk in chunk_list(channels, 100):
|
||||||
)
|
entities.extend(
|
||||||
|
[
|
||||||
|
TwitchSensor(channel, client)
|
||||||
|
async for channel in client.get_users(logins=chunk)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
|
||||||
class TwitchSensor(SensorEntity):
|
class TwitchSensor(SensorEntity):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user