/
/
/
1"""Setup flow for the AI Radio plugin."""
2
3from __future__ import annotations
4
5from typing import TYPE_CHECKING
6
7from music_assistant.helpers.plugin_engines import (
8 create_ai_engine_config_entries,
9 create_tts_engine_config_entries,
10 get_ai_engines,
11 get_tts_engines,
12)
13from music_assistant.models.setup_flow import AbortFlow
14
15from .constants import CONF_AI_ENGINE, CONF_TTS_ENGINE
16
17if TYPE_CHECKING:
18 from music_assistant_models.config_entries import ConfigEntry
19
20 from music_assistant.models.setup_flow import SetupSession
21
22
23async def run_setup(session: SetupSession) -> None:
24 """
25 Run the AI Radio setup flow.
26
27 Collects the AI and text-to-speech engines the stations run on. Both are
28 mandatory, so the flow aborts when no plugin currently provides one.
29
30 :param session: The setup session driving the flow.
31 """
32 if not await get_ai_engines(session.mass):
33 raise AbortFlow("no_ai_engine")
34 if not await get_tts_engines(session.mass):
35 raise AbortFlow("no_tts_engine")
36 entries: list[ConfigEntry] = [
37 *await create_ai_engine_config_entries(session.mass, CONF_AI_ENGINE, required=True),
38 *await create_tts_engine_config_entries(session.mass, CONF_TTS_ENGINE, required=True),
39 ]
40 for entry in entries:
41 if (prefill := session.context.setup_data.get(entry.key)) is not None:
42 entry.value = prefill
43 values = await session.form(entries, step_id="user", last_step=True)
44 await session.finish(values)
45