66import urllib .parse
77from typing import TYPE_CHECKING
88
9- import voluptuous as vol
10- from homeassistant .components import websocket_api
119from homeassistant .config_entries import ConfigEntryState
1210from homeassistant .core import callback
1311from homeassistant .exceptions import ServiceValidationError
@@ -256,7 +254,7 @@ def generate_image_url_from_image_data(image_data: dict, client):
256254 return f"{ base_url } /imageproxy?provider={ provider } &size=256&format=png&path={ img } "
257255
258256
259- async def _download_single_image_from_image_data (
257+ async def download_single_image_from_image_data (
260258 image_data : dict ,
261259 entity_id ,
262260 hass ,
@@ -275,62 +273,9 @@ async def _download_single_image_from_image_data(
275273 return None
276274
277275
278- @websocket_api .websocket_command (
279- {
280- vol .Required ("type" ): "mass_queue/encode_images" ,
281- vol .Required ("entity_id" ): str ,
282- vol .Required ("images" ): list ,
283- },
284- )
285- @websocket_api .async_response
286- async def download_images (
287- hass : HomeAssistant ,
288- connection : websocket_api .ActiveConnection ,
289- msg : dict ,
290- ) -> None :
291- """Download images and return them as b64 encoded."""
292- LOGGER .debug (f"Received message: { msg } " )
293- session = aiohttp_client .async_get_clientsession (hass )
294- images = msg ["images" ]
295- LOGGER .debug ("Pulled images from message" )
296- LOGGER .debug (images )
297- result = []
298- entity_id = msg ["entity_id" ]
299- for image in images :
300- img = await _download_single_image_from_image_data (
301- image ,
302- entity_id ,
303- hass ,
304- session ,
305- )
306- image ["encoded" ] = img
307- result .append (image )
308- connection .send_result (msg ["id" ], result )
309-
310-
311276async def download_and_encode_image (url : str , hass : HomeAssistant ):
312277 """Downloads and encodes a single image from the given URL."""
313278 session = aiohttp_client .async_get_clientsession (hass )
314279 req = await session .get (url )
315280 read = await req .content .read ()
316281 return f"data:image;base64,{ base64 .b64encode (read ).decode ('utf-8' )} "
317-
318-
319- @websocket_api .websocket_command (
320- {
321- vol .Required ("type" ): "mass_queue/download_and_encode_image" ,
322- vol .Required ("url" ): str ,
323- },
324- )
325- @websocket_api .async_response
326- async def api_download_and_encode_image (
327- hass : HomeAssistant ,
328- connection : websocket_api .ActiveConnection ,
329- msg : dict ,
330- ) -> None :
331- """Download images and return them as b64 encoded."""
332- LOGGER .debug (f"Got message: { msg } " )
333- url = msg ["url" ]
334- LOGGER .debug (f"URL: { url } " )
335- result = await download_and_encode_image (url , hass )
336- connection .send_result (msg ["id" ], result )
0 commit comments