|
1 | 1 | """DataUpdateCoordinator for the YouTube integration."""
|
2 | 2 | from __future__ import annotations
|
3 | 3 |
|
4 |
| -import asyncio |
5 | 4 | from datetime import timedelta
|
6 | 5 | from typing import Any
|
7 | 6 |
|
@@ -53,39 +52,42 @@ def __init__(self, hass: HomeAssistant, auth: AsyncConfigEntryAuth) -> None:
|
53 | 52 | )
|
54 | 53 |
|
55 | 54 | async def _async_update_data(self) -> dict[str, Any]:
|
56 |
| - data = {} |
57 | 55 | service = await self._auth.get_resource()
|
58 | 56 | channels = self.config_entry.options[CONF_CHANNELS]
|
59 | 57 | channel_request: HttpRequest = service.channels().list(
|
60 | 58 | part="snippet,statistics", id=",".join(channels), maxResults=50
|
61 | 59 | )
|
62 | 60 | response: dict = await self.hass.async_add_executor_job(channel_request.execute)
|
63 | 61 |
|
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] |
65 | 80 | data[channel["id"]] = {
|
66 | 81 | ATTR_ID: channel["id"],
|
67 | 82 | ATTR_TITLE: channel["snippet"]["title"],
|
68 | 83 | 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 | + }, |
70 | 91 | ATTR_SUBSCRIBER_COUNT: int(channel["statistics"]["subscriberCount"]),
|
71 | 92 | }
|
72 |
| - |
73 |
| - await asyncio.gather(*[_compile_data(channel) for channel in response["items"]]) |
74 | 93 | 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