Raise ConfigEntryAuthFailed on token refresh failure in fireservicerota (#185006)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
G Johansson
2026-10-08 21:48:39 +02:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 7d2f80be21
commit 03dd10d5a8
3 changed files with 49 additions and 1 deletions
@@ -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)
@@ -42,5 +42,10 @@
"name": "Incident response"
}
}
},
"exceptions": {
"token_refresh_failed": {
"message": "Failed to refresh the FireServiceRota authentication tokens"
}
}
}
@@ -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