/
/
1"""Helpers for building shared configuration entries."""
2
3from __future__ import annotations
4
5from typing import TYPE_CHECKING
6
7from music_assistant_models.config_entries import ConfigEntry, ConfigValueOption
8from music_assistant_models.enums import ConfigEntryType
9
10if TYPE_CHECKING:
11 from music_assistant_models.config_entries import ConfigValueType
12
13 from music_assistant.mass import MusicAssistant
14
15
16def create_player_selector(
17 mass: MusicAssistant,
18 key: str,
19 selected_value: ConfigValueType = None,
20 auto_value: str | None = None,
21) -> ConfigEntry:
22 """
23 Return a required single-player selector populated from the available players.
24
25 :param mass: The Music Assistant instance providing the current players.
26 :param key: The config entry key for the selected player.
27 :param selected_value: Previously selected player id to prefill when still available.
28 :param auto_value: Optional sentinel that enables automatic player selection.
29 """
30 options = [
31 *([ConfigValueOption(auto_value)] if auto_value is not None else []),
32 *(
33 ConfigValueOption(player.player_id, title=player.display_name)
34 for player in sorted(
35 mass.players.all_players(False, False),
36 key=lambda player: player.display_name.lower(),
37 )
38 ),
39 ]
40 selected = (
41 selected_value
42 if any(option.value == selected_value for option in options)
43 else options[0].value
44 if options
45 else None
46 )
47 return ConfigEntry(
48 key=key,
49 type=ConfigEntryType.STRING,
50 required=True,
51 default_value=selected,
52 value=selected,
53 options=options,
54 )
55