Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion homeassistant/components/music_assistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.const import CONF_URL, EVENT_HOMEASSISTANT_STOP, Platform
from homeassistant.core import Event, HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.exceptions import (
ConfigEntryAuthFailed,
ConfigEntryError,
ConfigEntryNotReady,
)
from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.issue_registry import (
Expand Down Expand Up @@ -101,6 +105,15 @@ async def async_setup_entry( # noqa: C901
)
raise ConfigEntryNotReady(f"Invalid server version: {err}") from err
except (AuthenticationRequired, AuthenticationFailed, InvalidToken) as err:
assert mass.server_info is not None
# Users cannot reauthenticate when running as Home Assistant addon,
# so raising ConfigEntryAuthFailed in that case would be incorrect.
# Instead we should wait until the addon discovery is completed,
# as that will set up authentication and reload the entry automatically.
if mass.server_info.homeassistant_addon:
Comment thread
arturpragacz marked this conversation as resolved.
raise ConfigEntryError(
"Authentication failed, addon discovery not completed yet"
) from err
raise ConfigEntryAuthFailed(
f"Authentication failed for {mass_url}: {err}"
) from err
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/music_assistant/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ async def async_step_hassio(
ConfigEntryState.LOADED,
ConfigEntryState.SETUP_ERROR,
ConfigEntryState.SETUP_RETRY,
ConfigEntryState.SETUP_IN_PROGRESS,
):
self.hass.config_entries.async_schedule_reload(entry.entry_id)

Expand Down
34 changes: 29 additions & 5 deletions tests/components/music_assistant/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ async def test_authentication_required_triggers_reauth(
music_assistant_client: MagicMock,
) -> None:
"""Test that AuthenticationRequired exception triggers reauth flow."""
# Create a config entry
config_entry = MockConfigEntry(
domain=DOMAIN,
title="Music Assistant",
Expand All @@ -173,19 +172,44 @@ async def test_authentication_required_triggers_reauth(
)
config_entry.add_to_hass(hass)

# Mock the client to raise AuthenticationRequired during connect
music_assistant_client.connect.side_effect = AuthenticationRequired(
"Authentication required"
)

# Try to set up the integration
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

# Verify the entry is in SETUP_ERROR state (auth failed)
assert config_entry.state is ConfigEntryState.SETUP_ERROR

# Verify a reauth repair issue was created
issue_reg = ir.async_get(hass)
issue_id = f"config_entry_reauth_{DOMAIN}_{config_entry.entry_id}"
assert issue_reg.async_get_issue("homeassistant", issue_id)


async def test_authentication_required_addon_no_reauth(
hass: HomeAssistant,
music_assistant_client: MagicMock,
) -> None:
"""Test that AuthenticationRequired exception does not trigger reauth for addon."""
config_entry = MockConfigEntry(
domain=DOMAIN,
title="Music Assistant",
data={"url": "http://localhost:8095", "token": "old_token"},
unique_id="test_server_id",
)
config_entry.add_to_hass(hass)

music_assistant_client.server_info.homeassistant_addon = True

music_assistant_client.connect.side_effect = AuthenticationRequired(
"Authentication required"
)

await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

assert config_entry.state is ConfigEntryState.SETUP_ERROR

issue_reg = ir.async_get(hass)
issue_id = f"config_entry_reauth_{DOMAIN}_{config_entry.entry_id}"
assert issue_reg.async_get_issue("homeassistant", issue_id) is None
Loading