diff --git a/homeassistant/components/command_line/utils.py b/homeassistant/components/command_line/utils.py index 46186db36cb4..f683791ed959 100644 --- a/homeassistant/components/command_line/utils.py +++ b/homeassistant/components/command_line/utils.py @@ -243,6 +243,13 @@ def render_template_args( prog, args = command.split(" ", 1) args_compiled = Template(args, hass) + if args_compiled.is_static: + # Rendering would still strip a trailing newline (from a YAML block + # scalar), making a plain command look like a substituted template. + issue_id = build_shell_template_issue_id(platform, name) + _update_issue(hass, issue_id, prog, platform, name, create=False) + LOGGER.debug("Running command: %s", command) + return command try: # parse_result=False keeps the output a string; the args are executed as a diff --git a/tests/components/command_line/test_sensor.py b/tests/components/command_line/test_sensor.py index 66a1197e2f0f..e97e8ad66547 100644 --- a/tests/components/command_line/test_sensor.py +++ b/tests/components/command_line/test_sensor.py @@ -1127,6 +1127,40 @@ async def test_template_with_shell_features_uses_shell_and_creates_issue( } +async def test_static_command_with_shell_features_no_issue( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Commands without a template keep the shell path and create no repair issue.""" + # A folded YAML block scalar (command: >) leaves a trailing newline, + # which rendering would strip. + command = "echo 'ROC temperature 42' | awk '/ROC temperature/ {print $NF}'\n" + await setup.async_setup_component( + hass, + DOMAIN, + {"command_line": [{"sensor": {"name": "Test", "command": command}}]}, + ) + await hass.async_block_till_done() + + with mock_asyncio_subprocess_run(b"42\n") as mock_shell: + async_fire_time_changed( + hass, + dt_util.utcnow() + timedelta(minutes=1), + ) + await hass.async_block_till_done(wait_background_tasks=True) + + mock_shell.assert_called_once_with( + command, + stdin=None, + stdout=-1, + close_fds=False, + ) + assert not any( + issue.translation_key == "shell_command_template_deprecation" + for issue in issue_registry.issues.values() + ) + + async def test_template_shell_feature_issue_cleared_when_metachar_gone( hass: HomeAssistant, issue_registry: ir.IssueRegistry, @@ -1345,18 +1379,26 @@ async def test_template_quoted_executable_uses_exec( ) +@pytest.mark.parametrize( + "edited_command", + [ + pytest.param("echo {{ 'clean' }}", id="template_without_shell_features"), + pytest.param("echo clean | cat", id="no_template"), + ], +) async def test_template_issue_id_stable_across_command_edits( hass: HomeAssistant, issue_registry: ir.IssueRegistry, + edited_command: str, ) -> None: - """Editing the command to remove shell features clears the original issue.""" + """Editing the command to remove templated shell features clears the original issue.""" # A shell feature creates the issue for this entity. render_template_args(hass, "echo {{ 'a|b' }}", "sensor", "Test") assert [iid for (dom, iid) in issue_registry.issues if dom == DOMAIN] - # The same entity with an edited, safe command clears it; a command-derived + # The same entity with an edited command clears it; a command-derived # id would instead leave the original issue behind. - render_template_args(hass, "echo {{ 'clean' }}", "sensor", "Test") + render_template_args(hass, edited_command, "sensor", "Test") assert not [iid for (dom, iid) in issue_registry.issues if dom == DOMAIN]