Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
HomeAssistant
Core
Commits
f0e9dccb
Unverified
Commit
f0e9dccb
authored
1 year ago
by
RoboMagus
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Only handle shell commands output when return_response requested (#97777)
parent
07a70155
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
homeassistant/components/shell_command/__init__.py
+18
-9
18 additions, 9 deletions
homeassistant/components/shell_command/__init__.py
tests/components/shell_command/test_init.py
+34
-0
34 additions, 0 deletions
tests/components/shell_command/test_init.py
with
52 additions
and
9 deletions
homeassistant/components/shell_command/__init__.py
+
18
−
9
View file @
f0e9dccb
...
...
@@ -105,14 +105,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
raise
service_response
:
JsonObjectType
=
{
"
stdout
"
:
""
,
"
stderr
"
:
""
,
"
returncode
"
:
process
.
returncode
,
}
if
stdout_data
:
service_response
[
"
stdout
"
]
=
stdout_data
.
decode
(
"
utf-8
"
).
strip
()
_LOGGER
.
debug
(
"
Stdout of command: `%s`, return code: %s:
\n
%s
"
,
cmd
,
...
...
@@ -120,7 +113,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
stdout_data
,
)
if
stderr_data
:
service_response
[
"
stderr
"
]
=
stderr_data
.
decode
(
"
utf-8
"
).
strip
()
_LOGGER
.
debug
(
"
Stderr of command: `%s`, return code: %s:
\n
%s
"
,
cmd
,
...
...
@@ -132,7 +124,24 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"
Error running command: `%s`, return code: %s
"
,
cmd
,
process
.
returncode
)
return
service_response
if
service
.
return_response
:
service_response
:
JsonObjectType
=
{
"
stdout
"
:
""
,
"
stderr
"
:
""
,
"
returncode
"
:
process
.
returncode
,
}
try
:
if
stdout_data
:
service_response
[
"
stdout
"
]
=
stdout_data
.
decode
(
"
utf-8
"
).
strip
()
if
stderr_data
:
service_response
[
"
stderr
"
]
=
stderr_data
.
decode
(
"
utf-8
"
).
strip
()
return
service_response
except
UnicodeDecodeError
:
_LOGGER
.
exception
(
"
Unable to handle non-utf8 output of command: `%s`
"
,
cmd
)
raise
return
None
for
name
in
conf
:
hass
.
services
.
async_register
(
...
...
This diff is collapsed.
Click to expand it.
tests/components/shell_command/test_init.py
+
34
−
0
View file @
f0e9dccb
...
...
@@ -174,6 +174,40 @@ async def test_stdout_captured(mock_output, hass: HomeAssistant) -> None:
assert
response
[
"
returncode
"
]
==
0
@patch
(
"
homeassistant.components.shell_command._LOGGER.debug
"
)
async
def
test_non_text_stdout_capture
(
mock_output
,
hass
:
HomeAssistant
,
caplog
:
pytest
.
LogCaptureFixture
)
->
None
:
"""
Test handling of non-text output.
"""
assert
await
async_setup_component
(
hass
,
shell_command
.
DOMAIN
,
{
shell_command
.
DOMAIN
:
{
"
output_image
"
:
"
curl -o - https://raw.githubusercontent.com/home-assistant/assets/master/misc/loading-screen.gif
"
}
},
)
# No problem without 'return_response'
response
=
await
hass
.
services
.
async_call
(
"
shell_command
"
,
"
output_image
"
,
blocking
=
True
)
await
hass
.
async_block_till_done
()
assert
not
response
# Non-text output throws with 'return_response'
with
pytest
.
raises
(
UnicodeDecodeError
):
response
=
await
hass
.
services
.
async_call
(
"
shell_command
"
,
"
output_image
"
,
blocking
=
True
,
return_response
=
True
)
await
hass
.
async_block_till_done
()
assert
not
response
assert
"
Unable to handle non-utf8 output of command
"
in
caplog
.
text
@patch
(
"
homeassistant.components.shell_command._LOGGER.debug
"
)
async
def
test_stderr_captured
(
mock_output
,
hass
:
HomeAssistant
)
->
None
:
"""
Test subprocess that has stderr.
"""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment