Config and service validation for shell_command component.
parent
1deaf2fe8f
commit
d90f31bf6e
|
@ -7,26 +7,26 @@ https://home-assistant.io/components/shell_command/
|
|||
import logging
|
||||
import subprocess
|
||||
|
||||
from homeassistant.util import slugify
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
DOMAIN = 'shell_command'
|
||||
|
||||
_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):
|
||||
"""Setup the shell_command component."""
|
||||
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
|
||||
conf = config.get(DOMAIN, {})
|
||||
|
||||
def service_handler(call):
|
||||
"""Execute a shell command service."""
|
||||
|
@ -38,6 +38,6 @@ def setup(hass, config):
|
|||
_LOGGER.exception('Error running command')
|
||||
|
||||
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
|
||||
|
|
|
@ -5,6 +5,7 @@ import unittest
|
|||
from unittest.mock import patch
|
||||
from subprocess import SubprocessError
|
||||
|
||||
from homeassistant.bootstrap import _setup_component
|
||||
from homeassistant.components import shell_command
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
@ -25,11 +26,11 @@ class TestShellCommand(unittest.TestCase):
|
|||
"""Test if able to call a configured service."""
|
||||
with tempfile.TemporaryDirectory() as tempdirname:
|
||||
path = os.path.join(tempdirname, 'called.txt')
|
||||
self.assertTrue(shell_command.setup(self.hass, {
|
||||
'shell_command': {
|
||||
assert _setup_component(self.hass, shell_command.DOMAIN, {
|
||||
shell_command.DOMAIN: {
|
||||
'test_service': "date > {}".format(path)
|
||||
}
|
||||
}))
|
||||
})
|
||||
|
||||
self.hass.services.call('shell_command', 'test_service',
|
||||
blocking=True)
|
||||
|
@ -38,16 +39,17 @@ class TestShellCommand(unittest.TestCase):
|
|||
|
||||
def test_config_not_dict(self):
|
||||
"""Test if config is not a dict."""
|
||||
self.assertFalse(shell_command.setup(self.hass, {
|
||||
'shell_command': ['some', 'weird', 'list']
|
||||
}))
|
||||
assert not _setup_component(self.hass, shell_command.DOMAIN, {
|
||||
shell_command.DOMAIN: ['some', 'weird', 'list']
|
||||
})
|
||||
|
||||
def test_config_not_valid_service_names(self):
|
||||
"""Test if config contains invalid service names."""
|
||||
self.assertFalse(shell_command.setup(self.hass, {
|
||||
'shell_command': {
|
||||
assert not _setup_component(self.hass, shell_command.DOMAIN, {
|
||||
shell_command.DOMAIN: {
|
||||
'this is invalid because space': 'touch bla.txt'
|
||||
}}))
|
||||
}
|
||||
})
|
||||
|
||||
@patch('homeassistant.components.shell_command.subprocess.call',
|
||||
side_effect=SubprocessError)
|
||||
|
@ -56,11 +58,11 @@ class TestShellCommand(unittest.TestCase):
|
|||
"""Test subprocess."""
|
||||
with tempfile.TemporaryDirectory() as tempdirname:
|
||||
path = os.path.join(tempdirname, 'called.txt')
|
||||
self.assertTrue(shell_command.setup(self.hass, {
|
||||
'shell_command': {
|
||||
assert _setup_component(self.hass, shell_command.DOMAIN, {
|
||||
shell_command.DOMAIN: {
|
||||
'test_service': "touch {}".format(path)
|
||||
}
|
||||
}))
|
||||
})
|
||||
|
||||
self.hass.services.call('shell_command', 'test_service',
|
||||
blocking=True)
|
||||
|
|
Loading…
Reference in New Issue