From 188f2a633885e375c68df3c32db4b76ceb0ce9e1 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 29 Aug 2026 01:25:26 +0200 Subject: [PATCH] Translate the Peblar setup exceptions (#180561) --- homeassistant/components/peblar/__init__.py | 21 ++++++++++------ tests/components/peblar/test_init.py | 28 ++++++++++++++++++--- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/peblar/__init__.py b/homeassistant/components/peblar/__init__.py index 105e2b90cc58..db4ebe80824b 100644 --- a/homeassistant/components/peblar/__init__.py +++ b/homeassistant/components/peblar/__init__.py @@ -48,14 +48,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: PeblarConfigEntry) -> bo system_information = await peblar.system_information() api = await peblar.rest_api(enable=True, access_mode=AccessMode.READ_WRITE) except PeblarConnectionError as err: - # pylint: disable-next=home-assistant-exception-not-translated - raise ConfigEntryNotReady("Could not connect to Peblar charger") from err - except PeblarAuthenticationError as err: - raise ConfigEntryAuthFailed from err - except PeblarError as err: - # pylint: disable-next=home-assistant-exception-not-translated raise ConfigEntryNotReady( - "Unknown error occurred while connecting to Peblar charger" + translation_domain=DOMAIN, + translation_key="communication_error", + translation_placeholders={"error": str(err)}, + ) from err + except PeblarAuthenticationError as err: + raise ConfigEntryAuthFailed( + translation_domain=DOMAIN, + translation_key="authentication_error", + ) from err + except PeblarError as err: + raise ConfigEntryNotReady( + translation_domain=DOMAIN, + translation_key="unknown_error", + translation_placeholders={"error": str(err)}, ) from err # Setup the data coordinators diff --git a/tests/components/peblar/test_init.py b/tests/components/peblar/test_init.py index acbfefa4f35f..980c9ad1ba70 100644 --- a/tests/components/peblar/test_init.py +++ b/tests/components/peblar/test_init.py @@ -35,16 +35,35 @@ async def test_load_unload_config_entry( @pytest.mark.parametrize( - "exception", - [PeblarConnectionError, PeblarError], + ("exception", "translation_key", "reason"), + [ + ( + PeblarConnectionError("Could not connect"), + "communication_error", + "An error occurred while communicating with the Peblar EV charger: " + "Could not connect", + ), + ( + PeblarError("Unknown error"), + "unknown_error", + "An unknown error occurred while communicating with the Peblar EV " + "charger: Unknown error", + ), + ], ) async def test_config_entry_not_ready( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_peblar: MagicMock, exception: Exception, + translation_key: str, + reason: str, ) -> None: - """Test the Peblar configuration entry not ready.""" + """Test the Peblar configuration entry not ready. + + The reason reaches the user, so it comes from strings.json rather than + from a sentence typed into the raise. + """ mock_peblar.login.side_effect = exception mock_config_entry.add_to_hass(hass) @@ -53,6 +72,8 @@ async def test_config_entry_not_ready( assert len(mock_peblar.login.mock_calls) == 1 assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY + assert mock_config_entry.error_reason_translation_key == translation_key + assert mock_config_entry.reason == reason async def test_config_entry_authentication_failed( @@ -69,6 +90,7 @@ async def test_config_entry_authentication_failed( await hass.async_block_till_done() assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR + assert mock_config_entry.error_reason_translation_key == "authentication_error" flows = hass.config_entries.flow.async_progress() assert len(flows) == 1