155 files
This commit is contained in:
1 parent
a212615470
commit
0ff8c287a8
155 files changed
+19264
-3148
No files matched your search
@@ -5,14 +5,21 @@ from typing import Any
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import voluptuous as vol
|
||||
from music_assistant_client import MusicAssistantClient
|
||||
from music_assistant_client.exceptions import CannotConnect, InvalidServerVersion
|
||||
from music_assistant_models.errors import AuthenticationFailed, AuthenticationRequired, MusicAssistantError
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult, OptionsFlow
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.selector import (
|
||||
SelectSelector,
|
||||
SelectSelectorConfig,
|
||||
SelectSelectorMode,
|
||||
TextSelector,
|
||||
TextSelectorConfig,
|
||||
TextSelectorType,
|
||||
)
|
||||
|
||||
from .const import (
|
||||
@@ -20,49 +27,38 @@ from .const import (
|
||||
CONF_DEFAULT_PLAYER,
|
||||
CONF_DEFAULT_TAB,
|
||||
CONF_EXCLUDED_PLAYERS,
|
||||
CONF_MA_TOKEN,
|
||||
CONF_MA_URL,
|
||||
DEFAULT_MA_URL,
|
||||
CONFIG_ENTRY_VERSION,
|
||||
DEFAULT_TAB,
|
||||
DOMAIN,
|
||||
MUSIC_ASSISTANT_DOMAIN,
|
||||
NAME,
|
||||
)
|
||||
|
||||
|
||||
def _get_ma_players(hass: HomeAssistant) -> dict[str, str]:
|
||||
"""Return a dict of {entity_id: friendly_name} for Music Assistant media_players."""
|
||||
def _get_mml_players(hass: HomeAssistant) -> dict[str, str]:
|
||||
"""Return {entity_id: friendly_name} for our own media_player entities."""
|
||||
ent_reg = er.async_get(hass)
|
||||
players: dict[str, str] = {}
|
||||
for entity in ent_reg.entities.values():
|
||||
if (
|
||||
entity.platform == MUSIC_ASSISTANT_DOMAIN
|
||||
and entity.domain == "media_player"
|
||||
and not entity.disabled
|
||||
):
|
||||
if entity.platform == DOMAIN and entity.domain == "media_player" and not entity.disabled:
|
||||
state = hass.states.get(entity.entity_id)
|
||||
name = (
|
||||
state.attributes.get("friendly_name", entity.entity_id)
|
||||
if state
|
||||
else entity.entity_id
|
||||
)
|
||||
name = state.attributes.get("friendly_name", entity.entity_id) if state else entity.entity_id
|
||||
players[entity.entity_id] = name
|
||||
return players
|
||||
|
||||
|
||||
def _get_all_players(hass: HomeAssistant) -> dict[str, str]:
|
||||
"""Return a dict of {entity_id: friendly_name} for all non-unavailable media_player entities."""
|
||||
"""Return {entity_id: friendly_name} for all non-unavailable media_player entities."""
|
||||
players: dict[str, str] = {}
|
||||
for state in hass.states.async_all("media_player"):
|
||||
if state.state != "unavailable":
|
||||
name = state.attributes.get("friendly_name", state.entity_id)
|
||||
players[state.entity_id] = name
|
||||
players[state.entity_id] = state.attributes.get("friendly_name", state.entity_id)
|
||||
return dict(sorted(players.items(), key=lambda x: x[1].lower()))
|
||||
|
||||
|
||||
def _validate_url(url: str) -> str | None:
|
||||
"""Return None if valid, or an error key if invalid."""
|
||||
if not url:
|
||||
return None # empty = not configured, that's fine
|
||||
try:
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme not in ("http", "https") or not parsed.netloc:
|
||||
@@ -72,10 +68,29 @@ def _validate_url(url: str) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
async def _test_connection(hass: HomeAssistant, url: str, token: str) -> str | None:
|
||||
"""Try connecting to the MA server. Return None on success, else an error key."""
|
||||
session = async_get_clientsession(hass)
|
||||
mass = MusicAssistantClient(url, session, token=token)
|
||||
try:
|
||||
await mass.connect()
|
||||
except (AuthenticationRequired, AuthenticationFailed):
|
||||
return "invalid_auth"
|
||||
except InvalidServerVersion:
|
||||
return "invalid_server_version"
|
||||
except (CannotConnect, TimeoutError):
|
||||
return "cannot_connect"
|
||||
except MusicAssistantError:
|
||||
return "cannot_connect"
|
||||
else:
|
||||
await mass.disconnect()
|
||||
return None
|
||||
|
||||
|
||||
class MyMusicLibraryConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for My Music Library."""
|
||||
|
||||
VERSION = 1
|
||||
VERSION = CONFIG_ENTRY_VERSION
|
||||
|
||||
@staticmethod
|
||||
@callback
|
||||
@@ -86,29 +101,37 @@ class MyMusicLibraryConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle the initial step."""
|
||||
"""Handle the initial step: Music Assistant server URL + long-lived token."""
|
||||
await self.async_set_unique_id(DOMAIN)
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
errors: dict[str, str] = {}
|
||||
ma_players = _get_ma_players(self.hass)
|
||||
|
||||
if user_input is not None:
|
||||
url_error = _validate_url(user_input.get(CONF_MA_URL, ""))
|
||||
if url_error:
|
||||
url = user_input[CONF_MA_URL].rstrip("/")
|
||||
token = user_input[CONF_MA_TOKEN]
|
||||
if url_error := _validate_url(url):
|
||||
errors[CONF_MA_URL] = url_error
|
||||
else:
|
||||
return self.async_create_entry(title=NAME, data=user_input)
|
||||
|
||||
player_options = {
|
||||
"": "Auto-detect (first available player)",
|
||||
**ma_players,
|
||||
}
|
||||
error = await _test_connection(self.hass, url, token)
|
||||
if error:
|
||||
errors["base"] = error
|
||||
else:
|
||||
return self.async_create_entry(
|
||||
title=NAME,
|
||||
data={
|
||||
CONF_MA_URL: url,
|
||||
CONF_MA_TOKEN: token,
|
||||
CONF_DEFAULT_TAB: user_input[CONF_DEFAULT_TAB],
|
||||
},
|
||||
)
|
||||
|
||||
schema = vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_MA_URL, default=DEFAULT_MA_URL): str,
|
||||
vol.Optional(CONF_DEFAULT_PLAYER, default=""): vol.In(player_options),
|
||||
vol.Required(CONF_MA_URL): TextSelector(TextSelectorConfig(type=TextSelectorType.URL)),
|
||||
vol.Required(CONF_MA_TOKEN): TextSelector(
|
||||
TextSelectorConfig(type=TextSelectorType.PASSWORD)
|
||||
),
|
||||
vol.Optional(CONF_DEFAULT_TAB, default=DEFAULT_TAB): vol.In(
|
||||
{
|
||||
"player": "Player",
|
||||
@@ -119,25 +142,67 @@ class MyMusicLibraryConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
}
|
||||
)
|
||||
|
||||
description_placeholders: dict[str, str] = {
|
||||
"ma_url_hint": "e.g. http://homeassistant.local:8095",
|
||||
}
|
||||
if not ma_players:
|
||||
description_placeholders["ma_warning"] = (
|
||||
"Music Assistant integration not found — "
|
||||
"install it first for full functionality."
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=schema,
|
||||
errors=errors,
|
||||
description_placeholders=description_placeholders,
|
||||
description_placeholders={
|
||||
"ma_url_hint": "e.g. http://homeassistant.local:8095",
|
||||
"token_hint": (
|
||||
"Generate a long-lived token in Music Assistant "
|
||||
"(Settings → your user → API tokens) and paste it here."
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
async def async_step_reauth(
|
||||
self, entry_data: dict[str, Any]
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle reauth when the stored token is missing, revoked, or the URL changed."""
|
||||
return await self.async_step_reauth_confirm()
|
||||
|
||||
async def async_step_reauth_confirm(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Ask for a fresh URL + token and update the existing entry."""
|
||||
errors: dict[str, str] = {}
|
||||
reauth_entry = self._get_reauth_entry()
|
||||
|
||||
if user_input is not None:
|
||||
url = user_input[CONF_MA_URL].rstrip("/")
|
||||
token = user_input[CONF_MA_TOKEN]
|
||||
if url_error := _validate_url(url):
|
||||
errors[CONF_MA_URL] = url_error
|
||||
else:
|
||||
error = await _test_connection(self.hass, url, token)
|
||||
if error:
|
||||
errors["base"] = error
|
||||
else:
|
||||
return self.async_update_reload_and_abort(
|
||||
reauth_entry,
|
||||
data={**reauth_entry.data, CONF_MA_URL: url, CONF_MA_TOKEN: token},
|
||||
)
|
||||
|
||||
schema = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_MA_URL, default=reauth_entry.data.get(CONF_MA_URL, "")): TextSelector(
|
||||
TextSelectorConfig(type=TextSelectorType.URL)
|
||||
),
|
||||
vol.Required(CONF_MA_TOKEN): TextSelector(
|
||||
TextSelectorConfig(type=TextSelectorType.PASSWORD)
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="reauth_confirm",
|
||||
data_schema=schema,
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
|
||||
class MyMusicLibraryOptionsFlow(OptionsFlow):
|
||||
"""Handle options for My Music Library (player exclusion, etc.)."""
|
||||
"""Handle options for My Music Library (default player, excluded players, debug)."""
|
||||
|
||||
async def async_step_init(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
@@ -147,6 +212,8 @@ class MyMusicLibraryOptionsFlow(OptionsFlow):
|
||||
return self.async_create_entry(data=user_input)
|
||||
|
||||
all_players = _get_all_players(self.hass)
|
||||
mml_players = _get_mml_players(self.hass)
|
||||
|
||||
current_excluded: list[str] = list(
|
||||
self.config_entry.options.get(CONF_EXCLUDED_PLAYERS, [])
|
||||
)
|
||||
@@ -156,12 +223,22 @@ class MyMusicLibraryOptionsFlow(OptionsFlow):
|
||||
if "*" in p or p in all_players
|
||||
]
|
||||
|
||||
current_default_player = self.config_entry.options.get(CONF_DEFAULT_PLAYER, "")
|
||||
if current_default_player not in mml_players:
|
||||
current_default_player = ""
|
||||
|
||||
current_debug: bool = bool(
|
||||
self.config_entry.options.get(CONF_DEBUG_MODE, False)
|
||||
)
|
||||
|
||||
player_options = {
|
||||
"": "Auto-detect (first available player)",
|
||||
**mml_players,
|
||||
}
|
||||
|
||||
schema = vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_DEFAULT_PLAYER, default=current_default_player): vol.In(player_options),
|
||||
vol.Optional(CONF_EXCLUDED_PLAYERS, default=current_excluded): SelectSelector(
|
||||
SelectSelectorConfig(
|
||||
options=[{"value": k, "label": v} for k, v in all_players.items()],
|
||||
|
||||
Reference in new issue
Block a user