Use HomeAssistant error in the right cases (#131923)

* Use the correct exceptions

* Improved exception strings
This commit is contained in:
J. Diego Rodríguez Royo 2024-11-30 04:11:15 +01:00 committed by GitHub
parent 24bd61be3b
commit 8c6a24c368
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 47 additions and 47 deletions

View File

@ -16,7 +16,7 @@ from homeassistant.components.light import (
LightEntityDescription,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.util.color as color_util
@ -150,7 +150,7 @@ class HomeConnectLight(HomeConnectEntity, LightEntity):
self.device.appliance.set_setting, self.bsh_key, True
)
except HomeConnectError as err:
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="turn_on_light",
translation_placeholders={
@ -169,7 +169,7 @@ class HomeConnectLight(HomeConnectEntity, LightEntity):
self._enable_custom_color_value_key,
)
except HomeConnectError as err:
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="select_light_custom_color",
translation_placeholders={
@ -187,7 +187,7 @@ class HomeConnectLight(HomeConnectEntity, LightEntity):
f"#{hex_val}",
)
except HomeConnectError as err:
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="set_light_color",
translation_placeholders={
@ -219,7 +219,7 @@ class HomeConnectLight(HomeConnectEntity, LightEntity):
f"#{hex_val}",
)
except HomeConnectError as err:
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="set_light_color",
translation_placeholders={
@ -244,7 +244,7 @@ class HomeConnectLight(HomeConnectEntity, LightEntity):
self.device.appliance.set_setting, self._brightness_key, brightness
)
except HomeConnectError as err:
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="set_light_brightness",
translation_placeholders={
@ -263,7 +263,7 @@ class HomeConnectLight(HomeConnectEntity, LightEntity):
self.device.appliance.set_setting, self.bsh_key, False
)
except HomeConnectError as err:
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="turn_off_light",
translation_placeholders={

View File

@ -12,7 +12,7 @@ from homeassistant.components.number import (
NumberEntityDescription,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import HomeConnectConfigEntry, get_dict_from_home_connect_error
@ -117,7 +117,7 @@ class HomeConnectNumberEntity(HomeConnectEntity, NumberEntity):
value,
)
except HomeConnectError as err:
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="set_setting",
translation_placeholders={

View File

@ -7,7 +7,7 @@ from homeconnect.api import HomeConnectError
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import (
@ -289,7 +289,7 @@ class HomeConnectProgramSelectEntity(HomeConnectEntity, SelectEntity):
translation_key = "start_program"
else:
translation_key = "select_program"
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key=translation_key,
translation_placeholders={

View File

@ -23,43 +23,43 @@
},
"exceptions": {
"turn_on_light": {
"message": "Error while trying to turn on {entity_id}: {description}"
"message": "Error turning on {entity_id}: {description}"
},
"turn_off_light": {
"message": "Error while trying to turn off {entity_id}: {description}"
"message": "Error turning off {entity_id}: {description}"
},
"set_light_brightness": {
"message": "Error while trying to set brightness of {entity_id}: {description}"
"message": "Error setting brightness of {entity_id}: {description}"
},
"select_light_custom_color": {
"message": "Error while trying to select custom color of {entity_id}: {description}"
"message": "Error selecting custom color of {entity_id}: {description}"
},
"set_light_color": {
"message": "Error while trying to set color of {entity_id}: {description}"
"message": "Error setting color of {entity_id}: {description}"
},
"set_setting": {
"message": "Error while trying to assign the value \"{value}\" to the setting \"{setting_key}\" for {entity_id}: {description}"
"message": "Error assigning the value \"{value}\" to the setting \"{setting_key}\" for {entity_id}: {description}"
},
"turn_on": {
"message": "Error while trying to turn on {entity_id} ({setting_key}): {description}"
"message": "Error turning on {entity_id} ({setting_key}): {description}"
},
"turn_off": {
"message": "Error while trying to turn off {entity_id} ({setting_key}): {description}"
"message": "Error turning off {entity_id} ({setting_key}): {description}"
},
"select_program": {
"message": "Error while trying to select program {program}: {description}"
"message": "Error selecting program {program}: {description}"
},
"start_program": {
"message": "Error while trying to start program {program}: {description}"
"message": "Error starting program {program}: {description}"
},
"stop_program": {
"message": "Error while trying to stop program {program}: {description}"
"message": "Error stopping program {program}: {description}"
},
"power_on": {
"message": "Error while trying to turn on {appliance_name}: {description}"
"message": "Error turning on {appliance_name}: {description}"
},
"power_off": {
"message": "Error while trying to turn off {appliance_name} with value \"{value}\": {description}"
"message": "Error turning off {appliance_name} with value \"{value}\": {description}"
},
"turn_off_not_supported": {
"message": "{appliance_name} does not support turning off or entering standby mode."

View File

@ -8,7 +8,7 @@ from homeconnect.api import HomeConnectError
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import HomeConnectConfigEntry, get_dict_from_home_connect_error
@ -134,7 +134,7 @@ class HomeConnectSwitch(HomeConnectEntity, SwitchEntity):
)
except HomeConnectError as err:
self._attr_available = False
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="turn_on",
translation_placeholders={
@ -158,7 +158,7 @@ class HomeConnectSwitch(HomeConnectEntity, SwitchEntity):
except HomeConnectError as err:
_LOGGER.error("Error while trying to turn off: %s", err)
self._attr_available = False
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="turn_off",
translation_placeholders={
@ -209,7 +209,7 @@ class HomeConnectProgramSwitch(HomeConnectEntity, SwitchEntity):
self.device.appliance.start_program, self.program_name
)
except HomeConnectError as err:
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="start_program",
translation_placeholders={
@ -225,7 +225,7 @@ class HomeConnectProgramSwitch(HomeConnectEntity, SwitchEntity):
try:
await self.hass.async_add_executor_job(self.device.appliance.stop_program)
except HomeConnectError as err:
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="stop_program",
translation_placeholders={
@ -278,7 +278,7 @@ class HomeConnectPowerSwitch(HomeConnectEntity, SwitchEntity):
)
except HomeConnectError as err:
self._attr_is_on = False
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="power_on",
translation_placeholders={
@ -291,7 +291,7 @@ class HomeConnectPowerSwitch(HomeConnectEntity, SwitchEntity):
async def async_turn_off(self, **kwargs: Any) -> None:
"""Switch the device off."""
if not hasattr(self, "power_off_state"):
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="unable_to_retrieve_turn_off",
translation_placeholders={
@ -300,7 +300,7 @@ class HomeConnectPowerSwitch(HomeConnectEntity, SwitchEntity):
)
if self.power_off_state is None:
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="turn_off_not_supported",
translation_placeholders={
@ -316,7 +316,7 @@ class HomeConnectPowerSwitch(HomeConnectEntity, SwitchEntity):
)
except HomeConnectError as err:
self._attr_is_on = True
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="power_off",
translation_placeholders={

View File

@ -7,7 +7,7 @@ from homeconnect.api import HomeConnectError
from homeassistant.components.time import TimeEntity, TimeEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import HomeConnectConfigEntry, get_dict_from_home_connect_error
@ -80,7 +80,7 @@ class HomeConnectTimeEntity(HomeConnectEntity, TimeEntity):
time_to_seconds(value),
)
except HomeConnectError as err:
raise ServiceValidationError(
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="set_setting",
translation_placeholders={

View File

@ -27,7 +27,7 @@ from homeassistant.const import (
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError
from .conftest import get_all_appliances
@ -341,7 +341,7 @@ async def test_switch_exception_handling(
problematic_appliance.status.update(status)
service_data["entity_id"] = entity_id
with pytest.raises(ServiceValidationError, match=exception_match):
with pytest.raises(HomeAssistantError, match=exception_match):
await hass.services.async_call(
LIGHT_DOMAIN, service, service_data, blocking=True
)

View File

@ -24,7 +24,7 @@ from homeassistant.components.number import (
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError
from .conftest import get_all_appliances
@ -162,7 +162,7 @@ async def test_number_entity_error(
getattr(problematic_appliance, mock_attr)()
with pytest.raises(
ServiceValidationError, match=r"Error.*assign.*value.*to.*setting.*"
HomeAssistantError, match=r"Error.*assign.*value.*to.*setting.*"
):
await hass.services.async_call(
NUMBER_DOMAIN,

View File

@ -14,7 +14,7 @@ from homeassistant.components.select import ATTR_OPTION, DOMAIN as SELECT_DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_SELECT_OPTION, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError
from .conftest import get_all_appliances
@ -151,7 +151,7 @@ async def test_select_exception_handling(
getattr(problematic_appliance, mock_attr)()
problematic_appliance.status.update(status)
with pytest.raises(ServiceValidationError, match=exception_match):
with pytest.raises(HomeAssistantError, match=exception_match):
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,

View File

@ -29,7 +29,7 @@ from homeassistant.const import (
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError
from .conftest import get_all_appliances
@ -219,7 +219,7 @@ async def test_switch_exception_handling(
with pytest.raises(HomeConnectError):
getattr(problematic_appliance, mock_attr)()
with pytest.raises(ServiceValidationError, match=exception_match):
with pytest.raises(HomeAssistantError, match=exception_match):
await hass.services.async_call(
SWITCH_DOMAIN, service, {"entity_id": entity_id}, blocking=True
)
@ -344,7 +344,7 @@ async def test_ent_desc_switch_exception_handling(
getattr(problematic_appliance, mock_attr)()
problematic_appliance.status.update(status)
with pytest.raises(ServiceValidationError, match=exception_match):
with pytest.raises(HomeAssistantError, match=exception_match):
await hass.services.async_call(
SWITCH_DOMAIN, service, {ATTR_ENTITY_ID: entity_id}, blocking=True
)
@ -502,7 +502,7 @@ async def test_power_switch_service_validation_errors(
appliance.status.update({BSH_POWER_STATE: {"value": BSH_POWER_ON}})
with pytest.raises(ServiceValidationError, match=exception_match):
with pytest.raises(HomeAssistantError, match=exception_match):
await hass.services.async_call(
SWITCH_DOMAIN, service, {"entity_id": entity_id}, blocking=True
)

View File

@ -12,7 +12,7 @@ from homeassistant.components.time import DOMAIN as TIME_DOMAIN, SERVICE_SET_VAL
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ATTR_ENTITY_ID, ATTR_TIME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError
from .conftest import get_all_appliances
@ -136,7 +136,7 @@ async def test_time_entity_error(
getattr(problematic_appliance, mock_attr)()
with pytest.raises(
ServiceValidationError, match=r"Error.*assign.*value.*to.*setting.*"
HomeAssistantError, match=r"Error.*assign.*value.*to.*setting.*"
):
await hass.services.async_call(
TIME_DOMAIN,