Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise HomeAssistantError if update fails #129727

Merged
merged 3 commits into from
Nov 26, 2024
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
16 changes: 14 additions & 2 deletions homeassistant/components/monzo/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,5 +46,16 @@ async def _async_update_data(self) -> 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)
10 changes: 9 additions & 1 deletion tests/components/monzo/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from unittest.mock import AsyncMock

from freezegun.api import FrozenDateTimeFactory
from monzopy import InvalidMonzoAPIResponseError
import pytest
from syrupy import SnapshotAssertion

Expand Down Expand Up @@ -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]
)
Expand Down
Loading