core/homeassistant/components/notify/syslog.py

88 lines
2.6 KiB
Python
Raw Normal View History

2015-06-04 07:44:28 +00:00
"""
Syslog notification service.
2015-10-13 20:16:26 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 17:33:11 +00:00
https://home-assistant.io/components/notify.syslog/
2015-06-04 07:44:28 +00:00
"""
import logging
from homeassistant.components.notify import (
ATTR_TITLE, ATTR_TITLE_DEFAULT, DOMAIN, BaseNotificationService)
2016-02-19 05:27:50 +00:00
from homeassistant.helpers import validate_config
2015-06-04 07:44:28 +00:00
_LOGGER = logging.getLogger(__name__)
def get_service(hass, config):
"""Get the syslog notification service."""
2015-11-09 06:15:34 +00:00
if not validate_config({DOMAIN: config},
{DOMAIN: ['facility', 'option', 'priority']},
2015-06-04 07:44:28 +00:00
_LOGGER):
return None
import syslog
_facility = {
'kernel': syslog.LOG_KERN,
'user': syslog.LOG_USER,
'mail': syslog.LOG_MAIL,
'daemon': syslog.LOG_DAEMON,
'auth': syslog.LOG_KERN,
'LPR': syslog.LOG_LPR,
'news': syslog.LOG_NEWS,
'uucp': syslog.LOG_UUCP,
'cron': syslog.LOG_CRON,
'syslog': syslog.LOG_SYSLOG,
'local0': syslog.LOG_LOCAL0,
'local1': syslog.LOG_LOCAL1,
'local2': syslog.LOG_LOCAL2,
'local3': syslog.LOG_LOCAL3,
'local4': syslog.LOG_LOCAL4,
'local5': syslog.LOG_LOCAL5,
'local6': syslog.LOG_LOCAL6,
'local7': syslog.LOG_LOCAL7,
}.get(config['facility'], 40)
_option = {
'pid': syslog.LOG_PID,
'cons': syslog.LOG_CONS,
'ndelay': syslog.LOG_NDELAY,
'nowait': syslog.LOG_NOWAIT,
'perror': syslog.LOG_PERROR
}.get(config['option'], 10)
_priority = {
5: syslog.LOG_EMERG,
4: syslog.LOG_ALERT,
3: syslog.LOG_CRIT,
2: syslog.LOG_ERR,
1: syslog.LOG_WARNING,
0: syslog.LOG_NOTICE,
-1: syslog.LOG_INFO,
-2: syslog.LOG_DEBUG
}.get(config['priority'], -1)
2015-06-04 07:44:28 +00:00
return SyslogNotificationService(_facility, _option, _priority)
2015-06-04 07:50:37 +00:00
# pylint: disable=too-few-public-methods
2015-06-04 07:44:28 +00:00
class SyslogNotificationService(BaseNotificationService):
2016-03-08 10:46:32 +00:00
"""Implement the syslog notification service."""
2015-06-04 07:44:28 +00:00
# pylint: disable=too-many-arguments
def __init__(self, facility, option, priority):
2016-03-08 10:46:32 +00:00
"""Initialize the service."""
2015-06-04 07:44:28 +00:00
self._facility = facility
self._option = option
self._priority = priority
def send_message(self, message="", **kwargs):
2016-03-08 10:46:32 +00:00
"""Send a message to a user."""
import syslog
2015-06-04 07:44:28 +00:00
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
2015-06-04 07:44:28 +00:00
syslog.openlog(title, self._option, self._facility)
syslog.syslog(self._priority, message)
syslog.closelog()