Convert command_line to use asyncio for subprocesses (#111927)

* Convert command_line to use asyncio for subprocesses

* fixes

* fix

* fixes

* more test fixes

* more fixes

* fixes

* preen
This commit is contained in:
J. Nick Koston 2024-03-01 18:15:10 -10:00 committed by GitHub
parent 5f65315e86
commit c0f7ade92b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 115 additions and 117 deletions

View file

@ -5,7 +5,6 @@ import asyncio
from datetime import timedelta
import json
import os
import subprocess
import tempfile
from unittest.mock import patch
@ -32,6 +31,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
import homeassistant.util.dt as dt_util
from . import mock_asyncio_subprocess_run
from tests.common import async_fire_time_changed
@ -374,13 +375,7 @@ async def test_switch_command_state_code_exceptions(
) -> None:
"""Test that switch state code exceptions are handled correctly."""
with patch(
"homeassistant.components.command_line.utils.subprocess.check_output",
side_effect=[
subprocess.TimeoutExpired("cmd", 10),
subprocess.SubprocessError(),
],
) as check_output:
with mock_asyncio_subprocess_run(exception=asyncio.TimeoutError) as run:
await setup.async_setup_component(
hass,
DOMAIN,
@ -401,12 +396,13 @@ async def test_switch_command_state_code_exceptions(
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL)
await hass.async_block_till_done()
assert check_output.called
assert run.called
assert "Timeout for command" in caplog.text
with mock_asyncio_subprocess_run(returncode=127) as run:
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL * 2)
await hass.async_block_till_done()
assert check_output.called
assert run.called
assert "Error trying to exec command" in caplog.text
@ -415,13 +411,7 @@ async def test_switch_command_state_value_exceptions(
) -> None:
"""Test that switch state value exceptions are handled correctly."""
with patch(
"homeassistant.components.command_line.utils.subprocess.check_output",
side_effect=[
subprocess.TimeoutExpired("cmd", 10),
subprocess.SubprocessError(),
],
) as check_output:
with mock_asyncio_subprocess_run(exception=asyncio.TimeoutError) as run:
await setup.async_setup_component(
hass,
DOMAIN,
@ -443,13 +433,14 @@ async def test_switch_command_state_value_exceptions(
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL)
await hass.async_block_till_done()
assert check_output.call_count == 1
assert run.call_count == 1
assert "Timeout for command" in caplog.text
with mock_asyncio_subprocess_run(returncode=127) as run:
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL * 2)
await hass.async_block_till_done()
assert check_output.call_count == 2
assert "Error trying to exec command" in caplog.text
assert run.call_count == 1
assert "Command failed (with return code 127)" in caplog.text
async def test_unique_id(
@ -750,10 +741,7 @@ async def test_availability(
hass.states.async_set("sensor.input1", "off")
await hass.async_block_till_done()
with patch(
"homeassistant.components.command_line.utils.subprocess.check_output",
return_value=b"50\n",
):
with mock_asyncio_subprocess_run(b"50\n"):
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()