core/homeassistant/components/shell_command.py

46 lines
1.3 KiB
Python
Raw Normal View History

2015-10-12 03:11:30 +00:00
"""
homeassistant.components.shell_command
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
from homeassistant.util import slugify
DOMAIN = 'shell_command'
_LOGGER = logging.getLogger(__name__)
def setup(hass, config):
""" Sets up 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
def service_handler(call):
""" Execute a shell command service. """
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)
return True