99from homeassistant .components import websocket_api
1010from homeassistant .config_entries import ConfigEntry , ConfigEntryState
1111from homeassistant .const import CONF_URL , EVENT_HOMEASSISTANT_STOP
12- from homeassistant .exceptions import ConfigEntryNotReady
12+ from homeassistant .exceptions import ConfigEntryAuthFailed , ConfigEntryNotReady
1313from homeassistant .helpers import config_validation as cv
1414from homeassistant .helpers import device_registry as dr
1515from homeassistant .helpers .aiohttp_client import async_get_clientsession
1919 async_delete_issue ,
2020)
2121from music_assistant_client import MusicAssistantClient
22- from music_assistant_client .exceptions import CannotConnect , InvalidServerVersion
23- from music_assistant_models .errors import ActionUnavailable , MusicAssistantError
22+ from music_assistant_client .exceptions import (
23+ CannotConnect ,
24+ InvalidServerVersion ,
25+ MusicAssistantClientException ,
26+ )
27+ from music_assistant_models .errors import (
28+ ActionUnavailable ,
29+ AuthenticationFailed ,
30+ AuthenticationRequired ,
31+ InvalidToken ,
32+ MusicAssistantError ,
33+ )
2434
2535from .actions import (
2636 MassQueueActions ,
2737 get_music_assistant_client ,
2838 setup_controller_and_actions ,
2939)
30- from .const import DOMAIN , LOGGER
40+ from .const import CONF_TOKEN , DOMAIN , LOGGER
3141from .services import register_actions
3242from .websocket_commands import (
3343 api_download_and_encode_image ,
@@ -63,14 +73,16 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa:
6373 return True
6474
6575
66- async def async_setup_entry (
76+ async def async_setup_entry ( # noqa: PLR0915
6777 hass : HomeAssistant ,
6878 entry : MusicAssistantConfigEntry ,
6979) -> bool :
7080 """Set up Music Assistant from a config entry."""
7181 http_session = async_get_clientsession (hass , verify_ssl = False )
7282 mass_url = entry .data [CONF_URL ]
73- mass = MusicAssistantClient (mass_url , http_session )
83+ # Get token from config entry (for schema >= AUTH_SCHEMA_VERSION)
84+ token = entry .data .get (CONF_TOKEN )
85+ mass = MusicAssistantClient (mass_url , http_session , token = token )
7486
7587 try :
7688 async with asyncio .timeout (CONNECT_TIMEOUT ):
@@ -91,6 +103,12 @@ async def async_setup_entry(
91103 )
92104 exc = f"Invalid server version: { err } "
93105 raise ConfigEntryNotReady (exc ) from err
106+ except (AuthenticationRequired , AuthenticationFailed , InvalidToken ) as err :
107+ exc = f"Authentication failed for { mass_url } : { err } "
108+ raise ConfigEntryAuthFailed (exc ) from err
109+ except MusicAssistantClientException as err :
110+ exc = f"Failed to connect to music assistant server { mass_url } : { err } "
111+ raise ConfigEntryNotReady (exc ) from err
94112 except MusicAssistantError as err :
95113 LOGGER .exception ("Failed to connect to music assistant server" , exc_info = err )
96114 exc = f"Unknown error connecting to the Music Assistant server { mass_url } "
0 commit comments