diff --git a/homeassistant/components/fireservicerota/__init__.py b/homeassistant/components/fireservicerota/__init__.py index 0d5b4a180079..f76f089087f2 100644 --- a/homeassistant/components/fireservicerota/__init__.py +++ b/homeassistant/components/fireservicerota/__init__.py @@ -4,7 +4,9 @@ from datetime import timedelta from homeassistant.const import Platform from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryAuthFailed +from .const import DOMAIN from .coordinator import ( FireServiceConfigEntry, FireServiceRotaClient, @@ -23,7 +25,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: FireServiceConfigEntry) await client.setup() if client.token_refresh_failure: - return False + raise ConfigEntryAuthFailed( + translation_domain=DOMAIN, + translation_key="token_refresh_failed", + ) entry.async_on_unload(client.async_stop_listener) coordinator = FireServiceUpdateCoordinator(hass, client, entry) diff --git a/homeassistant/components/fireservicerota/strings.json b/homeassistant/components/fireservicerota/strings.json index bb874de8bb28..2bfa67ecfdd0 100644 --- a/homeassistant/components/fireservicerota/strings.json +++ b/homeassistant/components/fireservicerota/strings.json @@ -42,5 +42,10 @@ "name": "Incident response" } } + }, + "exceptions": { + "token_refresh_failed": { + "message": "Failed to refresh the FireServiceRota authentication tokens" + } } } diff --git a/tests/components/fireservicerota/test_init.py b/tests/components/fireservicerota/test_init.py new file mode 100644 index 000000000000..58d7b1de69e6 --- /dev/null +++ b/tests/components/fireservicerota/test_init.py @@ -0,0 +1,38 @@ +"""Tests for the FireServiceRota integration setup.""" + +from unittest.mock import AsyncMock, patch + +from homeassistant.components.fireservicerota.const import DOMAIN +from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState +from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +async def test_setup_token_refresh_failure(hass: HomeAssistant) -> None: + """Test setup fails and starts reauth when refreshing tokens failed.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_USERNAME: "my@email.address", + CONF_PASSWORD: "mypassw0rd", + CONF_URL: "www.brandweerrooster.nl", + }, + unique_id="my@email.address", + ) + entry.add_to_hass(hass) + + with patch( + "homeassistant.components.fireservicerota.FireServiceRotaClient" + ) as mock_client: + mock_client.return_value.setup = AsyncMock() + mock_client.return_value.token_refresh_failure = True + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert entry.state is ConfigEntryState.SETUP_ERROR + assert entry.reason == "Failed to refresh the FireServiceRota authentication tokens" + flows = hass.config_entries.flow.async_progress_by_handler(DOMAIN) + assert len(flows) == 1 + assert flows[0]["context"]["source"] == SOURCE_REAUTH