Skip to content
Open
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
8 changes: 6 additions & 2 deletions homeassistant/components/monzo/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,19 @@ async def _async_update_data(self) -> MonzoData:
raise ConfigEntryAuthFailed from err
except InvalidMonzoAPIResponseError as err:
message = "Invalid Monzo API response."
translation_key = "invalid_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
translation_key = "invalid_api_response_with_details"
raise UpdateFailed(
translation_domain=DOMAIN,
translation_key=translation_key,
) from err

data = MonzoData(
accounts={account["id"]: account for account in accounts},
Expand Down
7 changes: 7 additions & 0 deletions homeassistant/components/monzo/icons.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"entity": {
"event": {
"transaction": {
"default": "mdi:bank-transfer"
}
}
},
"services": {
"deposit_into_pot": {
"service": "mdi:bank-transfer-in"
Expand Down
6 changes: 6 additions & 0 deletions homeassistant/components/monzo/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
"invalid_account": {
"message": "The selected device ({device_name}) is not an account belonging to this Monzo connection."
},
"invalid_api_response": {
"message": "The Monzo API returned an invalid response."
},
"invalid_api_response_with_details": {
"message": "The Monzo API returned an invalid response. Enable debug logging for details."
},
"invalid_device": {
"message": "The selected Monzo device could not be found."
},
Expand Down
25 changes: 25 additions & 0 deletions tests/components/monzo/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Tests for Monzo helpers."""

from homeassistant.components.monzo.helpers import (
get_account_name,
get_authenticated_owner_name,
)


def test_authenticated_owner_name_without_user_id() -> None:
"""Test an owner cannot be selected without an authenticated user ID."""
assert get_authenticated_owner_name([], None) is None


def test_account_name_ignores_malformed_owner() -> None:
"""Test malformed owner metadata does not affect a joint account name."""
account = {
"name": "Joint Account",
"owners": [
"invalid owner",
{"preferred_name": "Jake Martin"},
{"preferred_name": "Jane Martin"},
],
}

assert get_account_name(account) == "Joint Account — Jake Martin & Jane Martin"
33 changes: 25 additions & 8 deletions tests/components/monzo/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,27 +285,44 @@ async def test_all_entities(
)


@pytest.mark.parametrize(
("api_error", "expected_log_messages"),
[
pytest.param(
InvalidMonzoAPIResponseError(),
("The Monzo API returned an invalid response",),
id="invalid-response",
),
pytest.param(
InvalidMonzoAPIResponseError({"acc_id": None}, "account_id"),
(
"The Monzo API returned an invalid response. Enable debug logging for details",
"account_id",
"acc_id",
),
id="missing-key",
),
],
)
async def test_update_failed(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
monzo: AsyncMock,
polling_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
caplog: pytest.LogCaptureFixture,
api_error: InvalidMonzoAPIResponseError,
expected_log_messages: tuple[str, ...],
) -> None:
"""Test all entities."""
"""Test an invalid API response makes entities unavailable."""
await setup_integration(hass, polling_config_entry)

monzo.user_account.accounts.side_effect = InvalidMonzoAPIResponseError(
{"acc_id": None}, "account_id"
)
monzo.user_account.accounts.side_effect = api_error
freezer.tick(timedelta(minutes=10))
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)

assert "Invalid Monzo API response." in caplog.text
assert "account_id" in caplog.text
assert "acc_id" in caplog.text
for message in expected_log_messages:
assert message in caplog.text

entity_id = await async_get_entity_id(
hass, TEST_ACCOUNTS[0]["id"], ACCOUNT_SENSORS[0]
Expand Down
Loading