Enable raising ConfigEntryAuthFailed on BMW coordinator init (#116643)

Co-authored-by: Richard <rikroe@users.noreply.github.com>
This commit is contained in:
Richard Kroegel 2024-05-14 22:14:35 +03:00 committed by GitHub
parent fa815234be
commit 13e2bc7b6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 1 deletions

View file

@ -50,6 +50,9 @@ class BMWDataUpdateCoordinator(DataUpdateCoordinator[None]):
update_interval=timedelta(seconds=SCAN_INTERVALS[entry.data[CONF_REGION]]), update_interval=timedelta(seconds=SCAN_INTERVALS[entry.data[CONF_REGION]]),
) )
# Default to false on init so _async_update_data logic works
self.last_update_success = False
async def _async_update_data(self) -> None: async def _async_update_data(self) -> None:
"""Fetch data from BMW.""" """Fetch data from BMW."""
old_refresh_token = self.account.refresh_token old_refresh_token = self.account.refresh_token

View file

@ -7,8 +7,10 @@ from bimmer_connected.models import MyBMWAPIError, MyBMWAuthError
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
import respx import respx
from homeassistant.core import HomeAssistant from homeassistant.components.bmw_connected_drive import DOMAIN as BMW_DOMAIN
from homeassistant.core import DOMAIN as HA_DOMAIN, HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.issue_registry import IssueRegistry
from homeassistant.helpers.update_coordinator import UpdateFailed from homeassistant.helpers.update_coordinator import UpdateFailed
from . import FIXTURE_CONFIG_ENTRY from . import FIXTURE_CONFIG_ENTRY
@ -92,3 +94,29 @@ async def test_update_reauth(
assert coordinator.last_update_success is False assert coordinator.last_update_success is False
assert isinstance(coordinator.last_exception, ConfigEntryAuthFailed) is True assert isinstance(coordinator.last_exception, ConfigEntryAuthFailed) is True
async def test_init_reauth(
hass: HomeAssistant,
bmw_fixture: respx.Router,
freezer: FrozenDateTimeFactory,
issue_registry: IssueRegistry,
) -> None:
"""Test the reauth form."""
config_entry = MockConfigEntry(**FIXTURE_CONFIG_ENTRY)
config_entry.add_to_hass(hass)
assert len(issue_registry.issues) == 0
with patch(
"bimmer_connected.account.MyBMWAccount.get_vehicles",
side_effect=MyBMWAuthError("Test error"),
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
reauth_issue = issue_registry.async_get_issue(
HA_DOMAIN, f"config_entry_reauth_{BMW_DOMAIN}_{config_entry.entry_id}"
)
assert reauth_issue.active is True