Do not print warning when command line switch queries off (#38591)

This commit is contained in:
Paulus Schoutsen 2020-08-06 12:36:59 +02:00 committed by GitHub
parent e3e9ad1342
commit 95835326f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View File

@ -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)

View File

@ -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):