Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 additions & 1 deletion homeassistant/components/binary_sensor/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@

SCAN_INTERVAL = timedelta(seconds=60)

CONF_COMMAND_TIMEOUT = 'command_timeout'
DEFAULT_TIMEOUT = 15

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_COMMAND): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PAYLOAD_OFF, default=DEFAULT_PAYLOAD_OFF): cv.string,
vol.Optional(CONF_PAYLOAD_ON, default=DEFAULT_PAYLOAD_ON): cv.string,
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(
CONF_COMMAND_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
})


Expand All @@ -43,9 +48,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
payload_on = config.get(CONF_PAYLOAD_ON)
device_class = config.get(CONF_DEVICE_CLASS)
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([CommandBinarySensor(
hass, data, name, device_class, payload_on, payload_off,
Expand Down
13 changes: 10 additions & 3 deletions homeassistant/components/sensor/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@

SCAN_INTERVAL = timedelta(seconds=60)

CONF_COMMAND_TIMEOUT = 'command_timeout'
DEFAULT_TIMEOUT = 15

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,
})


Expand All @@ -41,9 +46,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 +98,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 +142,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
8 changes: 5 additions & 3 deletions tests/components/binary_sensor/test_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def test_setup(self):
config = {'name': 'Test',
'command': 'echo 1',
'payload_on': '1',
'payload_off': '0'}
'payload_off': '0',
'command_timeout': 15
}

devices = []

Expand All @@ -43,7 +45,7 @@ def add_dev_callback(devs, update):

def test_template(self):
"""Test setting the state with a template."""
data = command_line.CommandSensorData(self.hass, 'echo 10')
data = command_line.CommandSensorData(self.hass, 'echo 10', 15)

entity = command_line.CommandBinarySensor(
self.hass, data, 'test', None, '1.0', '0',
Expand All @@ -53,7 +55,7 @@ def test_template(self):

def test_sensor_off(self):
"""Test setting the state with a template."""
data = command_line.CommandSensorData(self.hass, 'echo 0')
data = command_line.CommandSensorData(self.hass, 'echo 0', 15)

entity = command_line.CommandBinarySensor(
self.hass, data, 'test', None, '1', '0', None)
Expand Down
9 changes: 5 additions & 4 deletions tests/components/sensor/test_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def test_setup(self):
"""Test sensor setup."""
config = {'name': 'Test',
'unit_of_measurement': 'in',
'command': 'echo 5'
'command': 'echo 5',
'command_timeout': 15
}
devices = []

Expand All @@ -41,7 +42,7 @@ def add_dev_callback(devs, update):

def test_template(self):
"""Test command sensor with template."""
data = command_line.CommandSensorData(self.hass, 'echo 50')
data = command_line.CommandSensorData(self.hass, 'echo 50', 15)

entity = command_line.CommandSensor(
self.hass, data, 'test', 'in',
Expand All @@ -55,15 +56,15 @@ def test_template_render(self):
self.hass.states.set('sensor.test_state', 'Works')
data = command_line.CommandSensorData(
self.hass,
'echo {{ states.sensor.test_state.state }}'
'echo {{ states.sensor.test_state.state }}', 15
)
data.update()

self.assertEqual("Works", data.value)

def test_bad_command(self):
"""Test bad command."""
data = command_line.CommandSensorData(self.hass, 'asdfasdf')
data = command_line.CommandSensorData(self.hass, 'asdfasdf', 15)
data.update()

self.assertEqual(None, data.value)