From 2b11632253e7d3831310b297ef0a8b9a8630674b Mon Sep 17 00:00:00 2001 From: thekiwismarthome Date: Sun, 1 Mar 2026 22:24:14 +1300 Subject: [PATCH] feat: Improve image download robustness with a User-Agent header and extended timeout, and enhance WebP conversion by ensuring images are in RGB mode. --- .../shopping_list_manager/websocket/handlers.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/custom_components/shopping_list_manager/websocket/handlers.py b/custom_components/shopping_list_manager/websocket/handlers.py index 8f944b4..db71127 100644 --- a/custom_components/shopping_list_manager/websocket/handlers.py +++ b/custom_components/shopping_list_manager/websocket/handlers.py @@ -816,7 +816,8 @@ async def websocket_download_product_image( try: session = async_get_clientsession(hass) - async with session.get(raw_url, timeout=ClientTimeout(total=10)) as resp: + headers = {"User-Agent": "Mozilla/5.0 (compatible; HomeAssistant/ShoppingListManager)"} + async with session.get(raw_url, timeout=ClientTimeout(total=15), headers=headers) as resp: if resp.status != 200: connection.send_error(msg["id"], "download_failed", f"HTTP {resp.status}") return @@ -827,8 +828,14 @@ async def websocket_download_product_image( try: img = Image.open(io.BytesIO(raw)) - if img.mode not in ("RGB", "RGBA"): - img = img.convert("RGBA") + # Convert to RGB for reliable lossy WebP encoding + # (RGBA, palette, grayscale modes can fail or produce oversized files) + if img.mode == "RGBA": + bg = Image.new("RGB", img.size, (255, 255, 255)) + bg.paste(img, mask=img.split()[3]) + img = bg + elif img.mode != "RGB": + img = img.convert("RGB") img.thumbnail((IMAGE_SIZE, IMAGE_SIZE), Image.LANCZOS) out = io.BytesIO() img.save(out, format="WEBP", quality=IMAGE_QUALITY)