Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions homeassistant/components/sensor/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@

SCAN_INTERVAL = timedelta(seconds=60)

CONF_COMMAND_TIMEOUT ='command_timeout'
DEFAULT_TIMEOUT ='15'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an int not a string.


PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_COMMAND): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_COMMAND_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (81 > 79 characters)

})


Expand All @@ -41,9 +45,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
command = config.get(CONF_COMMAND)
unit = config.get(CONF_UNIT_OF_MEASUREMENT)
value_template = config.get(CONF_VALUE_TEMPLATE)
command_timeout = config.get(CONF_COMMAND_TIMEOUT)
if value_template is not None:
value_template.hass = hass
data = CommandSensorData(hass, command)
data = CommandSensorData(hass, command, command_timeout)

add_devices([CommandSensor(hass, data, name, unit, value_template)], True)

Expand Down Expand Up @@ -92,11 +97,12 @@ def update(self):
class CommandSensorData(object):
"""The class for handling the data retrieval."""

def __init__(self, hass, command):
def __init__(self, hass, command, command_timeout):
"""Initialize the data object."""
self.value = None
self.hass = hass
self.command = command
self.timeout = command_timeout

def update(self):
"""Get the latest data with a shell command."""
Expand Down Expand Up @@ -135,7 +141,7 @@ def update(self):
try:
_LOGGER.info("Running command: %s", command)
return_value = subprocess.check_output(
command, shell=shell, timeout=15)
command, shell=shell, timeout=self.timeout)
self.value = return_value.strip().decode('utf-8')
except subprocess.CalledProcessError:
_LOGGER.error("Command failed: %s", command)
Expand Down