core/homeassistant/components/shell_command.py

44 lines
1.1 KiB
Python
Raw Normal View History

2015-10-12 03:11:30 +00:00
"""
2015-10-13 18:40:59 +00:00
Exposes regular shell commands as services.
2015-10-12 03:11:30 +00:00
2015-10-13 18:40:59 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/shell_command/
2015-10-12 03:11:30 +00:00
"""
import logging
import subprocess
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
2015-10-12 03:11:30 +00:00
DOMAIN = 'shell_command'
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
cv.slug: cv.string,
}),
}, extra=vol.ALLOW_EXTRA)
2015-10-12 03:11:30 +00:00
SHELL_COMMAND_SCHEMA = vol.Schema({})
2015-10-12 03:11:30 +00:00
def setup(hass, config):
"""Setup the shell_command component."""
conf = config.get(DOMAIN, {})
2015-10-12 03:11:30 +00:00
def service_handler(call):
2016-03-07 17:49:31 +00:00
"""Execute a shell command service."""
2015-10-12 03:11:30 +00:00
try:
subprocess.call(conf[call.service], shell=True,
2015-10-12 03:11:30 +00:00
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
except subprocess.SubprocessError:
_LOGGER.exception('Error running command')
for name in conf.keys():
hass.services.register(DOMAIN, name, service_handler,
schema=SHELL_COMMAND_SCHEMA)
2015-10-12 03:11:30 +00:00
return True