Add missing timeout to command_line platforms: cover, notify, switch (#38497)

* Add missing timeout to command_line platforms: cover, notify, switch

* add timeout test for notify
This commit is contained in:
J. Nick Koston authored and GitHub committed 2020-08-04 17:00:02 -10:00
1 parent dddcb8e299
commit 7b728b17f7
11 files changed
+132 -67

No files matched your search

@@ -8,26 +8,34 @@ from homeassistant.components.notify import PLATFORM_SCHEMA, BaseNotificationSer
from homeassistant.const import CONF_COMMAND, CONF_NAME
import homeassistant.helpers.config_validation as cv
from .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT
_LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Required(CONF_COMMAND): cv.string, vol.Optional(CONF_NAME): cv.string}
{
vol.Required(CONF_COMMAND): cv.string,
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_COMMAND_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
}
)
def get_service(hass, config, discovery_info=None):
"""Get the Command Line notification service."""
command = config[CONF_COMMAND]
timeout = config[CONF_COMMAND_TIMEOUT]
return CommandLineNotificationService(command)
return CommandLineNotificationService(command, timeout)
class CommandLineNotificationService(BaseNotificationService):
"""Implement the notification service for the Command Line service."""
def __init__(self, command):
def __init__(self, command, timeout):
"""Initialize the service."""
self.command = command
self._timeout = timeout
def send_message(self, message="", **kwargs):
"""Send a message to a command line."""
@@ -38,8 +46,10 @@ class CommandLineNotificationService(BaseNotificationService):
stdin=subprocess.PIPE,
shell=True, # nosec # shell by design
)
proc.communicate(input=message)
proc.communicate(input=message, timeout=self._timeout)
if proc.returncode != 0:
_LOGGER.error("Command failed: %s", self.command)
except subprocess.TimeoutExpired:
_LOGGER.error("Timeout for command: %s", self.command)
except subprocess.SubprocessError:
_LOGGER.error("Error trying to exec Command: %s", self.command)
_LOGGER.error("Error trying to exec command: %s", self.command)