mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Raise HomeAssistantError if update fails (#129727)
This commit is contained in:
parent
f1655c5d1a
commit
dfa7ababfb
@ -3,13 +3,14 @@
|
||||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from pprint import pformat
|
||||
from typing import Any
|
||||
|
||||
from monzopy import AuthorisationExpiredError
|
||||
from monzopy import AuthorisationExpiredError, InvalidMonzoAPIResponseError
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .api import AuthenticatedMonzoAPI
|
||||
from .const import DOMAIN
|
||||
@ -45,5 +46,16 @@ class MonzoCoordinator(DataUpdateCoordinator[MonzoData]):
|
||||
pots = await self.api.user_account.pots()
|
||||
except AuthorisationExpiredError as err:
|
||||
raise ConfigEntryAuthFailed from err
|
||||
except InvalidMonzoAPIResponseError as err:
|
||||
message = "Invalid Monzo API response."
|
||||
if err.missing_key:
|
||||
_LOGGER.debug(
|
||||
"%s\nMissing key: %s\nResponse:\n%s",
|
||||
message,
|
||||
err.missing_key,
|
||||
pformat(err.response),
|
||||
)
|
||||
message += " Enabling debug logging for details."
|
||||
raise UpdateFailed(message) from err
|
||||
|
||||
return MonzoData(accounts, pots)
|
||||
|
@ -5,6 +5,7 @@ from typing import Any
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from monzopy import InvalidMonzoAPIResponseError
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
@ -123,15 +124,22 @@ async def test_update_failed(
|
||||
monzo: AsyncMock,
|
||||
polling_config_entry: MockConfigEntry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test all entities."""
|
||||
await setup_integration(hass, polling_config_entry)
|
||||
|
||||
monzo.user_account.accounts.side_effect = Exception
|
||||
monzo.user_account.accounts.side_effect = InvalidMonzoAPIResponseError(
|
||||
{"acc_id": None}, "account_id"
|
||||
)
|
||||
freezer.tick(timedelta(minutes=10))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "Invalid Monzo API response." in caplog.text
|
||||
assert "account_id" in caplog.text
|
||||
assert "acc_id" in caplog.text
|
||||
|
||||
entity_id = await async_get_entity_id(
|
||||
hass, TEST_ACCOUNTS[0]["id"], ACCOUNT_SENSORS[0]
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user