/
/
/
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 .config import get_weather_location_entries
16from .constants import CONF_AI_ENGINE, CONF_TTS_ENGINE
17
18if TYPE_CHECKING:
19 from music_assistant_models.config_entries import ConfigEntry
20
21 from music_assistant.models.setup_flow import SetupSession
22
23
24async def run_setup(session: SetupSession) -> None:
25 """
26 Run the AI Radio setup flow.
27
28 Collects the mandatory AI and text-to-speech engines the stations run on, plus the
29 optional weather location used by stations that reference weather placeholders.
30 The flow aborts when no plugin currently provides an AI or TTS engine.
31
32 :param session: The setup session driving the flow.
33 """
34 if not await get_ai_engines(session.mass):
35 raise AbortFlow("no_ai_engine")
36 if not await get_tts_engines(session.mass):
37 raise AbortFlow("no_tts_engine")
38 weather_city, weather_country = await get_weather_location_entries(
39 session.mass, session.context.instance_id
40 )
41 entries: list[ConfigEntry] = [
42 *await create_ai_engine_config_entries(session.mass, CONF_AI_ENGINE, required=True),
43 *await create_tts_engine_config_entries(session.mass, CONF_TTS_ENGINE, required=True),
44 weather_city,
45 weather_country,
46 ]
47 for entry in entries:
48 if (prefill := session.context.setup_data.get(entry.key)) is not None:
49 entry.value = prefill
50 values = await session.form(entries, step_id="user", last_step=True)
51 await session.finish(values)
52