From 80b404f35164dbbd438a1ebedba872bd68bcdde2 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Fri, 16 Feb 2024 16:23:47 +0100 Subject: [PATCH] Raise translatable exceptions in entity set methods for BSBLan (#105693) Co-authored-by: Robert Resch --- homeassistant/components/bsblan/climate.py | 18 ++++++++++++++---- homeassistant/components/bsblan/strings.json | 8 ++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/bsblan/climate.py b/homeassistant/components/bsblan/climate.py index 511701cb538..1be595bf1cc 100644 --- a/homeassistant/components/bsblan/climate.py +++ b/homeassistant/components/bsblan/climate.py @@ -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() diff --git a/homeassistant/components/bsblan/strings.json b/homeassistant/components/bsblan/strings.json index 689d1f893d3..7a67d353803 100644 --- a/homeassistant/components/bsblan/strings.json +++ b/homeassistant/components/bsblan/strings.json @@ -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" + } } }