music-assistant-server

3.6 KBPY
__init__.py
3.6 KB112 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_BASE_URL,
13    CONF_QUALITY,
14    CONF_TOKEN,
15    DEFAULT_BASE_URL,
16    QUALITY_HIGH,
17    QUALITY_LOSSLESS,
18)
19from .provider import KionMusicProvider
20
21if TYPE_CHECKING:
22    from music_assistant_models.config_entries import ProviderConfig
23    from music_assistant_models.provider import ProviderManifest
24
25    from music_assistant.mass import MusicAssistant
26    from music_assistant.models import ProviderInstanceType
27
28
29SUPPORTED_FEATURES = {
30    ProviderFeature.LIBRARY_ARTISTS,
31    ProviderFeature.LIBRARY_ALBUMS,
32    ProviderFeature.LIBRARY_TRACKS,
33    ProviderFeature.LIBRARY_PLAYLISTS,
34    ProviderFeature.ARTIST_ALBUMS,
35    ProviderFeature.ARTIST_TOPTRACKS,
36    ProviderFeature.SEARCH,
37    ProviderFeature.LIBRARY_ARTISTS_EDIT,
38    ProviderFeature.LIBRARY_ALBUMS_EDIT,
39    ProviderFeature.LIBRARY_TRACKS_EDIT,
40    ProviderFeature.BROWSE,
41    ProviderFeature.SIMILAR_TRACKS,
42    ProviderFeature.RECOMMENDATIONS,
43}
44
45
46async def setup(
47    mass: MusicAssistant, manifest: ProviderManifest, config: ProviderConfig
48) -> ProviderInstanceType:
49    """Initialize provider(instance) with given configuration."""
50    return KionMusicProvider(mass, manifest, config, SUPPORTED_FEATURES)
51
52
53async def get_config_entries(
54    mass: MusicAssistant,  # noqa: ARG001
55    instance_id: str | None = None,  # noqa: ARG001
56    action: str | None = None,
57    values: dict[str, ConfigValueType] | None = None,
58) -> tuple[ConfigEntry, ...]:
59    """Return Config entries to setup this provider."""
60    if values is None:
61        values = {}
62
63    # Handle clear auth action
64    if action == CONF_ACTION_CLEAR_AUTH:
65        values[CONF_TOKEN] = None
66
67    # Check if user is authenticated
68    is_authenticated = bool(values.get(CONF_TOKEN))
69
70    return (
71        ConfigEntry(
72            key=CONF_TOKEN,
73            type=ConfigEntryType.SECURE_STRING,
74            label="KION Music Token",
75            description="Enter your KION Music OAuth token. "
76            "See the documentation for how to obtain it.",
77            required=True,
78            hidden=is_authenticated,
79            value=cast("str", values.get(CONF_TOKEN)) if values else None,
80        ),
81        ConfigEntry(
82            key=CONF_ACTION_CLEAR_AUTH,
83            type=ConfigEntryType.ACTION,
84            label="Reset authentication",
85            description="Clear the current authentication details.",
86            action=CONF_ACTION_CLEAR_AUTH,
87            hidden=not is_authenticated,
88        ),
89        ConfigEntry(
90            key=CONF_QUALITY,
91            type=ConfigEntryType.STRING,
92            label="Audio quality",
93            description="Select preferred audio quality.",
94            options=[
95                ConfigValueOption("High (320 kbps)", QUALITY_HIGH),
96                ConfigValueOption("Lossless (FLAC)", QUALITY_LOSSLESS),
97            ],
98            default_value=QUALITY_HIGH,
99        ),
100        ConfigEntry(
101            key=CONF_BASE_URL,
102            type=ConfigEntryType.STRING,
103            label="API Base URL",
104            description="API endpoint base URL. "
105            "Only change if KION Music changes their API endpoint. "
106            "Default: https://music.mts.ru/ya_proxy_api",
107            default_value=DEFAULT_BASE_URL,
108            required=False,
109            advanced=True,
110        ),
111    )
112