mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Add translation support to Config Entry errors (#106305)
* Config Entry error translation * split key and placeholders * Fix config entries tests * translation optional * Mods
This commit is contained in:
@@ -243,7 +243,14 @@ class OperationNotAllowed(ConfigError):
|
||||
|
||||
UpdateListenerType = Callable[[HomeAssistant, "ConfigEntry"], Coroutine[Any, Any, None]]
|
||||
|
||||
FROZEN_CONFIG_ENTRY_ATTRS = {"entry_id", "domain", "state", "reason"}
|
||||
FROZEN_CONFIG_ENTRY_ATTRS = {
|
||||
"entry_id",
|
||||
"domain",
|
||||
"state",
|
||||
"reason",
|
||||
"error_reason_translation_key",
|
||||
"error_reason_translation_placeholders",
|
||||
}
|
||||
UPDATE_ENTRY_CONFIG_ENTRY_ATTRS = {
|
||||
"unique_id",
|
||||
"title",
|
||||
@@ -274,6 +281,8 @@ class ConfigEntry:
|
||||
unique_id: str | None
|
||||
state: ConfigEntryState
|
||||
reason: str | None
|
||||
error_reason_translation_key: str | None
|
||||
error_reason_translation_placeholders: dict[str, Any] | None
|
||||
pref_disable_new_entities: bool
|
||||
pref_disable_polling: bool
|
||||
version: int
|
||||
@@ -369,6 +378,8 @@ class ConfigEntry:
|
||||
|
||||
# Reason why config entry is in a failed state
|
||||
_setter(self, "reason", None)
|
||||
_setter(self, "error_reason_translation_key", None)
|
||||
_setter(self, "error_reason_translation_placeholders", None)
|
||||
|
||||
# Function to cancel a scheduled retry
|
||||
self._async_cancel_retry_setup: Callable[[], Any] | None = None
|
||||
@@ -472,6 +483,8 @@ class ConfigEntry:
|
||||
"pref_disable_polling": self.pref_disable_polling,
|
||||
"disabled_by": self.disabled_by,
|
||||
"reason": self.reason,
|
||||
"error_reason_translation_key": self.error_reason_translation_key,
|
||||
"error_reason_translation_placeholders": self.error_reason_translation_placeholders,
|
||||
}
|
||||
return json_fragment(json_bytes(json_repr))
|
||||
|
||||
@@ -543,6 +556,8 @@ class ConfigEntry:
|
||||
setup_phase = SetupPhases.CONFIG_ENTRY_PLATFORM_SETUP
|
||||
|
||||
error_reason = None
|
||||
error_reason_translation_key = None
|
||||
error_reason_translation_placeholders = None
|
||||
|
||||
try:
|
||||
with async_start_setup(
|
||||
@@ -557,6 +572,8 @@ class ConfigEntry:
|
||||
result = False
|
||||
except ConfigEntryError as exc:
|
||||
error_reason = str(exc) or "Unknown fatal config entry error"
|
||||
error_reason_translation_key = exc.translation_key
|
||||
error_reason_translation_placeholders = exc.translation_placeholders
|
||||
_LOGGER.exception(
|
||||
"Error setting up entry %s for %s: %s",
|
||||
self.title,
|
||||
@@ -569,6 +586,8 @@ class ConfigEntry:
|
||||
message = str(exc)
|
||||
auth_base_message = "could not authenticate"
|
||||
error_reason = message or auth_base_message
|
||||
error_reason_translation_key = exc.translation_key
|
||||
error_reason_translation_placeholders = exc.translation_placeholders
|
||||
auth_message = (
|
||||
f"{auth_base_message}: {message}" if message else auth_base_message
|
||||
)
|
||||
@@ -583,7 +602,15 @@ class ConfigEntry:
|
||||
result = False
|
||||
except ConfigEntryNotReady as exc:
|
||||
message = str(exc)
|
||||
self._async_set_state(hass, ConfigEntryState.SETUP_RETRY, message or None)
|
||||
error_reason_translation_key = exc.translation_key
|
||||
error_reason_translation_placeholders = exc.translation_placeholders
|
||||
self._async_set_state(
|
||||
hass,
|
||||
ConfigEntryState.SETUP_RETRY,
|
||||
message or None,
|
||||
error_reason_translation_key,
|
||||
error_reason_translation_placeholders,
|
||||
)
|
||||
wait_time = 2 ** min(self._tries, 4) * 5 + (
|
||||
randint(RANDOM_MICROSECOND_MIN, RANDOM_MICROSECOND_MAX) / 1000000
|
||||
)
|
||||
@@ -644,7 +671,13 @@ class ConfigEntry:
|
||||
if result:
|
||||
self._async_set_state(hass, ConfigEntryState.LOADED, None)
|
||||
else:
|
||||
self._async_set_state(hass, ConfigEntryState.SETUP_ERROR, error_reason)
|
||||
self._async_set_state(
|
||||
hass,
|
||||
ConfigEntryState.SETUP_ERROR,
|
||||
error_reason,
|
||||
error_reason_translation_key,
|
||||
error_reason_translation_placeholders,
|
||||
)
|
||||
|
||||
@callback
|
||||
def _async_setup_again(self, hass: HomeAssistant, *_: Any) -> None:
|
||||
@@ -771,7 +804,12 @@ class ConfigEntry:
|
||||
|
||||
@callback
|
||||
def _async_set_state(
|
||||
self, hass: HomeAssistant, state: ConfigEntryState, reason: str | None
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
state: ConfigEntryState,
|
||||
reason: str | None,
|
||||
error_reason_translation_key: str | None = None,
|
||||
error_reason_translation_placeholders: dict[str, str] | None = None,
|
||||
) -> None:
|
||||
"""Set the state of the config entry."""
|
||||
if state not in NO_RESET_TRIES_STATES:
|
||||
@@ -779,6 +817,12 @@ class ConfigEntry:
|
||||
_setter = object.__setattr__
|
||||
_setter(self, "state", state)
|
||||
_setter(self, "reason", reason)
|
||||
_setter(self, "error_reason_translation_key", error_reason_translation_key)
|
||||
_setter(
|
||||
self,
|
||||
"error_reason_translation_placeholders",
|
||||
error_reason_translation_placeholders,
|
||||
)
|
||||
self.clear_cache()
|
||||
async_dispatcher_send(
|
||||
hass, SIGNAL_CONFIG_ENTRY_CHANGED, ConfigEntryChange.UPDATED, self
|
||||
|
||||
Reference in New Issue
Block a user