Refactor syslog component for Windows users

This commit is contained in:
Paulus Schoutsen 2016-01-12 23:59:15 -08:00
parent e6846e7eb9
commit 2a377a6125

View File

@ -7,14 +7,25 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.syslog/ https://home-assistant.io/components/notify.syslog/
""" """
import logging import logging
import syslog
from homeassistant.helpers import validate_config from homeassistant.helpers import validate_config
from homeassistant.components.notify import ( from homeassistant.components.notify import (
DOMAIN, ATTR_TITLE, BaseNotificationService) DOMAIN, ATTR_TITLE, BaseNotificationService)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
FACILITIES = {'kernel': syslog.LOG_KERN,
def get_service(hass, config):
"""Get the syslog notification service."""
if not validate_config({DOMAIN: config},
{DOMAIN: ['facility', 'option', 'priority']},
_LOGGER):
return None
import syslog
_facility = {
'kernel': syslog.LOG_KERN,
'user': syslog.LOG_USER, 'user': syslog.LOG_USER,
'mail': syslog.LOG_MAIL, 'mail': syslog.LOG_MAIL,
'daemon': syslog.LOG_DAEMON, 'daemon': syslog.LOG_DAEMON,
@ -31,35 +42,27 @@ FACILITIES = {'kernel': syslog.LOG_KERN,
'local4': syslog.LOG_LOCAL4, 'local4': syslog.LOG_LOCAL4,
'local5': syslog.LOG_LOCAL5, 'local5': syslog.LOG_LOCAL5,
'local6': syslog.LOG_LOCAL6, 'local6': syslog.LOG_LOCAL6,
'local7': syslog.LOG_LOCAL7} 'local7': syslog.LOG_LOCAL7,
}.get(config['facility'], 40)
OPTIONS = {'pid': syslog.LOG_PID, _option = {
'pid': syslog.LOG_PID,
'cons': syslog.LOG_CONS, 'cons': syslog.LOG_CONS,
'ndelay': syslog.LOG_NDELAY, 'ndelay': syslog.LOG_NDELAY,
'nowait': syslog.LOG_NOWAIT, 'nowait': syslog.LOG_NOWAIT,
'perror': syslog.LOG_PERROR} 'perror': syslog.LOG_PERROR
}.get(config['option'], 10)
PRIORITIES = {5: syslog.LOG_EMERG, _priority = {
5: syslog.LOG_EMERG,
4: syslog.LOG_ALERT, 4: syslog.LOG_ALERT,
3: syslog.LOG_CRIT, 3: syslog.LOG_CRIT,
2: syslog.LOG_ERR, 2: syslog.LOG_ERR,
1: syslog.LOG_WARNING, 1: syslog.LOG_WARNING,
0: syslog.LOG_NOTICE, 0: syslog.LOG_NOTICE,
-1: syslog.LOG_INFO, -1: syslog.LOG_INFO,
-2: syslog.LOG_DEBUG} -2: syslog.LOG_DEBUG
}.get(config['priority'], -1)
def get_service(hass, config):
""" Get the mail notification service. """
if not validate_config({DOMAIN: config},
{DOMAIN: ['facility', 'option', 'priority']},
_LOGGER):
return None
_facility = FACILITIES.get(config['facility'], 40)
_option = OPTIONS.get(config['option'], 10)
_priority = PRIORITIES.get(config['priority'], -1)
return SyslogNotificationService(_facility, _option, _priority) return SyslogNotificationService(_facility, _option, _priority)
@ -76,6 +79,7 @@ class SyslogNotificationService(BaseNotificationService):
def send_message(self, message="", **kwargs): def send_message(self, message="", **kwargs):
""" Send a message to a user. """ """ Send a message to a user. """
import syslog
title = kwargs.get(ATTR_TITLE) title = kwargs.get(ATTR_TITLE)