Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Notify.Events platform for notify component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 import os.path
7 
8 from notify_events import Message
9 
11  ATTR_DATA,
12  ATTR_TITLE,
13  BaseNotificationService,
14 )
15 from homeassistant.const import CONF_TOKEN
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
18 
19 from .const import DOMAIN
20 
21 ATTR_LEVEL = "level"
22 ATTR_PRIORITY = "priority"
23 
24 ATTR_FILES = "files"
25 ATTR_IMAGES = "images"
26 
27 ATTR_FILE_URL = "url"
28 ATTR_FILE_PATH = "path"
29 ATTR_FILE_CONTENT = "content"
30 ATTR_FILE_NAME = "name"
31 ATTR_FILE_MIME_TYPE = "mime_type"
32 
33 ATTR_FILE_KIND_FILE = "file"
34 ATTR_FILE_KIND_IMAGE = "image"
35 
36 ATTR_TOKEN = "token"
37 
38 _LOGGER = logging.getLogger(__name__)
39 
40 
42  hass: HomeAssistant,
43  config: ConfigType,
44  discovery_info: DiscoveryInfoType | None = None,
45 ) -> NotifyEventsNotificationService:
46  """Get the Notify.Events notification service."""
47  return NotifyEventsNotificationService(hass.data[DOMAIN][CONF_TOKEN])
48 
49 
50 class NotifyEventsNotificationService(BaseNotificationService):
51  """Implement the notification service for Notify.Events."""
52 
53  def __init__(self, token):
54  """Initialize the service."""
55  self.tokentoken = token
56 
57  def file_exists(self, filename) -> bool:
58  """Check if a file exists on disk and is in authorized path."""
59  if not self.hass.config.is_allowed_path(filename):
60  return False
61  return os.path.isfile(filename)
62 
63  def attach_file(self, msg: Message, item: dict, kind: str = ATTR_FILE_KIND_FILE):
64  """Append a file or image to message."""
65  file_name = None
66  mime_type = None
67 
68  if ATTR_FILE_NAME in item:
69  file_name = item[ATTR_FILE_NAME]
70 
71  if ATTR_FILE_MIME_TYPE in item:
72  mime_type = item[ATTR_FILE_MIME_TYPE]
73 
74  if ATTR_FILE_URL in item:
75  if kind == ATTR_FILE_KIND_IMAGE:
76  msg.add_image_from_url(item[ATTR_FILE_URL], file_name, mime_type)
77  else:
78  msg.add_file_from_url(item[ATTR_FILE_URL], file_name, mime_type)
79  elif ATTR_FILE_CONTENT in item:
80  if kind == ATTR_FILE_KIND_IMAGE:
81  msg.add_image_from_content(
82  item[ATTR_FILE_CONTENT], file_name, mime_type
83  )
84  else:
85  msg.add_file_from_content(item[ATTR_FILE_CONTENT], file_name, mime_type)
86  elif ATTR_FILE_PATH in item:
87  file_exists = self.file_existsfile_exists(item[ATTR_FILE_PATH])
88 
89  if file_exists:
90  if kind == ATTR_FILE_KIND_IMAGE:
91  msg.add_image(item[ATTR_FILE_PATH], file_name, mime_type)
92  else:
93  msg.add_file(item[ATTR_FILE_PATH], file_name, mime_type)
94  else:
95  _LOGGER.error("File does not exist: %s", item[ATTR_FILE_PATH])
96 
97  def prepare_message(self, message, data) -> Message:
98  """Prepare a message to send."""
99  msg = Message(message)
100 
101  if ATTR_TITLE in data:
102  msg.set_title(data[ATTR_TITLE])
103 
104  if ATTR_LEVEL in data:
105  try:
106  msg.set_level(data[ATTR_LEVEL])
107  except ValueError as error:
108  _LOGGER.warning("Setting level error: %s", error)
109 
110  if ATTR_PRIORITY in data:
111  try:
112  msg.set_priority(data[ATTR_PRIORITY])
113  except ValueError as error:
114  _LOGGER.warning("Setting priority error: %s", error)
115 
116  if ATTR_IMAGES in data:
117  for image in data[ATTR_IMAGES]:
118  self.attach_fileattach_file(msg, image, ATTR_FILE_KIND_IMAGE)
119 
120  if ATTR_FILES in data:
121  for file in data[ATTR_FILES]:
122  self.attach_fileattach_file(msg, file)
123 
124  return msg
125 
126  def send_message(self, message, **kwargs):
127  """Send a message."""
128  data = kwargs.get(ATTR_DATA) or {}
129  token = data.get(ATTR_TOKEN, self.tokentoken)
130 
131  msg = self.prepare_messageprepare_message(message, data)
132 
133  msg.send(token)
def attach_file(self, Message msg, dict item, str kind=ATTR_FILE_KIND_FILE)
Definition: notify.py:63
NotifyEventsNotificationService get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:45