Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Syslog notification service."""
2 
3 from __future__ import annotations
4 
5 import syslog
6 
7 import voluptuous as vol
8 
10  ATTR_TITLE,
11  ATTR_TITLE_DEFAULT,
12  PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
13  BaseNotificationService,
14 )
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
17 
18 CONF_FACILITY = "facility"
19 CONF_OPTION = "option"
20 CONF_PRIORITY = "priority"
21 
22 SYSLOG_FACILITY = {
23  "kernel": "LOG_KERN",
24  "user": "LOG_USER",
25  "mail": "LOG_MAIL",
26  "daemon": "LOG_DAEMON",
27  "auth": "LOG_KERN",
28  "LPR": "LOG_LPR",
29  "news": "LOG_NEWS",
30  "uucp": "LOG_UUCP",
31  "cron": "LOG_CRON",
32  "syslog": "LOG_SYSLOG",
33  "local0": "LOG_LOCAL0",
34  "local1": "LOG_LOCAL1",
35  "local2": "LOG_LOCAL2",
36  "local3": "LOG_LOCAL3",
37  "local4": "LOG_LOCAL4",
38  "local5": "LOG_LOCAL5",
39  "local6": "LOG_LOCAL6",
40  "local7": "LOG_LOCAL7",
41 }
42 
43 SYSLOG_OPTION = {
44  "pid": "LOG_PID",
45  "cons": "LOG_CONS",
46  "ndelay": "LOG_NDELAY",
47  "nowait": "LOG_NOWAIT",
48  "perror": "LOG_PERROR",
49 }
50 
51 SYSLOG_PRIORITY = {
52  5: "LOG_EMERG",
53  4: "LOG_ALERT",
54  3: "LOG_CRIT",
55  2: "LOG_ERR",
56  1: "LOG_WARNING",
57  0: "LOG_NOTICE",
58  -1: "LOG_INFO",
59  -2: "LOG_DEBUG",
60 }
61 
62 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
63  {
64  vol.Optional(CONF_FACILITY, default="syslog"): vol.In(SYSLOG_FACILITY.keys()),
65  vol.Optional(CONF_OPTION, default="pid"): vol.In(SYSLOG_OPTION.keys()),
66  vol.Optional(CONF_PRIORITY, default=-1): vol.In(SYSLOG_PRIORITY.keys()),
67  }
68 )
69 
70 
72  hass: HomeAssistant,
73  config: ConfigType,
74  discovery_info: DiscoveryInfoType | None = None,
75 ) -> SyslogNotificationService:
76  """Get the syslog notification service."""
77 
78  facility = getattr(syslog, SYSLOG_FACILITY[config[CONF_FACILITY]])
79  option = getattr(syslog, SYSLOG_OPTION[config[CONF_OPTION]])
80  priority = getattr(syslog, SYSLOG_PRIORITY[config[CONF_PRIORITY]])
81 
82  return SyslogNotificationService(facility, option, priority)
83 
84 
85 class SyslogNotificationService(BaseNotificationService):
86  """Implement the syslog notification service."""
87 
88  def __init__(self, facility, option, priority):
89  """Initialize the service."""
90  self._facility_facility = facility
91  self._option_option = option
92  self._priority_priority = priority
93 
94  def send_message(self, message="", **kwargs):
95  """Send a message to syslog."""
96 
97  title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
98 
99  syslog.openlog(title, self._option_option, self._facility_facility)
100  syslog.syslog(self._priority_priority, message)
101  syslog.closelog()
def __init__(self, facility, option, priority)
Definition: notify.py:88
SyslogNotificationService get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:75