core/homeassistant/components/notify/syslog.py

97 lines
2.5 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
2016-09-02 04:28:33 +00:00
import voluptuous as vol
2015-06-04 07:44:28 +00:00
from homeassistant.components.notify import (
2016-09-02 04:28:33 +00:00
ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, BaseNotificationService)
_LOGGER = logging.getLogger(__name__)
2016-09-02 04:28:33 +00:00
CONF_FACILITY = 'facility'
CONF_OPTION = 'option'
CONF_PRIORITY = 'priority'
SYSLOG_FACILITY = {
'kernel': 'LOG_KERN',
'user': 'LOG_USER',
'mail': 'LOG_MAIL',
'daemon': 'LOG_DAEMON',
'auth': 'LOG_KERN',
'LPR': 'LOG_LPR',
'news': 'LOG_NEWS',
'uucp': 'LOG_UUCP',
'cron': 'LOG_CRON',
'syslog': 'LOG_SYSLOG',
'local0': 'LOG_LOCAL0',
'local1': 'LOG_LOCAL1',
'local2': 'LOG_LOCAL2',
'local3': 'LOG_LOCAL3',
'local4': 'LOG_LOCAL4',
'local5': 'LOG_LOCAL5',
'local6': 'LOG_LOCAL6',
'local7': 'LOG_LOCAL7',
}
SYSLOG_OPTION = {
'pid': 'LOG_PID',
'cons': 'LOG_CONS',
'ndelay': 'LOG_NDELAY',
'nowait': 'LOG_NOWAIT',
'perror': 'LOG_PERROR',
}
SYSLOG_PRIORITY = {
5: 'LOG_EMERG',
4: 'LOG_ALERT',
3: 'LOG_CRIT',
2: 'LOG_ERR',
1: 'LOG_WARNING',
0: 'LOG_NOTICE',
-1: 'LOG_INFO',
-2: 'LOG_DEBUG',
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_FACILITY, default='syslog'):
vol.In(SYSLOG_FACILITY.keys()),
vol.Optional(CONF_OPTION, default='pid'): vol.In(SYSLOG_OPTION.keys()),
vol.Optional(CONF_PRIORITY, default=-1): vol.In(SYSLOG_PRIORITY.keys()),
})
2015-06-04 07:44:28 +00:00
def get_service(hass, config, discovery_info=None):
"""Get the syslog notification service."""
import syslog
2016-09-02 04:28:33 +00:00
facility = getattr(syslog, SYSLOG_FACILITY[config.get(CONF_FACILITY)])
option = getattr(syslog, SYSLOG_OPTION[config.get(CONF_OPTION)])
priority = getattr(syslog, SYSLOG_PRIORITY[config.get(CONF_PRIORITY)])
return SyslogNotificationService(facility, option, priority)
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
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()