Skip to content

Commit

Permalink
A collection of small bugfixes and optimizations (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt authored Feb 19, 2024
1 parent 0b16ed0 commit adc4e94
Show file tree
Hide file tree
Showing 20 changed files with 160 additions and 122 deletions.
4 changes: 2 additions & 2 deletions music_assistant/common/models/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from __future__ import annotations

import logging
from collections.abc import Iterable # noqa: TCH003
from collections.abc import Iterable
from dataclasses import dataclass
from types import NoneType
from typing import Any

from mashumaro import DataClassDictMixin

from music_assistant.common.models.enums import ProviderType # noqa: TCH001
from music_assistant.common.models.enums import ProviderType
from music_assistant.constants import (
CONF_AUTO_PLAY,
CONF_CROSSFADE,
Expand Down
3 changes: 2 additions & 1 deletion music_assistant/common/models/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Player(DataClassDictMixin):
"""Representation of a Player within Music Assistant."""

player_id: str
provider: str
provider: str # instance_id of the player provider
type: PlayerType
name: str
available: bool
Expand All @@ -51,6 +51,7 @@ class Player(DataClassDictMixin):
# active_source: return player_id of the active queue for this player
# if the player is grouped and a group is active, this will be set to the group's player_id
# otherwise it will be set to the own player_id
# can also be an actual different source if the player supports that
active_source: str | None = None

# current_item_id: return item_id/uri of the current active/loaded item on the player
Expand Down
4 changes: 2 additions & 2 deletions music_assistant/common/models/player_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

from mashumaro import DataClassDictMixin

from music_assistant.common.models.media_items import MediaItemType # noqa: TCH001
from music_assistant.common.models.media_items import MediaItemType

from .enums import PlayerState, RepeatMode
from .queue_item import QueueItem # noqa: TCH001
from .queue_item import QueueItem


@dataclass
Expand Down
4 changes: 2 additions & 2 deletions music_assistant/common/models/provider.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Models for providers and plugins in the MA ecosystem."""
from __future__ import annotations

import asyncio # noqa: TCH003
import asyncio
from dataclasses import dataclass, field
from typing import Any, TypedDict

from mashumaro.mixins.orjson import DataClassORJSONMixin

from music_assistant.common.helpers.json import load_json_file

from .enums import MediaType, ProviderFeature, ProviderType # noqa: TCH001
from .enums import MediaType, ProviderFeature, ProviderType


@dataclass
Expand Down
4 changes: 1 addition & 3 deletions music_assistant/server/controllers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,11 @@ async def reload_provider(self, instance_id: str) -> None:
async def get_player_configs(self, provider: str | None = None) -> list[PlayerConfig]:
"""Return all known player configurations, optionally filtered by provider domain."""
available_providers = {x.instance_id for x in self.mass.providers}
# add both domain and instance id
available_providers.update({x.domain for x in self.mass.providers})
return [
await self.get_player_config(raw_conf["player_id"])
for raw_conf in list(self.get(CONF_PLAYERS, {}).values())
# filter out unavailable providers
if self.mass.get_provider(raw_conf["provider"])
if raw_conf["provider"] in available_providers
# optional provider filter
and (provider in (None, raw_conf["provider"]))
]
Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/controllers/media/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import asyncio
import random
import time
from collections.abc import AsyncGenerator # noqa: TCH003
from collections.abc import AsyncGenerator
from typing import Any

from music_assistant.common.helpers.datetime import utc_timestamp
Expand Down
6 changes: 4 additions & 2 deletions music_assistant/server/controllers/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,11 @@ async def get_image_url_for_item(

return None

def get_image_url(self, image: MediaItemImage, size: int = 0) -> str:
def get_image_url(
self, image: MediaItemImage, size: int = 0, prefer_proxy: bool = False
) -> str:
"""Get (proxied) URL for MediaItemImage."""
if image.provider != "url":
if image.provider != "url" or prefer_proxy or size:
# return imageproxy url for images that need to be resolved
# the original path is double encoded
encoded_url = urllib.parse.quote(urllib.parse.quote(image.path))
Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/controllers/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import shutil
import statistics
from collections.abc import AsyncGenerator # noqa: TCH003
from collections.abc import AsyncGenerator
from contextlib import suppress
from itertools import zip_longest
from typing import TYPE_CHECKING
Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/controllers/player_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
import random
import time
from collections.abc import AsyncGenerator # noqa: TCH003
from collections.abc import AsyncGenerator
from contextlib import suppress
from typing import TYPE_CHECKING, Any

Expand Down
15 changes: 15 additions & 0 deletions music_assistant/server/controllers/players.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ def register(self, player: Player) -> None:
msg = f"Player {player_id} is already registered"
raise AlreadyRegisteredError(msg)

# make sure that the player's provider is set to the instance id
if prov := self.mass.get_provider(player.provider):
player.provider = prov.instance_id
else:
raise RuntimeError("Invalid provider ID given: %s", player.provider)

# make sure a default config exists
self.mass.config.create_default_player_config(
player_id, player.provider, player.name, player.enabled_by_default
Expand Down Expand Up @@ -203,6 +209,7 @@ def register_or_update(self, player: Player) -> None:
return

if player.player_id in self._players:
self._players[player.player_id] = player
self.update(player.player_id)
return

Expand Down Expand Up @@ -665,6 +672,12 @@ async def cmd_sync(self, player_id: str, target_player: str) -> None:
elif child_player.state == PlayerState.PLAYING:
# stop child player if it is currently playing
await self.cmd_stop(player_id)
if player_id not in parent_player.can_sync_with:
raise RuntimeError(
"Player %s can not be synced with %s",
child_player.display_name,
parent_player.display_name,
)
# all checks passed, forward command to the player provider
player_provider = self.get_player_provider(player_id)
await player_provider.cmd_sync(player_id, target_player)
Expand Down Expand Up @@ -695,6 +708,8 @@ async def cmd_unsync(self, player_id: str) -> None:
# all checks passed, forward command to the player provider
player_provider = self.get_player_provider(player_id)
await player_provider.cmd_unsync(player_id)
# reset active_source just in case
player.active_source = None

@api_command("players/create_group")
async def create_group(self, provider: str, name: str, members: list[str]) -> Player:
Expand Down
2 changes: 1 addition & 1 deletion music_assistant/server/controllers/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging
import time
import urllib.parse
from collections.abc import AsyncGenerator # noqa: TCH003
from collections.abc import AsyncGenerator
from contextlib import suppress
from typing import TYPE_CHECKING

Expand Down
Loading

0 comments on commit adc4e94

Please sign in to comment.