Skip to content

Commit 6abff84

Browse files
authored
Add exception translations for Splunk setup errors (home-assistant#163579)
1 parent 0996ad4 commit 6abff84

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

homeassistant/components/splunk/__init__.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,24 +179,42 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
179179
)
180180
except ClientConnectionError as err:
181181
raise ConfigEntryNotReady(
182-
f"Connection error connecting to Splunk at {host}:{port}: {err}"
182+
translation_domain=DOMAIN,
183+
translation_key="connection_error",
184+
translation_placeholders={
185+
"host": host,
186+
"port": str(port),
187+
"error": str(err),
188+
},
183189
) from err
184190
except TimeoutError as err:
185191
raise ConfigEntryNotReady(
186-
f"Timeout connecting to Splunk at {host}:{port}"
192+
translation_domain=DOMAIN,
193+
translation_key="timeout_connect",
194+
translation_placeholders={"host": host, "port": str(port)},
187195
) from err
188196
except Exception as err:
189197
_LOGGER.exception("Unexpected error setting up Splunk")
190198
raise ConfigEntryNotReady(
191-
f"Unexpected error connecting to Splunk: {err}"
199+
translation_domain=DOMAIN,
200+
translation_key="unexpected_error",
201+
translation_placeholders={
202+
"host": host,
203+
"port": str(port),
204+
"error": str(err),
205+
},
192206
) from err
193207

194208
if not connectivity_ok:
195209
raise ConfigEntryNotReady(
196-
f"Unable to connect to Splunk instance at {host}:{port}"
210+
translation_domain=DOMAIN,
211+
translation_key="cannot_connect",
212+
translation_placeholders={"host": host, "port": str(port)},
197213
)
198214
if not token_ok:
199-
raise ConfigEntryAuthFailed("Invalid Splunk token - please reauthenticate")
215+
raise ConfigEntryAuthFailed(
216+
translation_domain=DOMAIN, translation_key="invalid_auth"
217+
)
200218

201219
# Send startup event
202220
payload: dict[str, Any] = {

homeassistant/components/splunk/quality_scale.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ rules:
107107
status: exempt
108108
comment: |
109109
Integration does not create entities.
110-
exception-translations:
111-
status: todo
112-
comment: |
113-
Consider adding exception translations for user-facing errors beyond the current strings.json error section to provide more detailed translated error messages.
110+
exception-translations: done
114111
icon-translations:
115112
status: exempt
116113
comment: |

homeassistant/components/splunk/strings.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@
4848
}
4949
}
5050
},
51+
"exceptions": {
52+
"cannot_connect": {
53+
"message": "Unable to connect to Splunk at {host}:{port}."
54+
},
55+
"connection_error": {
56+
"message": "Unable to connect to Splunk at {host}:{port}: {error}."
57+
},
58+
"invalid_auth": {
59+
"message": "[%key:common::config_flow::error::invalid_auth%]"
60+
},
61+
"timeout_connect": {
62+
"message": "Connection to Splunk at {host}:{port} timed out."
63+
},
64+
"unexpected_error": {
65+
"message": "Unexpected error while connecting to Splunk at {host}:{port}: {error}."
66+
}
67+
},
5168
"issues": {
5269
"deprecated_yaml_import_issue_cannot_connect": {
5370
"description": "Configuring {integration_title} via YAML is deprecated and will be removed in a future release.\n\nWhile importing your configuration, a connection error occurred. Please correct your YAML configuration and restart Home Assistant, or remove the connection settings from your `{domain}:` configuration and configure the integration via the UI.\n\nNote: Entity filtering via YAML (`filter:`) will continue to work.",

tests/components/splunk/test_init.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,21 @@ async def test_setup_entry_success(
4141

4242

4343
@pytest.mark.parametrize(
44-
("side_effect", "expected_state"),
44+
("side_effect", "expected_state", "expected_error_key"),
4545
[
46-
([False, False], ConfigEntryState.SETUP_RETRY),
47-
(ClientConnectionError("Connection failed"), ConfigEntryState.SETUP_RETRY),
48-
(TimeoutError(), ConfigEntryState.SETUP_RETRY),
49-
([True, False], ConfigEntryState.SETUP_ERROR),
46+
([False, False], ConfigEntryState.SETUP_RETRY, "cannot_connect"),
47+
(
48+
ClientConnectionError("Connection failed"),
49+
ConfigEntryState.SETUP_RETRY,
50+
"connection_error",
51+
),
52+
(TimeoutError(), ConfigEntryState.SETUP_RETRY, "timeout_connect"),
53+
(
54+
Exception("Unexpected error"),
55+
ConfigEntryState.SETUP_RETRY,
56+
"unexpected_error",
57+
),
58+
([True, False], ConfigEntryState.SETUP_ERROR, "invalid_auth"),
5059
],
5160
)
5261
async def test_setup_entry_error(
@@ -55,6 +64,7 @@ async def test_setup_entry_error(
5564
mock_config_entry: MockConfigEntry,
5665
side_effect: Exception | list[bool],
5766
expected_state: ConfigEntryState,
67+
expected_error_key: str,
5868
) -> None:
5969
"""Test setup with various errors results in appropriate states."""
6070
mock_config_entry.add_to_hass(hass)
@@ -65,6 +75,7 @@ async def test_setup_entry_error(
6575
await hass.async_block_till_done()
6676

6777
assert mock_config_entry.state is expected_state
78+
assert mock_config_entry.error_reason_translation_key == expected_error_key
6879

6980

7081
async def test_unload_entry(

0 commit comments

Comments
 (0)