From 95835326f30403f5cbe45e58c5cfc18464477a6d Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 6 Aug 2020 12:36:59 +0200 Subject: [PATCH] Do not print warning when command line switch queries off (#38591) --- homeassistant/components/command_line/__init__.py | 11 ++++++++--- homeassistant/components/command_line/switch.py | 4 +++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/command_line/__init__.py b/homeassistant/components/command_line/__init__.py index 92f219a13ea..4f98818d9b3 100644 --- a/homeassistant/components/command_line/__init__.py +++ b/homeassistant/components/command_line/__init__.py @@ -6,8 +6,12 @@ import subprocess _LOGGER = logging.getLogger(__name__) -def call_shell_with_timeout(command, timeout): - """Run a shell command with a timeout.""" +def call_shell_with_timeout(command, timeout, *, log_return_code=True): + """Run a shell command with a timeout. + + If log_return_code is set to False, it will not print an error if a non-zero + return code is returned. + """ try: _LOGGER.debug("Running command: %s", command) subprocess.check_output( @@ -15,7 +19,8 @@ def call_shell_with_timeout(command, timeout): ) return 0 except subprocess.CalledProcessError as proc_exception: - _LOGGER.error("Command failed: %s", command) + if log_return_code: + _LOGGER.error("Command failed: %s", command) return proc_exception.returncode except subprocess.TimeoutExpired: _LOGGER.error("Timeout for command: %s", command) diff --git a/homeassistant/components/command_line/switch.py b/homeassistant/components/command_line/switch.py index 50cda31a537..804e3c6a4d5 100644 --- a/homeassistant/components/command_line/switch.py +++ b/homeassistant/components/command_line/switch.py @@ -114,7 +114,9 @@ class CommandSwitch(SwitchEntity): def _query_state_code(self, command): """Execute state command for return code.""" _LOGGER.info("Running state code command: %s", command) - return call_shell_with_timeout(command, self._timeout) == 0 + return ( + call_shell_with_timeout(command, self._timeout, log_return_code=False) == 0 + ) @property def should_poll(self):