Add error translations for Sensibo (#105600)

This commit is contained in:
G Johansson 2023-12-13 19:40:57 +01:00 committed by GitHub
parent 6dc8c2c370
commit 65514fbd73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 86 additions and 13 deletions

View file

@ -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

View file

@ -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

View file

@ -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(

View file

@ -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"
}
}
}

View file

@ -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)

View file

@ -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,