Skip to content

Commit df491f5

Browse files
committed
Move WS commands to separate file
1 parent 8a54d35 commit df491f5

File tree

3 files changed

+77
-58
lines changed

3 files changed

+77
-58
lines changed

custom_components/mass_queue/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
)
3030
from .const import DOMAIN, LOGGER
3131
from .services import register_actions
32-
from .utils import api_download_and_encode_image, download_images
32+
from .websocket_commands import (
33+
api_download_and_encode_image,
34+
api_download_images,
35+
)
3336

3437
if TYPE_CHECKING:
3538
from homeassistant.core import HomeAssistant
@@ -121,7 +124,7 @@ async def on_hass_stop(event: Event) -> None: # noqa: ARG001
121124
actions = await setup_controller_and_actions(hass, mass, entry)
122125
register_actions(hass)
123126
entry.runtime_data = MusicAssistantQueueEntryData(mass, actions, listen_task)
124-
websocket_api.async_register_command(hass, download_images)
127+
websocket_api.async_register_command(hass, api_download_images)
125128
websocket_api.async_register_command(hass, api_download_and_encode_image)
126129

127130
# If the listen task is already failed, we need to raise ConfigEntryNotReady

custom_components/mass_queue/utils.py

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import urllib.parse
77
from typing import TYPE_CHECKING
88

9-
import voluptuous as vol
10-
from homeassistant.components import websocket_api
119
from homeassistant.config_entries import ConfigEntryState
1210
from homeassistant.core import callback
1311
from 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-
311276
async 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)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""Music Assistant Queue Actions Websocket Commands."""
2+
3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
6+
7+
import voluptuous as vol
8+
from homeassistant.components import websocket_api
9+
from homeassistant.helpers import aiohttp_client
10+
11+
if TYPE_CHECKING:
12+
from homeassistant.core import HomeAssistant
13+
14+
from .const import LOGGER
15+
from .utils import (
16+
download_and_encode_image,
17+
download_single_image_from_image_data,
18+
)
19+
20+
21+
@websocket_api.websocket_command(
22+
{
23+
vol.Required("type"): "mass_queue/download_and_encode_image",
24+
vol.Required("url"): str,
25+
},
26+
)
27+
@websocket_api.async_response
28+
async def api_download_and_encode_image(
29+
hass: HomeAssistant,
30+
connection: websocket_api.ActiveConnection,
31+
msg: dict,
32+
) -> None:
33+
"""Download images and return them as b64 encoded."""
34+
LOGGER.debug(f"Got message: {msg}")
35+
url = msg["url"]
36+
LOGGER.debug(f"URL: {url}")
37+
result = await download_and_encode_image(url, hass)
38+
connection.send_result(msg["id"], result)
39+
40+
41+
@websocket_api.websocket_command(
42+
{
43+
vol.Required("type"): "mass_queue/encode_images",
44+
vol.Required("entity_id"): str,
45+
vol.Required("images"): list,
46+
},
47+
)
48+
@websocket_api.async_response
49+
async def api_download_images(
50+
hass: HomeAssistant,
51+
connection: websocket_api.ActiveConnection,
52+
msg: dict,
53+
) -> None:
54+
"""Download images and return them as b64 encoded."""
55+
LOGGER.debug(f"Received message: {msg}")
56+
session = aiohttp_client.async_get_clientsession(hass)
57+
images = msg["images"]
58+
LOGGER.debug("Pulled images from message")
59+
LOGGER.debug(images)
60+
result = []
61+
entity_id = msg["entity_id"]
62+
for image in images:
63+
img = await download_single_image_from_image_data(
64+
image,
65+
entity_id,
66+
hass,
67+
session,
68+
)
69+
image["encoded"] = img
70+
result.append(image)
71+
connection.send_result(msg["id"], result)

0 commit comments

Comments
 (0)