From 65e6e1fa28b46fce5842b575ef7b1c9ef9d794bb Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Wed, 15 May 2024 10:00:56 +0100 Subject: [PATCH] Add exception translations to System Bridge integration (#112206) * Add exception translations to System Bridge integration * Add translated error to coordinator * Refactor strings.json in system_bridge component * Sort * Add HomeAssistantError import --- .../components/system_bridge/__init__.py | 73 ++++++++++++++++--- .../components/system_bridge/coordinator.py | 11 ++- .../components/system_bridge/strings.json | 18 +++++ 3 files changed, 91 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/system_bridge/__init__.py b/homeassistant/components/system_bridge/__init__.py index 03ef06dc914..a991d151959 100644 --- a/homeassistant/components/system_bridge/__init__.py +++ b/homeassistant/components/system_bridge/__init__.py @@ -43,6 +43,7 @@ from homeassistant.core import ( from homeassistant.exceptions import ( ConfigEntryAuthFailed, ConfigEntryNotReady, + HomeAssistantError, ServiceValidationError, ) from homeassistant.helpers import ( @@ -108,14 +109,31 @@ async def async_setup_entry( supported = await version.check_supported() except AuthenticationException as exception: _LOGGER.error("Authentication failed for %s: %s", entry.title, exception) - raise ConfigEntryAuthFailed from exception + raise ConfigEntryAuthFailed( + translation_domain=DOMAIN, + translation_key="authentication_failed", + translation_placeholders={ + "title": entry.title, + "host": entry.data[CONF_HOST], + }, + ) from exception except (ConnectionClosedException, ConnectionErrorException) as exception: raise ConfigEntryNotReady( - f"Could not connect to {entry.title} ({entry.data[CONF_HOST]})." + translation_domain=DOMAIN, + translation_key="connection_failed", + translation_placeholders={ + "title": entry.title, + "host": entry.data[CONF_HOST], + }, ) from exception except TimeoutError as exception: raise ConfigEntryNotReady( - f"Timed out waiting for {entry.title} ({entry.data[CONF_HOST]})." + translation_domain=DOMAIN, + translation_key="timeout", + translation_placeholders={ + "title": entry.title, + "host": entry.data[CONF_HOST], + }, ) from exception # If not supported, create an issue and raise ConfigEntryNotReady @@ -130,7 +148,12 @@ async def async_setup_entry( is_fixable=False, ) raise ConfigEntryNotReady( - "You are not running a supported version of System Bridge. Please update to the latest version." + translation_domain=DOMAIN, + translation_key="unsupported_version", + translation_placeholders={ + "title": entry.title, + "host": entry.data[CONF_HOST], + }, ) coordinator = SystemBridgeDataUpdateCoordinator( @@ -143,14 +166,31 @@ async def async_setup_entry( await coordinator.async_get_data(MODULES) except AuthenticationException as exception: _LOGGER.error("Authentication failed for %s: %s", entry.title, exception) - raise ConfigEntryAuthFailed from exception + raise ConfigEntryAuthFailed( + translation_domain=DOMAIN, + translation_key="authentication_failed", + translation_placeholders={ + "title": entry.title, + "host": entry.data[CONF_HOST], + }, + ) from exception except (ConnectionClosedException, ConnectionErrorException) as exception: raise ConfigEntryNotReady( - f"Could not connect to {entry.title} ({entry.data[CONF_HOST]})." + translation_domain=DOMAIN, + translation_key="connection_failed", + translation_placeholders={ + "title": entry.title, + "host": entry.data[CONF_HOST], + }, ) from exception except TimeoutError as exception: raise ConfigEntryNotReady( - f"Timed out waiting for {entry.title} ({entry.data[CONF_HOST]})." + translation_domain=DOMAIN, + translation_key="timeout", + translation_placeholders={ + "title": entry.title, + "host": entry.data[CONF_HOST], + }, ) from exception # Fetch initial data so we have data when entities subscribe @@ -168,7 +208,12 @@ async def async_setup_entry( await asyncio.sleep(1) except TimeoutError as exception: raise ConfigEntryNotReady( - f"Timed out waiting for {entry.title} ({entry.data[CONF_HOST]})." + translation_domain=DOMAIN, + translation_key="timeout", + translation_placeholders={ + "title": entry.title, + "host": entry.data[CONF_HOST], + }, ) from exception hass.data.setdefault(DOMAIN, {}) @@ -208,8 +253,16 @@ async def async_setup_entry( if entry.entry_id in device_entry.config_entries ) except StopIteration as exception: - raise vol.Invalid(f"Could not find device {device}") from exception - raise vol.Invalid(f"Device {device} does not exist") + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="device_not_found", + translation_placeholders={"device": device}, + ) from exception + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="device_not_found", + translation_placeholders={"device": device}, + ) async def handle_get_process_by_id(service_call: ServiceCall) -> ServiceResponse: """Handle the get process by id service call.""" diff --git a/homeassistant/components/system_bridge/coordinator.py b/homeassistant/components/system_bridge/coordinator.py index f810c69a873..836e7361923 100644 --- a/homeassistant/components/system_bridge/coordinator.py +++ b/homeassistant/components/system_bridge/coordinator.py @@ -59,6 +59,8 @@ class SystemBridgeDataUpdateCoordinator(DataUpdateCoordinator[SystemBridgeData]) session=async_get_clientsession(hass), ) + self._host = entry.data[CONF_HOST] + super().__init__( hass, LOGGER, @@ -191,7 +193,14 @@ class SystemBridgeDataUpdateCoordinator(DataUpdateCoordinator[SystemBridgeData]) self.unsub = None self.last_update_success = False self.async_update_listeners() - raise ConfigEntryAuthFailed from exception + raise ConfigEntryAuthFailed( + translation_domain=DOMAIN, + translation_key="authentication_failed", + translation_placeholders={ + "title": self.title, + "host": self._host, + }, + ) from exception except ConnectionErrorException as exception: self.logger.warning( "Connection error occurred for %s. Will retry: %s", diff --git a/homeassistant/components/system_bridge/strings.json b/homeassistant/components/system_bridge/strings.json index 98a1fe4c08d..b5ceba9bd84 100644 --- a/homeassistant/components/system_bridge/strings.json +++ b/homeassistant/components/system_bridge/strings.json @@ -95,8 +95,26 @@ } }, "exceptions": { + "authentication_failed": { + "message": "Authentication failed for {title} ({host})" + }, + "connection_failed": { + "message": "A connection error occurred for {title} ({host})" + }, + "device_not_found": { + "message": "Could not find device {device}" + }, + "no_data_received": { + "message": "No data received from {host}" + }, "process_not_found": { "message": "Could not find process with id {id}." + }, + "timeout": { + "message": "A timeout occurred for {title} ({host})" + }, + "unsupported_version": { + "message": "You are not running a supported version of System Bridge for {title} ({host}). Please upgrade to the latest version" } }, "issues": {