|
1 | 1 | """Utilities.""" |
2 | 2 |
|
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import base64 |
| 6 | +import urllib.parse |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +import voluptuous as vol |
| 10 | +from homeassistant.components import websocket_api |
| 11 | +from homeassistant.config_entries import ConfigEntryState |
| 12 | +from homeassistant.core import callback |
| 13 | +from homeassistant.exceptions import ServiceValidationError |
| 14 | +from homeassistant.helpers import aiohttp_client |
| 15 | +from homeassistant.helpers import entity_registry as er |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from homeassistant.core import HomeAssistant |
| 19 | + from music_assistant_client import MusicAssistantClient |
| 20 | + |
| 21 | + from . import MassQueueEntryData |
| 22 | + |
3 | 23 | from .const import LOGGER |
4 | 24 |
|
5 | 25 |
|
| 26 | +@callback |
| 27 | +def _get_config_entry( |
| 28 | + hass: HomeAssistant, |
| 29 | + config_entry_id: str, |
| 30 | +) -> MusicAssistantClient: |
| 31 | + """Get Music Assistant Client from config_entry_id.""" |
| 32 | + entry: MassQueueEntryData | None |
| 33 | + if not (entry := hass.config_entries.async_get_entry(config_entry_id)): |
| 34 | + exc = "Entry not found." |
| 35 | + raise ServiceValidationError(exc) |
| 36 | + if entry.state is not ConfigEntryState.LOADED: |
| 37 | + exc = "Entry not loaded" |
| 38 | + raise ServiceValidationError(exc) |
| 39 | + return entry |
| 40 | + |
| 41 | + |
| 42 | +def get_entity_actions_controller(hass, entity_id): |
| 43 | + """Gets the actions for the selected entity.""" |
| 44 | + mass_entry = get_mass_entry(hass, entity_id) |
| 45 | + mass = mass_entry.runtime_data.mass.connection.ws_server_url |
| 46 | + mass_queue_entry = find_mass_queue_entry(hass, mass) |
| 47 | + return mass_queue_entry.runtime_data.actions |
| 48 | + |
| 49 | + |
| 50 | +def get_mass_entry(hass, entity_id): |
| 51 | + """Helper function to pull MA Config Entry.""" |
| 52 | + config_id = _get_mass_entity_config_entry_id(hass, entity_id) |
| 53 | + return _get_config_entry(hass, config_id) |
| 54 | + |
| 55 | + |
| 56 | +def _get_mass_entity_config_entry_id(hass, entity_id): |
| 57 | + """Helper to grab config entry ID from entity ID.""" |
| 58 | + registry = er.async_get(hass) |
| 59 | + return registry.async_get(entity_id).config_entry_id |
| 60 | + |
| 61 | + |
| 62 | +def find_mass_queue_entry(hass, mass_url): |
| 63 | + """Finds the mass_queue entry for the given MA URL.""" |
| 64 | + entries = _get_mass_queue_entries(hass) |
| 65 | + for entry in entries: |
| 66 | + entry_url = entry.runtime_data.mass.connection.ws_server_url |
| 67 | + if entry_url == mass_url: |
| 68 | + return entry |
| 69 | + msg = f"Cannot find entry for Music Assistant at {mass_url}" |
| 70 | + raise ServiceValidationError(msg) |
| 71 | + |
| 72 | + |
| 73 | +def _get_mass_queue_entries(hass): |
| 74 | + """Gets all entries for mass_queue domain.""" |
| 75 | + entries = hass.config_entries.async_entries() |
| 76 | + return [entry for entry in entries if entry.domain == "mass_queue"] |
| 77 | + |
| 78 | + |
6 | 79 | def format_event_data_queue_item(queue_item): |
7 | 80 | """Format event data results for usage by controller.""" |
8 | 81 | if queue_item is None: |
@@ -173,3 +246,57 @@ def process_recommendations(recs: list): |
173 | 246 | if len(processed["items"]): |
174 | 247 | result.append(processed) |
175 | 248 | return result |
| 249 | + |
| 250 | + |
| 251 | +def _generate_image_url(image_data: dict, client): |
| 252 | + img_path = image_data["path"] |
| 253 | + provider = image_data["provider"] |
| 254 | + base_url = client.server_url |
| 255 | + img = urllib.parse.quote_plus(urllib.parse.quote_plus(img_path)) |
| 256 | + return f"{base_url}/imageproxy?provider={provider}&size=256&format=png&path={img}" |
| 257 | + |
| 258 | + |
| 259 | +async def _download_single_image(image_data: dict, entity_id, hass, session): |
| 260 | + """Downloads a single image from Music Assistant and returns the base64 encoded string.""" |
| 261 | + entry = get_mass_entry(hass, entity_id) |
| 262 | + client = entry.runtime_data.mass |
| 263 | + url = _generate_image_url(image_data, client) |
| 264 | + try: |
| 265 | + req = await session.get(url) |
| 266 | + read = await req.content.read() |
| 267 | + return f"data:image;base64,{base64.b64encode(read).decode('utf-8')}" |
| 268 | + except: # noqa: E722 |
| 269 | + LOGGER.error(f"Unable to get image with data {image_data}") |
| 270 | + return None |
| 271 | + |
| 272 | + |
| 273 | +@websocket_api.websocket_command( |
| 274 | + { |
| 275 | + vol.Required("type"): "mass_queue/encode_images", |
| 276 | + vol.Required("entity_id"): str, |
| 277 | + vol.Required("images"): list, |
| 278 | + }, |
| 279 | +) |
| 280 | +@websocket_api.async_response |
| 281 | +async def download_images( |
| 282 | + hass: HomeAssistant, |
| 283 | + connection: websocket_api.ActiveConnection, |
| 284 | + msg: dict, |
| 285 | +) -> None: |
| 286 | + """Download images and return them as b64 encoded.""" |
| 287 | + LOGGER.error(f"Received message: {msg}") |
| 288 | + session = aiohttp_client.async_get_clientsession(hass) |
| 289 | + LOGGER.error("Got session") |
| 290 | + images = msg["images"] |
| 291 | + LOGGER.error("Pulled images from message") |
| 292 | + LOGGER.error(images) |
| 293 | + result = [] |
| 294 | + entity_id = msg["entity_id"] |
| 295 | + for image in images: |
| 296 | + img = await _download_single_image(image, entity_id, hass, session) |
| 297 | + LOGGER.error(f"Downloaded image: {img}") |
| 298 | + image["encoded"] = img |
| 299 | + result.append(image) |
| 300 | + LOGGER.error("Final:") |
| 301 | + LOGGER.error(result) |
| 302 | + connection.send_result(msg["id"], result) |
0 commit comments