Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Apprise platform for notify component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import apprise
9 import voluptuous as vol
10 
12  ATTR_TARGET,
13  ATTR_TITLE,
14  ATTR_TITLE_DEFAULT,
15  PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
16  BaseNotificationService,
17 )
18 from homeassistant.const import CONF_URL
19 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 CONF_FILE = "config"
26 
27 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
28  {
29  vol.Optional(CONF_URL): vol.All(cv.ensure_list, [str]),
30  vol.Optional(CONF_FILE): cv.string,
31  }
32 )
33 
34 
36  hass: HomeAssistant,
37  config: ConfigType,
38  discovery_info: DiscoveryInfoType | None = None,
39 ) -> AppriseNotificationService | None:
40  """Get the Apprise notification service."""
41  # Create our Apprise Instance (reference our asset)
42  a_obj = apprise.Apprise()
43 
44  if config.get(CONF_FILE):
45  # Sourced from a Configuration File
46  a_config = apprise.AppriseConfig()
47  if not a_config.add(config[CONF_FILE]):
48  _LOGGER.error("Invalid Apprise config url provided")
49  return None
50 
51  if not a_obj.add(a_config):
52  _LOGGER.error("Invalid Apprise config url provided")
53  return None
54 
55  # Ordered list of URLs
56  if urls := config.get(CONF_URL):
57  for entry in urls:
58  if not a_obj.add(entry):
59  _LOGGER.error("One or more specified Apprise URL(s) are invalid")
60  return None
61 
62  return AppriseNotificationService(a_obj)
63 
64 
65 class AppriseNotificationService(BaseNotificationService):
66  """Implement the notification service for Apprise."""
67 
68  def __init__(self, a_obj: apprise.Apprise) -> None:
69  """Initialize the service."""
70  self.appriseapprise = a_obj
71 
72  def send_message(self, message: str = "", **kwargs: Any) -> None:
73  """Send a message to a specified target.
74 
75  If no target/tags are specified, then services are notified as is
76  However, if any tags are specified, then they will be applied
77  to the notification causing filtering (if set up that way).
78  """
79  targets = kwargs.get(ATTR_TARGET)
80  title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
81  self.appriseapprise.notify(body=message, title=title, tag=targets)
None send_message(self, str message="", **Any kwargs)
Definition: notify.py:72
AppriseNotificationService|None get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:39