music-assistant-server

3 KBPY
__init__.py
3 KB97 lines • python
1"""KION Music provider support for Music Assistant."""
2
3from __future__ import annotations
4
5from typing import TYPE_CHECKING, cast
6
7from music_assistant_models.config_entries import ConfigEntry, ConfigValueOption, ConfigValueType
8from music_assistant_models.enums import ConfigEntryType, ProviderFeature
9
10from .constants import (
11    CONF_ACTION_CLEAR_AUTH,
12    CONF_QUALITY,
13    CONF_TOKEN,
14    QUALITY_HIGH,
15    QUALITY_LOSSLESS,
16)
17from .provider import KionMusicProvider
18
19if TYPE_CHECKING:
20    from music_assistant_models.config_entries import ProviderConfig
21    from music_assistant_models.provider import ProviderManifest
22
23    from music_assistant.mass import MusicAssistant
24    from music_assistant.models import ProviderInstanceType
25
26
27SUPPORTED_FEATURES = {
28    ProviderFeature.LIBRARY_ARTISTS,
29    ProviderFeature.LIBRARY_ALBUMS,
30    ProviderFeature.LIBRARY_TRACKS,
31    ProviderFeature.LIBRARY_PLAYLISTS,
32    ProviderFeature.ARTIST_ALBUMS,
33    ProviderFeature.ARTIST_TOPTRACKS,
34    ProviderFeature.SEARCH,
35    ProviderFeature.LIBRARY_ARTISTS_EDIT,
36    ProviderFeature.LIBRARY_ALBUMS_EDIT,
37    ProviderFeature.LIBRARY_TRACKS_EDIT,
38    ProviderFeature.BROWSE,
39}
40
41
42async def setup(
43    mass: MusicAssistant, manifest: ProviderManifest, config: ProviderConfig
44) -> ProviderInstanceType:
45    """Initialize provider(instance) with given configuration."""
46    return KionMusicProvider(mass, manifest, config, SUPPORTED_FEATURES)
47
48
49async def get_config_entries(
50    mass: MusicAssistant,  # noqa: ARG001
51    instance_id: str | None = None,  # noqa: ARG001
52    action: str | None = None,
53    values: dict[str, ConfigValueType] | None = None,
54) -> tuple[ConfigEntry, ...]:
55    """Return Config entries to setup this provider."""
56    if values is None:
57        values = {}
58
59    # Handle clear auth action
60    if action == CONF_ACTION_CLEAR_AUTH:
61        values[CONF_TOKEN] = None
62
63    # Check if user is authenticated
64    is_authenticated = bool(values.get(CONF_TOKEN))
65
66    return (
67        ConfigEntry(
68            key=CONF_TOKEN,
69            type=ConfigEntryType.SECURE_STRING,
70            label="KION Music Token",
71            description="Enter your KION Music OAuth token. "
72            "See the documentation for how to obtain it.",
73            required=True,
74            hidden=is_authenticated,
75            value=cast("str", values.get(CONF_TOKEN)) if values else None,
76        ),
77        ConfigEntry(
78            key=CONF_ACTION_CLEAR_AUTH,
79            type=ConfigEntryType.ACTION,
80            label="Reset authentication",
81            description="Clear the current authentication details.",
82            action=CONF_ACTION_CLEAR_AUTH,
83            hidden=not is_authenticated,
84        ),
85        ConfigEntry(
86            key=CONF_QUALITY,
87            type=ConfigEntryType.STRING,
88            label="Audio quality",
89            description="Select preferred audio quality.",
90            options=[
91                ConfigValueOption("High (320 kbps)", QUALITY_HIGH),
92                ConfigValueOption("Lossless (FLAC)", QUALITY_LOSSLESS),
93            ],
94            default_value=QUALITY_HIGH,
95        ),
96    )
97