Fix error handling in Shell Command integration (#116409)

* raise proper HomeAssistantError on command timeout

* raise proper HomeAssistantError on non-utf8 command output

* add error translation and test it

* Update homeassistant/components/shell_command/strings.json

* Update tests/components/shell_command/test_init.py

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
This commit is contained in:
Michael
2024-04-29 20:19:14 +02:00
committed by GitHub
parent d1c58467c5
commit 50d83bbdbf
3 changed files with 35 additions and 8 deletions

View File

@ -11,7 +11,7 @@ import pytest
from homeassistant.components import shell_command
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import TemplateError
from homeassistant.exceptions import HomeAssistantError, TemplateError
from homeassistant.setup import async_setup_component
@ -199,7 +199,10 @@ async def test_non_text_stdout_capture(
assert not response
# Non-text output throws with 'return_response'
with pytest.raises(UnicodeDecodeError):
with pytest.raises(
HomeAssistantError,
match="Unable to handle non-utf8 output of command: `curl -o - https://raw.githubusercontent.com/home-assistant/assets/master/misc/loading-screen.gif`",
):
response = await hass.services.async_call(
"shell_command", "output_image", blocking=True, return_response=True
)
@ -258,7 +261,10 @@ async def test_do_not_run_forever(
side_effect=mock_create_subprocess_shell,
),
):
with pytest.raises(TimeoutError):
with pytest.raises(
HomeAssistantError,
match="Timed out running command: `mock_sleep 10000`, after: 0.001 seconds",
):
await hass.services.async_call(
shell_command.DOMAIN,
"test_service",