From 6f9c8b2aa04fc2218b629af19b396cc79dbc0b61 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 23 Apr 2025 08:40:31 +0200 Subject: [PATCH] Add exception translations to Renault (#143452) --- .../components/renault/quality_scale.yaml | 2 +- .../components/renault/renault_vehicle.py | 6 ++- homeassistant/components/renault/strings.json | 3 ++ tests/components/renault/test_services.py | 40 +++++++++++++++---- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/renault/quality_scale.yaml b/homeassistant/components/renault/quality_scale.yaml index f2d70622192..a4e3252dcd6 100644 --- a/homeassistant/components/renault/quality_scale.yaml +++ b/homeassistant/components/renault/quality_scale.yaml @@ -52,7 +52,7 @@ rules: entity-device-class: done entity-disabled-by-default: done entity-translations: done - exception-translations: todo + exception-translations: done icon-translations: done reconfiguration-flow: todo repair-issues: done diff --git a/homeassistant/components/renault/renault_vehicle.py b/homeassistant/components/renault/renault_vehicle.py index 8d096a734e1..2ecaa7e1061 100644 --- a/homeassistant/components/renault/renault_vehicle.py +++ b/homeassistant/components/renault/renault_vehicle.py @@ -43,7 +43,11 @@ def with_error_wrapping[**_P, _R]( try: return await func(self, *args, **kwargs) except RenaultException as err: - raise HomeAssistantError(err) from err + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="unknown_error", + translation_placeholders={"error": str(err)}, + ) from err return wrapper diff --git a/homeassistant/components/renault/strings.json b/homeassistant/components/renault/strings.json index 727e8cf32f1..1e6af2b10fe 100644 --- a/homeassistant/components/renault/strings.json +++ b/homeassistant/components/renault/strings.json @@ -232,6 +232,9 @@ }, "no_config_entry_for_device": { "message": "No loaded config entry was found for device with ID {device_id}" + }, + "unknown_error": { + "message": "An unknown error occurred while communicating with the Renault servers: {error}" } } } diff --git a/tests/components/renault/test_services.py b/tests/components/renault/test_services.py index 970d7cf4ad8..1aa31768004 100644 --- a/tests/components/renault/test_services.py +++ b/tests/components/renault/test_services.py @@ -72,13 +72,14 @@ async def test_service_set_ac_cancel( ATTR_VEHICLE: get_device_id(hass), } - with ( - patch( - "renault_api.renault_vehicle.RenaultVehicle.set_ac_stop", - side_effect=RenaultException("Didn't work"), - ) as mock_action, - pytest.raises(HomeAssistantError, match="Didn't work"), - ): + with patch( + "renault_api.renault_vehicle.RenaultVehicle.set_ac_stop", + return_value=( + schemas.KamereonVehicleHvacStartActionDataSchema.loads( + load_fixture("renault/action.set_ac_stop.json") + ) + ), + ) as mock_action: await hass.services.async_call( DOMAIN, SERVICE_AC_CANCEL, service_data=data, blocking=True ) @@ -380,3 +381,28 @@ async def test_service_invalid_device_id2( ) assert err.value.translation_key == "no_config_entry_for_device" assert err.value.translation_placeholders == {"device_id": "REG-NUMBER"} + + +async def test_service_exception( + hass: HomeAssistant, config_entry: ConfigEntry +) -> None: + """Test that service invokes renault_api with correct data.""" + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + data = { + ATTR_VEHICLE: get_device_id(hass), + } + + with ( + patch( + "renault_api.renault_vehicle.RenaultVehicle.set_ac_stop", + side_effect=RenaultException("Didn't work"), + ) as mock_action, + pytest.raises(HomeAssistantError, match="Didn't work"), + ): + await hass.services.async_call( + DOMAIN, SERVICE_AC_CANCEL, service_data=data, blocking=True + ) + assert len(mock_action.mock_calls) == 1 + assert mock_action.mock_calls[0][1] == ()