Config and service validation for shell_command component.

pull/1821/head
Jan Harkes 2016-04-12 00:52:39 -04:00
parent 1deaf2fe8f
commit d90f31bf6e
2 changed files with 28 additions and 26 deletions

View File

@ -7,26 +7,26 @@ https://home-assistant.io/components/shell_command/
import logging import logging
import subprocess import subprocess
from homeassistant.util import slugify import voluptuous as vol
import homeassistant.helpers.config_validation as cv
DOMAIN = 'shell_command' DOMAIN = 'shell_command'
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
cv.slug: cv.string,
}),
}, extra=vol.ALLOW_EXTRA)
SHELL_COMMAND_SCHEMA = vol.Schema({})
def setup(hass, config): def setup(hass, config):
"""Setup the shell_command component.""" """Setup the shell_command component."""
conf = config.get(DOMAIN) conf = config.get(DOMAIN, {})
if not isinstance(conf, dict):
_LOGGER.error('Expected configuration to be a dictionary')
return False
for name in conf.keys():
if name != slugify(name):
_LOGGER.error('Invalid service name: %s. Try %s',
name, slugify(name))
return False
def service_handler(call): def service_handler(call):
"""Execute a shell command service.""" """Execute a shell command service."""
@ -38,6 +38,6 @@ def setup(hass, config):
_LOGGER.exception('Error running command') _LOGGER.exception('Error running command')
for name in conf.keys(): for name in conf.keys():
hass.services.register(DOMAIN, name, service_handler) hass.services.register(DOMAIN, name, service_handler,
schema=SHELL_COMMAND_SCHEMA)
return True return True

View File

@ -5,6 +5,7 @@ import unittest
from unittest.mock import patch from unittest.mock import patch
from subprocess import SubprocessError from subprocess import SubprocessError
from homeassistant.bootstrap import _setup_component
from homeassistant.components import shell_command from homeassistant.components import shell_command
from tests.common import get_test_home_assistant from tests.common import get_test_home_assistant
@ -25,11 +26,11 @@ class TestShellCommand(unittest.TestCase):
"""Test if able to call a configured service.""" """Test if able to call a configured service."""
with tempfile.TemporaryDirectory() as tempdirname: with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, 'called.txt') path = os.path.join(tempdirname, 'called.txt')
self.assertTrue(shell_command.setup(self.hass, { assert _setup_component(self.hass, shell_command.DOMAIN, {
'shell_command': { shell_command.DOMAIN: {
'test_service': "date > {}".format(path) 'test_service': "date > {}".format(path)
} }
})) })
self.hass.services.call('shell_command', 'test_service', self.hass.services.call('shell_command', 'test_service',
blocking=True) blocking=True)
@ -38,16 +39,17 @@ class TestShellCommand(unittest.TestCase):
def test_config_not_dict(self): def test_config_not_dict(self):
"""Test if config is not a dict.""" """Test if config is not a dict."""
self.assertFalse(shell_command.setup(self.hass, { assert not _setup_component(self.hass, shell_command.DOMAIN, {
'shell_command': ['some', 'weird', 'list'] shell_command.DOMAIN: ['some', 'weird', 'list']
})) })
def test_config_not_valid_service_names(self): def test_config_not_valid_service_names(self):
"""Test if config contains invalid service names.""" """Test if config contains invalid service names."""
self.assertFalse(shell_command.setup(self.hass, { assert not _setup_component(self.hass, shell_command.DOMAIN, {
'shell_command': { shell_command.DOMAIN: {
'this is invalid because space': 'touch bla.txt' 'this is invalid because space': 'touch bla.txt'
}})) }
})
@patch('homeassistant.components.shell_command.subprocess.call', @patch('homeassistant.components.shell_command.subprocess.call',
side_effect=SubprocessError) side_effect=SubprocessError)
@ -56,11 +58,11 @@ class TestShellCommand(unittest.TestCase):
"""Test subprocess.""" """Test subprocess."""
with tempfile.TemporaryDirectory() as tempdirname: with tempfile.TemporaryDirectory() as tempdirname:
path = os.path.join(tempdirname, 'called.txt') path = os.path.join(tempdirname, 'called.txt')
self.assertTrue(shell_command.setup(self.hass, { assert _setup_component(self.hass, shell_command.DOMAIN, {
'shell_command': { shell_command.DOMAIN: {
'test_service': "touch {}".format(path) 'test_service': "touch {}".format(path)
} }
})) })
self.hass.services.call('shell_command', 'test_service', self.hass.services.call('shell_command', 'test_service',
blocking=True) blocking=True)