From 65514fbd733bdfb7f1d0b0ee43581cddea680e1e Mon Sep 17 00:00:00 2001 From: G Johansson Date: Wed, 13 Dec 2023 19:40:57 +0100 Subject: [PATCH] Add error translations for Sensibo (#105600) --- homeassistant/components/sensibo/climate.py | 34 +++++++++++++++---- homeassistant/components/sensibo/entity.py | 16 +++++++-- homeassistant/components/sensibo/select.py | 9 ++++- homeassistant/components/sensibo/strings.json | 32 +++++++++++++++++ homeassistant/components/sensibo/switch.py | 4 ++- tests/components/sensibo/test_climate.py | 4 +-- 6 files changed, 86 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/sensibo/climate.py b/homeassistant/components/sensibo/climate.py index 40aa54e5d56..89e1fafa213 100644 --- a/homeassistant/components/sensibo/climate.py +++ b/homeassistant/components/sensibo/climate.py @@ -22,7 +22,7 @@ from homeassistant.const import ( UnitOfTemperature, ) from homeassistant.core import HomeAssistant -from homeassistant.exceptions import HomeAssistantError +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util.unit_conversion import TemperatureConverter @@ -314,11 +314,17 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): """Set new target temperature.""" if "targetTemperature" not in self.device_data.active_features: raise HomeAssistantError( - "Current mode doesn't support setting Target Temperature" + "Current mode doesn't support setting Target Temperature", + translation_domain=DOMAIN, + translation_key="no_target_temperature_in_features", ) if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None: - raise ValueError("No target temperature provided") + raise ServiceValidationError( + "No target temperature provided", + translation_domain=DOMAIN, + translation_key="no_target_temperature", + ) if temperature == self.target_temperature: return @@ -334,10 +340,17 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): async def async_set_fan_mode(self, fan_mode: str) -> None: """Set new target fan mode.""" if "fanLevel" not in self.device_data.active_features: - raise HomeAssistantError("Current mode doesn't support setting Fanlevel") + raise HomeAssistantError( + "Current mode doesn't support setting Fanlevel", + translation_domain=DOMAIN, + translation_key="no_fan_level_in_features", + ) if fan_mode not in AVAILABLE_FAN_MODES: raise HomeAssistantError( - f"Climate fan mode {fan_mode} is not supported by the integration, please open an issue" + f"Climate fan mode {fan_mode} is not supported by the integration, please open an issue", + translation_domain=DOMAIN, + translation_key="fan_mode_not_supported", + translation_placeholders={"fan_mode": fan_mode}, ) transformation = self.device_data.fan_modes_translated @@ -379,10 +392,17 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): async def async_set_swing_mode(self, swing_mode: str) -> None: """Set new target swing operation.""" if "swing" not in self.device_data.active_features: - raise HomeAssistantError("Current mode doesn't support setting Swing") + raise HomeAssistantError( + "Current mode doesn't support setting Swing", + translation_domain=DOMAIN, + translation_key="no_swing_in_features", + ) if swing_mode not in AVAILABLE_SWING_MODES: raise HomeAssistantError( - f"Climate swing mode {swing_mode} is not supported by the integration, please open an issue" + f"Climate swing mode {swing_mode} is not supported by the integration, please open an issue", + translation_domain=DOMAIN, + translation_key="swing_not_supported", + translation_placeholders={"swing_mode": swing_mode}, ) transformation = self.device_data.swing_modes_translated diff --git a/homeassistant/components/sensibo/entity.py b/homeassistant/components/sensibo/entity.py index 0a60fc4a85d..f9056fa6624 100644 --- a/homeassistant/components/sensibo/entity.py +++ b/homeassistant/components/sensibo/entity.py @@ -26,15 +26,27 @@ def async_handle_api_call( async def wrap_api_call(entity: _T, *args: _P.args, **kwargs: _P.kwargs) -> None: """Wrap services for api calls.""" res: bool = False + if TYPE_CHECKING: + assert isinstance(entity.name, str) try: async with asyncio.timeout(TIMEOUT): res = await function(entity, *args, **kwargs) except SENSIBO_ERRORS as err: - raise HomeAssistantError from err + raise HomeAssistantError( + str(err), + translation_domain=DOMAIN, + translation_key="service_raised", + translation_placeholders={"error": str(err), "name": entity.name}, + ) from err LOGGER.debug("Result %s for entity %s with arguments %s", res, entity, kwargs) if res is not True: - raise HomeAssistantError(f"Could not execute service for {entity.name}") + raise HomeAssistantError( + f"Could not execute service for {entity.name}", + translation_domain=DOMAIN, + translation_key="service_result_not_true", + translation_placeholders={"name": entity.name}, + ) if ( isinstance(key := kwargs.get("key"), str) and (value := kwargs.get("value")) is not None diff --git a/homeassistant/components/sensibo/select.py b/homeassistant/components/sensibo/select.py index cda8a972ede..3e13c6cec70 100644 --- a/homeassistant/components/sensibo/select.py +++ b/homeassistant/components/sensibo/select.py @@ -106,9 +106,16 @@ class SensiboSelect(SensiboDeviceBaseEntity, SelectEntity): async def async_select_option(self, option: str) -> None: """Set state to the selected option.""" if self.entity_description.key not in self.device_data.active_features: + hvac_mode = self.device_data.hvac_mode if self.device_data.hvac_mode else "" raise HomeAssistantError( f"Current mode {self.device_data.hvac_mode} doesn't support setting" - f" {self.entity_description.name}" + f" {self.entity_description.name}", + translation_domain=DOMAIN, + translation_key="select_option_not_available", + translation_placeholders={ + "hvac_mode": hvac_mode, + "key": self.entity_description.key, + }, ) await self.async_send_api_call( diff --git a/homeassistant/components/sensibo/strings.json b/homeassistant/components/sensibo/strings.json index 6081c668d89..a5f71e53c17 100644 --- a/homeassistant/components/sensibo/strings.json +++ b/homeassistant/components/sensibo/strings.json @@ -478,5 +478,37 @@ } } } + }, + "exceptions": { + "no_target_temperature_in_features": { + "message": "Current mode doesn't support setting target temperature" + }, + "no_target_temperature": { + "message": "No target temperature provided" + }, + "no_fan_level_in_features": { + "message": "Current mode doesn't support setting fan level" + }, + "fan_mode_not_supported": { + "message": "Climate fan mode {fan_mode} is not supported by the integration, please open an issue" + }, + "no_swing_in_features": { + "message": "Current mode doesn't support setting swing" + }, + "swing_not_supported": { + "message": "Climate swing mode {swing_mode} is not supported by the integration, please open an issue" + }, + "service_result_not_true": { + "message": "Could not execute service for {name}" + }, + "service_raised": { + "message": "Could not execute service for {name} with error {error}" + }, + "select_option_not_available": { + "message": "Current mode {hvac_mode} doesn't support setting {key}" + }, + "climate_react_not_available": { + "message": "Use Sensibo Enable Climate React Service once to enable switch or the Sensibo app" + } } } diff --git a/homeassistant/components/sensibo/switch.py b/homeassistant/components/sensibo/switch.py index 204ed622f13..a27307fcceb 100644 --- a/homeassistant/components/sensibo/switch.py +++ b/homeassistant/components/sensibo/switch.py @@ -184,7 +184,9 @@ class SensiboDeviceSwitch(SensiboDeviceBaseEntity, SwitchEntity): if self.device_data.smart_type is None: raise HomeAssistantError( "Use Sensibo Enable Climate React Service once to enable switch or the" - " Sensibo app" + " Sensibo app", + translation_domain=DOMAIN, + translation_key="climate_react_not_available", ) data: dict[str, Any] = {"enabled": value} result = await self._client.async_enable_climate_react(self._device_id, data) diff --git a/tests/components/sensibo/test_climate.py b/tests/components/sensibo/test_climate.py index 9cf0a8972a9..71680733098 100644 --- a/tests/components/sensibo/test_climate.py +++ b/tests/components/sensibo/test_climate.py @@ -55,7 +55,7 @@ from homeassistant.const import ( STATE_UNKNOWN, ) from homeassistant.core import HomeAssistant -from homeassistant.exceptions import HomeAssistantError +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed @@ -438,7 +438,7 @@ async def test_climate_temperature_is_none( with patch( "homeassistant.components.sensibo.util.SensiboClient.async_set_ac_state_property", - ), pytest.raises(ValueError): + ), pytest.raises(ServiceValidationError): await hass.services.async_call( CLIMATE_DOMAIN, SERVICE_SET_TEMPERATURE,