core/homeassistant/components/syslog/notify.py

96 lines
2.4 KiB
Python
Raw Normal View History

"""Syslog notification service."""
2015-06-04 07:44:28 +00:00
import logging
2019-10-13 20:38:42 +00:00
import syslog
2015-06-04 07:44:28 +00:00
2016-09-02 04:28:33 +00:00
import voluptuous as vol
from homeassistant.components.notify import (
2019-07-31 19:25:30 +00:00
ATTR_TITLE,
ATTR_TITLE_DEFAULT,
PLATFORM_SCHEMA,
BaseNotificationService,
)
2016-09-02 04:28:33 +00:00
_LOGGER = logging.getLogger(__name__)
2016-09-02 04:28:33 +00:00
2019-07-31 19:25:30 +00:00
CONF_FACILITY = "facility"
CONF_OPTION = "option"
CONF_PRIORITY = "priority"
2016-09-02 04:28:33 +00:00
SYSLOG_FACILITY = {
2019-07-31 19:25:30 +00:00
"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",
2016-09-02 04:28:33 +00:00
}
SYSLOG_OPTION = {
2019-07-31 19:25:30 +00:00
"pid": "LOG_PID",
"cons": "LOG_CONS",
"ndelay": "LOG_NDELAY",
"nowait": "LOG_NOWAIT",
"perror": "LOG_PERROR",
2016-09-02 04:28:33 +00:00
}
SYSLOG_PRIORITY = {
2019-07-31 19:25:30 +00:00
5: "LOG_EMERG",
4: "LOG_ALERT",
3: "LOG_CRIT",
2: "LOG_ERR",
1: "LOG_WARNING",
0: "LOG_NOTICE",
-1: "LOG_INFO",
-2: "LOG_DEBUG",
2016-09-02 04:28:33 +00:00
}
2019-07-31 19:25:30 +00:00
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()),
}
)
2016-09-02 04:28:33 +00:00
2015-06-04 07:44:28 +00:00
def get_service(hass, config, discovery_info=None):
"""Get the syslog notification service."""
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."""
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()