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
10 changed files with 115 additions and 117 deletions

View File

@@ -28,7 +28,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import dt as dt_util, slugify
from .const import CONF_COMMAND_TIMEOUT, LOGGER
from .utils import call_shell_with_timeout, check_output_or_log
from .utils import async_call_shell_with_timeout, async_check_output_or_log
SCAN_INTERVAL = timedelta(seconds=30)
@@ -121,28 +121,26 @@ class CommandSwitch(ManualTriggerEntity, SwitchEntity):
"""Execute the actual commands."""
LOGGER.info("Running command: %s", command)
success = (
await self.hass.async_add_executor_job(
call_shell_with_timeout, command, self._timeout
)
== 0
)
success = await async_call_shell_with_timeout(command, self._timeout) == 0
if not success:
LOGGER.error("Command failed: %s", command)
return success
def _query_state_value(self, command: str) -> str | None:
async def _async_query_state_value(self, command: str) -> str | None:
"""Execute state command for return value."""
LOGGER.info("Running state value command: %s", command)
return check_output_or_log(command, self._timeout)
return await async_check_output_or_log(command, self._timeout)
def _query_state_code(self, command: str) -> bool:
async def _async_query_state_code(self, command: str) -> bool:
"""Execute state command for return code."""
LOGGER.info("Running state code command: %s", command)
return (
call_shell_with_timeout(command, self._timeout, log_return_code=False) == 0
await async_call_shell_with_timeout(
command, self._timeout, log_return_code=False
)
== 0
)
@property
@@ -150,12 +148,12 @@ class CommandSwitch(ManualTriggerEntity, SwitchEntity):
"""Return true if we do optimistic updates."""
return self._command_state is None
def _query_state(self) -> str | int | None:
async def _async_query_state(self) -> str | int | None:
"""Query for state."""
if self._command_state:
if self._value_template:
return self._query_state_value(self._command_state)
return self._query_state_code(self._command_state)
return await self._async_query_state_value(self._command_state)
return await self._async_query_state_code(self._command_state)
if TYPE_CHECKING:
return None
@@ -177,7 +175,7 @@ class CommandSwitch(ManualTriggerEntity, SwitchEntity):
async def _async_update(self) -> None:
"""Update device state."""
if self._command_state:
payload = str(await self.hass.async_add_executor_job(self._query_state))
payload = str(await self._async_query_state())
value = None
if self._value_template:
value = self._value_template.async_render_with_possible_json_value(