Raise translatable exceptions in entity set methods for BSBLan (#105693)

Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
Jan-Philipp Benecke 2024-02-16 16:23:47 +01:00 committed by GitHub
parent 3392660537
commit 80b404f351
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 4 deletions

View File

@ -17,6 +17,7 @@ from homeassistant.components.climate import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
@ -26,7 +27,7 @@ from homeassistant.helpers.update_coordinator import (
from homeassistant.util.enum import try_parse_enum
from . import HomeAssistantBSBLANData
from .const import ATTR_TARGET_TEMPERATURE, DOMAIN, LOGGER
from .const import ATTR_TARGET_TEMPERATURE, DOMAIN
from .entity import BSBLANEntity
PARALLEL_UPDATES = 1
@ -147,7 +148,12 @@ class BSBLANClimate(
if self.hvac_mode == HVACMode.AUTO:
await self.async_set_data(preset_mode=preset_mode)
else:
LOGGER.error("Can't set preset mode when hvac mode is not auto")
raise ServiceValidationError(
"Can't set preset mode when hvac mode is not auto",
translation_domain=DOMAIN,
translation_key="set_preset_mode_error",
translation_placeholders={"preset_mode": preset_mode},
)
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperatures."""
@ -168,6 +174,10 @@ class BSBLANClimate(
data[ATTR_HVAC_MODE] = kwargs[ATTR_PRESET_MODE]
try:
await self.client.thermostat(**data)
except BSBLANError:
LOGGER.error("An error occurred while updating the BSBLAN device")
except BSBLANError as err:
raise HomeAssistantError(
"An error occurred while updating the BSBLAN device",
translation_domain=DOMAIN,
translation_key="set_data_error",
) from err
await self.coordinator.async_request_refresh()

View File

@ -24,5 +24,13 @@
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]"
}
},
"exceptions": {
"set_preset_mode_error": {
"message": "Can't set preset mode to {preset_mode} when HVAC mode is not set to auto"
},
"set_data_error": {
"message": "An error occurred while sending the data to the BSBLAN device"
}
}
}