2016-02-12 10:25:26 +00:00
|
|
|
"""
|
2016-03-08 10:46:32 +00:00
|
|
|
Support for command line notification services.
|
2016-02-12 10:25:26 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/notify.command_line/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
from homeassistant.helpers import validate_config
|
|
|
|
from homeassistant.components.notify import (
|
|
|
|
DOMAIN, BaseNotificationService)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def get_service(hass, config):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Get the Command Line notification service."""
|
2016-02-12 10:25:26 +00:00
|
|
|
if not validate_config({DOMAIN: config},
|
|
|
|
{DOMAIN: ['command']},
|
|
|
|
_LOGGER):
|
|
|
|
return None
|
|
|
|
|
|
|
|
command = config['command']
|
|
|
|
|
|
|
|
return CommandLineNotificationService(command)
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class CommandLineNotificationService(BaseNotificationService):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Implement the notification service for the Command Line service."""
|
2016-02-12 10:25:26 +00:00
|
|
|
|
|
|
|
def __init__(self, command):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Initialize the service."""
|
2016-02-12 10:25:26 +00:00
|
|
|
self.command = command
|
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Send a message to a command line."""
|
2016-02-12 10:25:26 +00:00
|
|
|
try:
|
2016-02-14 21:22:11 +00:00
|
|
|
proc = subprocess.Popen(self.command, universal_newlines=True,
|
|
|
|
stdin=subprocess.PIPE, shell=True)
|
|
|
|
proc.communicate(input=message)
|
|
|
|
if proc.returncode != 0:
|
|
|
|
_LOGGER.error('Command failed: %s', self.command)
|
|
|
|
except subprocess.SubprocessError:
|
|
|
|
_LOGGER.error('Error trying to exec Command: %s', self.command)
|