1 """Notify.Events platform for notify component."""
3 from __future__
import annotations
8 from notify_events
import Message
13 BaseNotificationService,
19 from .const
import DOMAIN
22 ATTR_PRIORITY =
"priority"
25 ATTR_IMAGES =
"images"
28 ATTR_FILE_PATH =
"path"
29 ATTR_FILE_CONTENT =
"content"
30 ATTR_FILE_NAME =
"name"
31 ATTR_FILE_MIME_TYPE =
"mime_type"
33 ATTR_FILE_KIND_FILE =
"file"
34 ATTR_FILE_KIND_IMAGE =
"image"
38 _LOGGER = logging.getLogger(__name__)
44 discovery_info: DiscoveryInfoType |
None =
None,
45 ) -> NotifyEventsNotificationService:
46 """Get the Notify.Events notification service."""
51 """Implement the notification service for Notify.Events."""
54 """Initialize the service."""
58 """Check if a file exists on disk and is in authorized path."""
59 if not self.hass.config.is_allowed_path(filename):
61 return os.path.isfile(filename)
63 def attach_file(self, msg: Message, item: dict, kind: str = ATTR_FILE_KIND_FILE):
64 """Append a file or image to message."""
68 if ATTR_FILE_NAME
in item:
69 file_name = item[ATTR_FILE_NAME]
71 if ATTR_FILE_MIME_TYPE
in item:
72 mime_type = item[ATTR_FILE_MIME_TYPE]
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)
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
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])
90 if kind == ATTR_FILE_KIND_IMAGE:
91 msg.add_image(item[ATTR_FILE_PATH], file_name, mime_type)
93 msg.add_file(item[ATTR_FILE_PATH], file_name, mime_type)
95 _LOGGER.error(
"File does not exist: %s", item[ATTR_FILE_PATH])
98 """Prepare a message to send."""
99 msg = Message(message)
101 if ATTR_TITLE
in data:
102 msg.set_title(data[ATTR_TITLE])
104 if ATTR_LEVEL
in data:
106 msg.set_level(data[ATTR_LEVEL])
107 except ValueError
as error:
108 _LOGGER.warning(
"Setting level error: %s", error)
110 if ATTR_PRIORITY
in data:
112 msg.set_priority(data[ATTR_PRIORITY])
113 except ValueError
as error:
114 _LOGGER.warning(
"Setting priority error: %s", error)
116 if ATTR_IMAGES
in data:
117 for image
in data[ATTR_IMAGES]:
118 self.
attach_fileattach_file(msg, image, ATTR_FILE_KIND_IMAGE)
120 if ATTR_FILES
in data:
121 for file
in data[ATTR_FILES]:
127 """Send a message."""
128 data = kwargs.get(ATTR_DATA)
or {}
129 token = data.get(ATTR_TOKEN, self.
tokentoken)
def __init__(self, token)
Message prepare_message(self, message, data)
def attach_file(self, Message msg, dict item, str kind=ATTR_FILE_KIND_FILE)
bool file_exists(self, filename)
def send_message(self, message, **kwargs)
NotifyEventsNotificationService get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)