Skip to content

Commit

Permalink
Add translation support for ServiceNotFound exc
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh committed Oct 24, 2023
1 parent cd8cb1b commit 173183b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 35 deletions.
10 changes: 8 additions & 2 deletions homeassistant/components/websocket_api/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,15 @@ async def handle_call_service(
connection.send_result(msg["id"], {"context": context})
except ServiceNotFound as err:
if err.domain == msg["domain"] and err.service == msg["service"]:
connection.send_error(msg["id"], const.ERR_NOT_FOUND, "Service not found.")
err.translation_key = "service_not_found_general"
err.domain = const.DOMAIN
code = const.ERR_NOT_FOUND
else:
connection.send_error(msg["id"], const.ERR_HOME_ASSISTANT_ERROR, str(err))
err.translation_key = "service_not_found"
err.domain = const.DOMAIN
code = const.ERR_HOME_ASSISTANT_ERROR
message = await async_build_error_message(hass, err)
connection.send_error(msg["id"], code, message)
except vol.Invalid as err:
connection.send_error(msg["id"], const.ERR_INVALID_FORMAT, str(err))
except ServiceValidationError as err:
Expand Down
10 changes: 10 additions & 0 deletions homeassistant/components/websocket_api/strings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"exceptions": {
"service_not_found_general": {
"message": "Service not found"
},
"service_not_found": {
"message": "Service {domain}.{service} not found"
}
}
}
1 change: 1 addition & 0 deletions homeassistant/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class ServiceNotFound(HomeAssistantError):
def __init__(self, domain: str, service: str) -> None:
"""Initialize error."""
super().__init__(self, f"Service {domain}.{service} not found")
self.translation_placeholders = {"domain": domain, "service": service}
self.domain = domain
self.service = service

Expand Down
5 changes: 0 additions & 5 deletions homeassistant/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@
"unknown_authorize_url_generation": "Unknown error generating an authorize URL.",
"cloud_not_connected": "Not connected to Home Assistant Cloud."
}
},
"exceptions": {
"service_not_found": {
"message": "Service not found"
}
}
}
}
76 changes: 48 additions & 28 deletions tests/components/websocket_api/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,21 +328,31 @@ async def test_call_service_not_found(
hass: HomeAssistant, websocket_client: MockHAClientWebSocket
) -> None:
"""Test call service command."""
await websocket_client.send_json(
{
"id": 5,
"type": "call_service",
"domain": "domain_test",
"service": "test_service",
"service_data": {"hello": "world"},
}
)
translations = {
"component.websocket_api.exceptions.service_not_found_general.message": "Service not found",
"component.websocket_api.exceptions.service_not_found.message": "Service {domain}.{service} not found",
}
with patch(
"homeassistant.components.websocket_api.commands.async_get_translations",
return_value=translations,
) as mock_get_translations:
await websocket_client.send_json(
{
"id": 5,
"type": "call_service",
"domain": "domain_test",
"service": "test_service",
"service_data": {"hello": "world"},
}
)

msg = await websocket_client.receive_json()
assert msg["id"] == 5
assert msg["type"] == const.TYPE_RESULT
assert not msg["success"]
assert msg["error"]["code"] == const.ERR_NOT_FOUND
msg = await websocket_client.receive_json()
assert msg["id"] == 5
assert msg["type"] == const.TYPE_RESULT
assert not msg["success"]
assert msg["error"]["code"] == const.ERR_NOT_FOUND
assert msg["error"]["message"] == "Service not found"
assert len(mock_get_translations.mock_calls) == 1


async def test_call_service_child_not_found(
Expand All @@ -355,21 +365,31 @@ async def serv_handler(call):

hass.services.async_register("domain_test", "test_service", serv_handler)

await websocket_client.send_json(
{
"id": 5,
"type": "call_service",
"domain": "domain_test",
"service": "test_service",
"service_data": {"hello": "world"},
}
)
translations = {
"component.websocket_api.exceptions.service_not_found_general.message": "Service not found",
"component.websocket_api.exceptions.service_not_found.message": "Service {domain}.{service} not found",
}
with patch(
"homeassistant.components.websocket_api.commands.async_get_translations",
return_value=translations,
) as mock_get_translations:
await websocket_client.send_json(
{
"id": 5,
"type": "call_service",
"domain": "domain_test",
"service": "test_service",
"service_data": {"hello": "world"},
}
)

msg = await websocket_client.receive_json()
assert msg["id"] == 5
assert msg["type"] == const.TYPE_RESULT
assert not msg["success"]
assert msg["error"]["code"] == const.ERR_HOME_ASSISTANT_ERROR
msg = await websocket_client.receive_json()
assert msg["id"] == 5
assert msg["type"] == const.TYPE_RESULT
assert not msg["success"]
assert msg["error"]["code"] == const.ERR_HOME_ASSISTANT_ERROR
assert msg["error"]["message"] == "Service non.existing not found"
assert len(mock_get_translations.mock_calls) == 1


async def test_call_service_schema_validation_error(
Expand Down

0 comments on commit 173183b

Please sign in to comment.