Skip to content

Commit 68c646b

Browse files
authored
Improve async youtube (home-assistant#93685)
* Improve async youtube * Improve async youtube
1 parent 25b5ad7 commit 68c646b

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

homeassistant/components/youtube/coordinator.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""DataUpdateCoordinator for the YouTube integration."""
22
from __future__ import annotations
33

4-
import asyncio
54
from datetime import timedelta
65
from typing import Any
76

@@ -53,39 +52,42 @@ def __init__(self, hass: HomeAssistant, auth: AsyncConfigEntryAuth) -> None:
5352
)
5453

5554
async def _async_update_data(self) -> dict[str, Any]:
56-
data = {}
5755
service = await self._auth.get_resource()
5856
channels = self.config_entry.options[CONF_CHANNELS]
5957
channel_request: HttpRequest = service.channels().list(
6058
part="snippet,statistics", id=",".join(channels), maxResults=50
6159
)
6260
response: dict = await self.hass.async_add_executor_job(channel_request.execute)
6361

64-
async def _compile_data(channel: dict[str, Any]) -> None:
62+
return await self.hass.async_add_executor_job(
63+
self._get_channel_data, service, response["items"]
64+
)
65+
66+
def _get_channel_data(
67+
self, service: Resource, channels: list[dict[str, Any]]
68+
) -> dict[str, Any]:
69+
data: dict[str, Any] = {}
70+
for channel in channels:
71+
playlist_id = get_upload_playlist_id(channel["id"])
72+
response = (
73+
service.playlistItems()
74+
.list(
75+
part="snippet,contentDetails", playlistId=playlist_id, maxResults=1
76+
)
77+
.execute()
78+
)
79+
video = response["items"][0]
6580
data[channel["id"]] = {
6681
ATTR_ID: channel["id"],
6782
ATTR_TITLE: channel["snippet"]["title"],
6883
ATTR_ICON: channel["snippet"]["thumbnails"]["high"]["url"],
69-
ATTR_LATEST_VIDEO: await self._get_latest_video(service, channel["id"]),
84+
ATTR_LATEST_VIDEO: {
85+
ATTR_PUBLISHED_AT: video["snippet"]["publishedAt"],
86+
ATTR_TITLE: video["snippet"]["title"],
87+
ATTR_DESCRIPTION: video["snippet"]["description"],
88+
ATTR_THUMBNAIL: video["snippet"]["thumbnails"]["standard"]["url"],
89+
ATTR_VIDEO_ID: video["contentDetails"]["videoId"],
90+
},
7091
ATTR_SUBSCRIBER_COUNT: int(channel["statistics"]["subscriberCount"]),
7192
}
72-
73-
await asyncio.gather(*[_compile_data(channel) for channel in response["items"]])
7493
return data
75-
76-
async def _get_latest_video(
77-
self, service: Resource, channel_id: str
78-
) -> dict[str, Any]:
79-
playlist_id = get_upload_playlist_id(channel_id)
80-
job: HttpRequest = service.playlistItems().list(
81-
part="snippet,contentDetails", playlistId=playlist_id, maxResults=1
82-
)
83-
response: dict = await self.hass.async_add_executor_job(job.execute)
84-
video = response["items"][0]
85-
return {
86-
ATTR_PUBLISHED_AT: video["snippet"]["publishedAt"],
87-
ATTR_TITLE: video["snippet"]["title"],
88-
ATTR_DESCRIPTION: video["snippet"]["description"],
89-
ATTR_THUMBNAIL: video["snippet"]["thumbnails"]["standard"]["url"],
90-
ATTR_VIDEO_ID: video["contentDetails"]["videoId"],
91-
}

0 commit comments

Comments
 (0)