Add exception translations for KNX services (#146104)

This commit is contained in:
Matthias Alphart 2025-06-03 11:31:32 +02:00 committed by GitHub
parent 842e7ce171
commit 980dbf364d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 6 deletions

View File

@ -99,7 +99,7 @@ rules:
status: exempt
comment: |
Since all entities are configured manually, names are user-defined.
exception-translations: todo
exception-translations: done
icon-translations: done
reconfiguration-flow: todo
repair-issues: todo

View File

@ -87,7 +87,9 @@ def get_knx_module(hass: HomeAssistant) -> KNXModule:
try:
return hass.data[KNX_MODULE_KEY]
except KeyError as err:
raise HomeAssistantError("KNX entry not loaded") from err
raise HomeAssistantError(
translation_domain=DOMAIN, translation_key="integration_not_loaded"
) from err
SERVICE_KNX_EVENT_REGISTER_SCHEMA = vol.Schema(
@ -166,7 +168,11 @@ async def service_exposure_register_modify(call: ServiceCall) -> None:
removed_exposure = knx_module.service_exposures.pop(group_address)
except KeyError as err:
raise ServiceValidationError(
f"Could not find exposure for '{group_address}' to remove."
translation_domain=DOMAIN,
translation_key="service_exposure_remove_not_found",
translation_placeholders={
"group_address": group_address,
},
) from err
removed_exposure.async_remove()
@ -234,13 +240,17 @@ async def service_send_to_knx_bus(call: ServiceCall) -> None:
transcoder = DPTBase.parse_transcoder(attr_type)
if transcoder is None:
raise ServiceValidationError(
f"Invalid type for knx.send service: {attr_type}"
translation_domain=DOMAIN,
translation_key="service_send_invalid_type",
translation_placeholders={"type": attr_type},
)
try:
payload = transcoder.to_knx(attr_payload)
except ConversionError as err:
raise ServiceValidationError(
f"Invalid payload for knx.send service: {err}"
translation_domain=DOMAIN,
translation_key="service_send_invalid_payload",
translation_placeholders={"error": str(err)},
) from err
elif isinstance(attr_payload, int):
payload = DPTBinary(attr_payload)

View File

@ -143,6 +143,20 @@
"unsupported_tunnel_type": "Selected tunneling type not supported by gateway."
}
},
"exceptions": {
"integration_not_loaded": {
"message": "KNX integration is not loaded."
},
"service_exposure_remove_not_found": {
"message": "Could not find exposure for `{group_address}` to remove."
},
"service_send_invalid_payload": {
"message": "Invalid payload for `knx.send` service. {error}"
},
"service_send_invalid_type": {
"message": "Invalid type for `knx.send` service: {type}"
}
},
"options": {
"step": {
"init": {

View File

@ -6,6 +6,7 @@ import pytest
from xknx.telegram.apci import GroupValueResponse, GroupValueWrite
from homeassistant.components.knx import async_unload_entry as knx_async_unload_entry
from homeassistant.components.knx.const import DOMAIN
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
@ -295,4 +296,5 @@ async def test_service_setup_failed(hass: HomeAssistant, knx: KNXTestKit) -> Non
{"address": "1/2/3", "payload": True, "response": False},
blocking=True,
)
assert str(exc_info.value) == "KNX entry not loaded"
assert exc_info.value.translation_domain == DOMAIN
assert exc_info.value.translation_key == "integration_not_loaded"