Add exception translations to Bring integration (#115547)

* Add exception translations

* Add test for exceptions

* Remove unnecessary logging
pull/115549/head^2
Mr. Bubbles 2024-04-14 00:26:37 +02:00 committed by GitHub
parent 4955364948
commit 927ea14562
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 78 additions and 18 deletions

View File

@ -39,23 +39,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await bring.load_lists()
except BringRequestException as e:
raise ConfigEntryNotReady(
f"Timeout while connecting for email '{email}'"
) from e
except BringAuthException as e:
_LOGGER.error(
"Authentication failed for '%s', check your email and password",
email,
)
raise ConfigEntryError(
f"Authentication failed for '{email}', check your email and password"
translation_domain=DOMAIN,
translation_key="setup_request_exception",
) from e
except BringParseException as e:
_LOGGER.error(
"Failed to parse request '%s', check your email and password",
email,
)
raise ConfigEntryNotReady(
"Failed to parse response request from server, try again later"
translation_domain=DOMAIN,
translation_key="setup_request_exception",
) from e
except BringAuthException as e:
raise ConfigEntryError(
translation_domain=DOMAIN,
translation_key="setup_authentication_exception",
translation_placeholders={CONF_EMAIL: email},
) from e
coordinator = BringDataUpdateCoordinator(hass, bring)

View File

@ -16,5 +16,28 @@
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]"
}
},
"exceptions": {
"todo_save_item_failed": {
"message": "Failed to save item {name} to Bring! list"
},
"todo_update_item_failed": {
"message": "Failed to update item {name} to Bring! list"
},
"todo_rename_item_failed": {
"message": "Failed to rename item {name} to Bring! list"
},
"todo_delete_item_failed": {
"message": "Failed to delete {count} item(s) from Bring! list"
},
"setup_request_exception": {
"message": "Failed to connect to server, try again later"
},
"setup_parse_exception": {
"message": "Failed to parse server response, try again later"
},
"setup_authentication_exception": {
"message": "Authentication failed for {email}, check your email and password"
}
}
}

View File

@ -112,7 +112,11 @@ class BringTodoListEntity(
str(uuid.uuid4()),
)
except BringRequestException as e:
raise HomeAssistantError("Unable to save todo item for bring") from e
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="todo_save_item_failed",
translation_placeholders={"name": item.summary or ""},
) from e
await self.coordinator.async_refresh()
@ -167,7 +171,11 @@ class BringTodoListEntity(
else BringItemOperation.COMPLETE,
)
except BringRequestException as e:
raise HomeAssistantError("Unable to update todo item for bring") from e
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="todo_update_item_failed",
translation_placeholders={"name": item.summary or ""},
) from e
else:
try:
await self.coordinator.bring.batch_update_list(
@ -191,7 +199,11 @@ class BringTodoListEntity(
)
except BringRequestException as e:
raise HomeAssistantError("Unable to replace todo item for bring") from e
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="todo_rename_item_failed",
translation_placeholders={"name": item.summary or ""},
) from e
await self.coordinator.async_refresh()
@ -212,6 +224,10 @@ class BringTodoListEntity(
BringItemOperation.REMOVE,
)
except BringRequestException as e:
raise HomeAssistantError("Unable to delete todo item for bring") from e
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="todo_delete_item_failed",
translation_placeholders={"count": str(len(uids))},
) from e
await self.coordinator.async_refresh()

View File

@ -8,10 +8,12 @@ from homeassistant.components.bring import (
BringAuthException,
BringParseException,
BringRequestException,
async_setup_entry,
)
from homeassistant.components.bring.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
from tests.common import MockConfigEntry
@ -62,3 +64,26 @@ async def test_init_failure(
mock_bring_client.login.side_effect = exception
await setup_integration(hass, bring_config_entry)
assert bring_config_entry.state == status
@pytest.mark.parametrize(
("exception", "expected"),
[
(BringRequestException, ConfigEntryNotReady),
(BringAuthException, ConfigEntryError),
(BringParseException, ConfigEntryNotReady),
],
)
async def test_init_exceptions(
hass: HomeAssistant,
mock_bring_client: AsyncMock,
exception: Exception,
expected: Exception,
bring_config_entry: MockConfigEntry | None,
) -> None:
"""Test an initialization error on integration load."""
bring_config_entry.add_to_hass(hass)
mock_bring_client.login.side_effect = exception
with pytest.raises(expected):
await async_setup_entry(hass, bring_config_entry)