|
| 1 | +# ty:ignore[unresolved-import] |
1 | 2 | """Utilities.""" |
2 | 3 |
|
3 | 4 | from __future__ import annotations |
|
6 | 7 | import urllib.parse |
7 | 8 | from typing import TYPE_CHECKING |
8 | 9 |
|
| 10 | +from aiocache import cached |
| 11 | +from aiocache.serializers import PickleSerializer |
9 | 12 | from homeassistant.config_entries import ConfigEntryState |
10 | | -from homeassistant.core import callback |
| 13 | +from homeassistant.core import async_get_hass, callback |
11 | 14 | from homeassistant.exceptions import ServiceValidationError |
12 | 15 | from homeassistant.helpers import aiohttp_client |
13 | 16 | from homeassistant.helpers import device_registry as dr |
@@ -118,7 +121,7 @@ def get_queue_id_from_player_data(player_data): |
118 | 121 | return current_media.get("queue_id") |
119 | 122 |
|
120 | 123 |
|
121 | | -def return_image_or_none(img_data: dict, remotely_accessible: bool): |
| 124 | +def return_image_or_none(img_data: dict | None, remotely_accessible: bool): |
122 | 125 | """Returns None if image is not present or not remotely accessible.""" |
123 | 126 | if type(img_data) is dict: |
124 | 127 | img = img_data.get("path") |
@@ -168,10 +171,12 @@ def find_image_from_artists(data: dict, remotely_accessible: bool): |
168 | 171 | """Attempts to find the image via the artists key.""" |
169 | 172 | artist = data.get("artist", {}) |
170 | 173 | img_data = artist.get("image") or [] |
171 | | - img_data += artist.get("metadata") or [] |
| 174 | + img_data += artist.get("metadata", {}) |
172 | 175 | if len(img_data): |
173 | 176 | return search_image_list(img_data, remotely_accessible) |
174 | | - return return_image_or_none(img_data, remotely_accessible) |
| 177 | + if isinstance(img_data, dict): |
| 178 | + return return_image_or_none(img_data, remotely_accessible) |
| 179 | + return None |
175 | 180 |
|
176 | 181 |
|
177 | 182 | def find_image(data: dict, remotely_accessible: bool = True): |
@@ -233,7 +238,7 @@ def process_recommendation_section_items(items: list): |
233 | 238 | return [process_recommendation_section_item(item) for item in items] |
234 | 239 |
|
235 | 240 |
|
236 | | -def process_recommendation_section(section: dict): |
| 241 | +def process_recommendation_section(section): |
237 | 242 | """Process and reformat a single recommendation section.""" |
238 | 243 | LOGGER.debug(f"Got section: {section}") |
239 | 244 | section = section.to_dict() |
@@ -287,8 +292,10 @@ async def download_single_image_from_image_data( |
287 | 292 | return None |
288 | 293 |
|
289 | 294 |
|
290 | | -async def download_and_encode_image(url: str, hass: HomeAssistant): |
| 295 | +@cached(serializer=PickleSerializer()) |
| 296 | +async def download_and_encode_image(url: str): |
291 | 297 | """Downloads and encodes a single image from the given URL.""" |
| 298 | + hass = async_get_hass() |
292 | 299 | session = aiohttp_client.async_get_clientsession(hass) |
293 | 300 | req = await session.get(url) |
294 | 301 | read = await req.content.read() |
|
0 commit comments