diff --git a/homeassistant/components/lcn/__init__.py b/homeassistant/components/lcn/__init__.py index 43438fa64dd..77d1bb4e709 100644 --- a/homeassistant/components/lcn/__init__.py +++ b/homeassistant/components/lcn/__init__.py @@ -104,7 +104,11 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: LcnConfigEntry) - ) as ex: await lcn_connection.async_close() raise ConfigEntryNotReady( - f"Unable to connect to {config_entry.title}: {ex}" + translation_domain=DOMAIN, + translation_key="cannot_connect", + translation_placeholders={ + "config_entry_title": config_entry.title, + }, ) from ex _LOGGER.info('LCN connected to "%s"', config_entry.title) diff --git a/homeassistant/components/lcn/helpers.py b/homeassistant/components/lcn/helpers.py index 515f64b6e31..4937b5dbca7 100644 --- a/homeassistant/components/lcn/helpers.py +++ b/homeassistant/components/lcn/helpers.py @@ -26,6 +26,7 @@ from homeassistant.const import ( CONF_SWITCHES, ) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.typing import ConfigType @@ -100,7 +101,11 @@ def get_resource(domain_name: str, domain_data: ConfigType) -> str: return cast(str, domain_data["setpoint"]) if domain_name == "scene": return f"{domain_data['register']}{domain_data['scene']}" - raise ValueError("Unknown domain") + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="invalid_domain", + translation_placeholders={CONF_DOMAIN: domain_name}, + ) def generate_unique_id( @@ -304,6 +309,8 @@ def get_device_config( def is_states_string(states_string: str) -> list[str]: """Validate the given states string and return states list.""" if len(states_string) != 8: - raise ValueError("Invalid length of states string") + raise HomeAssistantError( + translation_domain=DOMAIN, translation_key="invalid_length_of_states_string" + ) states = {"1": "ON", "0": "OFF", "T": "TOGGLE", "-": "NOCHANGE"} return [states[state_string] for state_string in states_string] diff --git a/homeassistant/components/lcn/quality_scale.yaml b/homeassistant/components/lcn/quality_scale.yaml index 26be4d210ba..35d76a2ebdc 100644 --- a/homeassistant/components/lcn/quality_scale.yaml +++ b/homeassistant/components/lcn/quality_scale.yaml @@ -19,7 +19,7 @@ rules: test-before-setup: done unique-config-entry: done # Silver - action-exceptions: todo + action-exceptions: done config-entry-unloading: done docs-configuration-parameters: status: exempt diff --git a/homeassistant/components/lcn/services.py b/homeassistant/components/lcn/services.py index 15d60639a1c..8a172ccac2e 100644 --- a/homeassistant/components/lcn/services.py +++ b/homeassistant/components/lcn/services.py @@ -330,8 +330,9 @@ class SendKeys(LcnServiceCall): if (delay_time := service.data[CONF_TIME]) != 0: hit = pypck.lcn_defs.SendKeyCommand.HIT if pypck.lcn_defs.SendKeyCommand[service.data[CONF_STATE]] != hit: - raise ValueError( - "Only hit command is allowed when sending deferred keys." + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="invalid_send_keys_action", ) delay_unit = pypck.lcn_defs.TimeUnit.parse(service.data[CONF_TIME_UNIT]) await device_connection.send_keys_hit_deferred(keys, delay_time, delay_unit) @@ -368,8 +369,9 @@ class LockKeys(LcnServiceCall): if (delay_time := service.data[CONF_TIME]) != 0: if table_id != 0: - raise ValueError( - "Only table A is allowed when locking keys for a specific time." + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="invalid_lock_keys_table", ) delay_unit = pypck.lcn_defs.TimeUnit.parse(service.data[CONF_TIME_UNIT]) await device_connection.lock_keys_tab_a_temporary( diff --git a/homeassistant/components/lcn/strings.json b/homeassistant/components/lcn/strings.json index 9d806bce104..4e4ca7e0dcd 100644 --- a/homeassistant/components/lcn/strings.json +++ b/homeassistant/components/lcn/strings.json @@ -414,11 +414,23 @@ } }, "exceptions": { - "invalid_address": { - "message": "LCN device for given address has not been configured." + "cannot_connect": { + "message": "Unable to connect to {config_entry_title}." }, "invalid_device_id": { - "message": "LCN device for given device ID has not been configured." + "message": "LCN device for given device ID {device_id} has not been configured." + }, + "invalid_domain": { + "message": "Invalid domain {domain}." + }, + "invalid_send_keys_action": { + "message": "Invalid state for sending keys. Only 'hit' allowed for deferred sending." + }, + "invalid_lock_keys_table": { + "message": "Invalid table for locking keys. Only table A allowed when locking for a specific time." + }, + "invalid_length_of_states_string": { + "message": "Invalid length of states string. Expected 8 characters." } } } diff --git a/tests/components/lcn/test_services.py b/tests/components/lcn/test_services.py index cdc8e9671c0..46ede8959ff 100644 --- a/tests/components/lcn/test_services.py +++ b/tests/components/lcn/test_services.py @@ -30,6 +30,7 @@ from homeassistant.const import ( CONF_UNIT_OF_MEASUREMENT, ) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.setup import async_setup_component from .conftest import ( @@ -134,6 +135,23 @@ async def test_service_relays( control_relays.assert_awaited_with(relay_states) + # wrong states string + with ( + patch.object(MockModuleConnection, "control_relays") as control_relays, + pytest.raises(HomeAssistantError) as exc_info, + ): + await hass.services.async_call( + DOMAIN, + LcnService.RELAYS, + { + CONF_DEVICE_ID: get_device(hass, entry, (0, 7, False)).id, + CONF_STATE: "0011TT--00", + }, + blocking=True, + ) + assert exc_info.value.translation_domain == DOMAIN + assert exc_info.value.translation_key == "invalid_length_of_states_string" + async def test_service_led( hass: HomeAssistant, @@ -328,7 +346,7 @@ async def test_service_send_keys_hit_deferred( patch.object( MockModuleConnection, "send_keys_hit_deferred" ) as send_keys_hit_deferred, - pytest.raises(ValueError), + pytest.raises(ServiceValidationError) as exc_info, ): await hass.services.async_call( DOMAIN, @@ -342,6 +360,8 @@ async def test_service_send_keys_hit_deferred( }, blocking=True, ) + assert exc_info.value.translation_domain == DOMAIN + assert exc_info.value.translation_key == "invalid_send_keys_action" async def test_service_lock_keys( @@ -369,6 +389,24 @@ async def test_service_lock_keys( lock_keys.assert_awaited_with(0, lock_states) + # wrong states string + with ( + patch.object(MockModuleConnection, "lock_keys") as lock_keys, + pytest.raises(HomeAssistantError) as exc_info, + ): + await hass.services.async_call( + DOMAIN, + LcnService.LOCK_KEYS, + { + CONF_DEVICE_ID: get_device(hass, entry, (0, 7, False)).id, + CONF_TABLE: "a", + CONF_STATE: "0011TT--00", + }, + blocking=True, + ) + assert exc_info.value.translation_domain == DOMAIN + assert exc_info.value.translation_key == "invalid_length_of_states_string" + async def test_service_lock_keys_tab_a_temporary( hass: HomeAssistant, @@ -406,7 +444,7 @@ async def test_service_lock_keys_tab_a_temporary( patch.object( MockModuleConnection, "lock_keys_tab_a_temporary" ) as lock_keys_tab_a_temporary, - pytest.raises(ValueError), + pytest.raises(ServiceValidationError) as exc_info, ): await hass.services.async_call( DOMAIN, @@ -420,6 +458,8 @@ async def test_service_lock_keys_tab_a_temporary( }, blocking=True, ) + assert exc_info.value.translation_domain == DOMAIN + assert exc_info.value.translation_key == "invalid_lock_keys_table" async def test_service_dyn_text( diff --git a/tests/components/lcn/test_websocket.py b/tests/components/lcn/test_websocket.py index 02bf6b4c546..75d8a605bfb 100644 --- a/tests/components/lcn/test_websocket.py +++ b/tests/components/lcn/test_websocket.py @@ -192,6 +192,16 @@ async def test_lcn_entities_add_command( assert entity_config in entry.data[CONF_ENTITIES] + # invalid domain + await client.send_json_auto_id( + {**ENTITIES_ADD_PAYLOAD, "entry_id": entry.entry_id, CONF_DOMAIN: "invalid"} + ) + + res = await client.receive_json() + assert not res["success"] + assert res["error"]["code"] == "home_assistant_error" + assert res["error"]["translation_key"] == "invalid_domain" + async def test_lcn_entities_delete_command( hass: HomeAssistant, hass_ws_client: WebSocketGenerator, entry: MockConfigEntry