Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions custom_components/mass_queue/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ty:ignore[unresolved-import]
"""Initialize component."""

from __future__ import annotations
Expand Down
12 changes: 7 additions & 5 deletions custom_components/mass_queue/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ty:ignore[unresolved-import]
"""Config flow for integration."""

from __future__ import annotations
Expand Down Expand Up @@ -67,12 +68,13 @@ def _parse_zeroconf_server_info(properties: dict[str, str]) -> ServerInfoMessage
)


def get_manual_schema(user_input: dict[str, Any]) -> vol.Schema:
def get_manual_schema(user_input: dict[str, Any] | None) -> vol.Schema:
"""Return a schema for the manual step."""
if type(user_input) is dict:
default_url = user_input.get(CONF_URL, DEFAULT_URL)
else:
default_url = DEFAULT_URL
default_url = (
user_input.get(CONF_URL, DEFAULT_URL)
if type(user_input) is dict
else DEFAULT_URL
)
return vol.Schema(
{
vol.Required(CONF_URL, default=default_url): str,
Expand Down
5 changes: 3 additions & 2 deletions custom_components/mass_queue/controller.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ty:ignore[unresolved-import]
"""Controller for queues, players cache."""

from __future__ import annotations
Expand Down Expand Up @@ -141,7 +142,7 @@ def update_player_queue(self, player_id: str):

async def send_command(self, command: str, data: dict | None = None):
"""Sends command to Music Assistant and returns response."""
data = data if data else {}
data = data or {}
return await self._client.send_command(command, require_schema=None, **data)

async def get_recommendations(self, providers: list | None = None):
Expand Down Expand Up @@ -378,7 +379,7 @@ async def process_image_single_item(self, queue_item: dict):
img_data = queue_item["media_item"]["metadata"]["images"][0]
url = generate_image_url_from_image_data(img_data, self._client)
LOGGER.debug(f"Downloading URL {url}")
result = await download_and_encode_image(url, self._hass)
result = await download_and_encode_image(url)
LOGGER.debug("Downloaded and setting")
queue_item["local_image_encoded"] = result
except Exception as e: # noqa: BLE001
Expand Down
1 change: 1 addition & 0 deletions custom_components/mass_queue/schemas.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ty:ignore[unresolved-import]
"""Schemas."""

from __future__ import annotations
Expand Down
1 change: 1 addition & 0 deletions custom_components/mass_queue/services.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ty:ignore[unresolved-import]
"""Service actions for mass_queue."""

from __future__ import annotations
Expand Down
19 changes: 13 additions & 6 deletions custom_components/mass_queue/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ty:ignore[unresolved-import]
"""Utilities."""

from __future__ import annotations
Expand All @@ -6,8 +7,10 @@
import urllib.parse
from typing import TYPE_CHECKING

from aiocache import cached
from aiocache.serializers import PickleSerializer
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import callback
from homeassistant.core import async_get_hass, callback
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers import device_registry as dr
Expand Down Expand Up @@ -118,7 +121,7 @@ def get_queue_id_from_player_data(player_data):
return current_media.get("queue_id")


def return_image_or_none(img_data: dict, remotely_accessible: bool):
def return_image_or_none(img_data: dict | None, remotely_accessible: bool):
"""Returns None if image is not present or not remotely accessible."""
if type(img_data) is dict:
img = img_data.get("path")
Expand Down Expand Up @@ -168,10 +171,12 @@ def find_image_from_artists(data: dict, remotely_accessible: bool):
"""Attempts to find the image via the artists key."""
artist = data.get("artist", {})
img_data = artist.get("image") or []
img_data += artist.get("metadata") or []
img_data += artist.get("metadata", {})
if len(img_data):
return search_image_list(img_data, remotely_accessible)
return return_image_or_none(img_data, remotely_accessible)
if isinstance(img_data, dict):
return return_image_or_none(img_data, remotely_accessible)
return None


def find_image(data: dict, remotely_accessible: bool = True):
Expand Down Expand Up @@ -233,7 +238,7 @@ def process_recommendation_section_items(items: list):
return [process_recommendation_section_item(item) for item in items]


def process_recommendation_section(section: dict):
def process_recommendation_section(section):
"""Process and reformat a single recommendation section."""
LOGGER.debug(f"Got section: {section}")
section = section.to_dict()
Expand Down Expand Up @@ -287,8 +292,10 @@ async def download_single_image_from_image_data(
return None


async def download_and_encode_image(url: str, hass: HomeAssistant):
@cached(serializer=PickleSerializer())
async def download_and_encode_image(url: str):
"""Downloads and encodes a single image from the given URL."""
hass = async_get_hass()
session = aiohttp_client.async_get_clientsession(hass)
req = await session.get(url)
read = await req.content.read()
Expand Down
5 changes: 3 additions & 2 deletions custom_components/mass_queue/websocket_commands.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ty:ignore[unresolved-import]
"""Music Assistant Queue Actions Websocket Commands."""

from __future__ import annotations
Expand Down Expand Up @@ -47,14 +48,14 @@ def api_get_entity_info(
)
@websocket_api.async_response
async def api_download_and_encode_image(
hass: HomeAssistant,
hass: HomeAssistant, # noqa: ARG001
connection: websocket_api.ActiveConnection,
msg: dict,
) -> None:
"""Download images and return them as b64 encoded."""
LOGGER.debug(f"Got message: {msg}")
url = msg["url"]
result = await download_and_encode_image(url, hass)
result = await download_and_encode_image(url)
connection.send_result(msg["id"], result)


Expand Down
Loading