Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Pushover platform for notify component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from pushover_complete import BadAPIRequestError, PushoverAPI
9 
11  ATTR_DATA,
12  ATTR_TARGET,
13  ATTR_TITLE,
14  ATTR_TITLE_DEFAULT,
15  BaseNotificationService,
16 )
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import HomeAssistantError
19 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
20 
21 from .const import (
22  ATTR_ATTACHMENT,
23  ATTR_CALLBACK_URL,
24  ATTR_EXPIRE,
25  ATTR_HTML,
26  ATTR_PRIORITY,
27  ATTR_RETRY,
28  ATTR_SOUND,
29  ATTR_TIMESTAMP,
30  ATTR_URL,
31  ATTR_URL_TITLE,
32  CONF_USER_KEY,
33  DOMAIN,
34 )
35 
36 _LOGGER = logging.getLogger(__name__)
37 
38 
40  hass: HomeAssistant,
41  config: ConfigType,
42  discovery_info: DiscoveryInfoType | None = None,
43 ) -> PushoverNotificationService | None:
44  """Get the Pushover notification service."""
45  if discovery_info is None:
46  return None
47  pushover_api: PushoverAPI = hass.data[DOMAIN][discovery_info["entry_id"]]
49  hass, pushover_api, discovery_info[CONF_USER_KEY]
50  )
51 
52 
53 class PushoverNotificationService(BaseNotificationService):
54  """Implement the notification service for Pushover."""
55 
56  def __init__(
57  self, hass: HomeAssistant, pushover: PushoverAPI, user_key: str
58  ) -> None:
59  """Initialize the service."""
60  self._hass_hass = hass
61  self._user_key_user_key = user_key
62  self.pushoverpushover = pushover
63 
64  def send_message(self, message: str = "", **kwargs: Any) -> None:
65  """Send a message to a user."""
66 
67  # Extract params from data dict
68  title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
69  data = dict(kwargs.get(ATTR_DATA) or {})
70  url = data.get(ATTR_URL)
71  url_title = data.get(ATTR_URL_TITLE)
72  priority = data.get(ATTR_PRIORITY)
73  retry = data.get(ATTR_RETRY)
74  expire = data.get(ATTR_EXPIRE)
75  callback_url = data.get(ATTR_CALLBACK_URL)
76  timestamp = data.get(ATTR_TIMESTAMP)
77  sound = data.get(ATTR_SOUND)
78  html = 1 if data.get(ATTR_HTML, False) else 0
79 
80  # Check for attachment
81  if (image := data.get(ATTR_ATTACHMENT)) is not None:
82  # Only allow attachments from whitelisted paths, check valid path
83  if self._hass_hass.config.is_allowed_path(data[ATTR_ATTACHMENT]):
84  # try to open it as a normal file.
85  try:
86  # pylint: disable-next=consider-using-with
87  file_handle = open(data[ATTR_ATTACHMENT], "rb")
88  # Replace the attachment identifier with file object.
89  image = file_handle
90  except OSError as ex_val:
91  _LOGGER.error(ex_val)
92  # Remove attachment key to send without attachment.
93  image = None
94  else:
95  _LOGGER.error("Path is not whitelisted")
96  # Remove attachment key to send without attachment.
97  image = None
98 
99  try:
100  self.pushoverpushover.send_message(
101  self._user_key_user_key,
102  message,
103  ",".join(kwargs.get(ATTR_TARGET, [])),
104  title,
105  url,
106  url_title,
107  image,
108  priority,
109  retry,
110  expire,
111  callback_url,
112  timestamp,
113  sound,
114  html,
115  )
116  except BadAPIRequestError as err:
117  raise HomeAssistantError(str(err)) from err
None __init__(self, HomeAssistant hass, PushoverAPI pushover, str user_key)
Definition: notify.py:58
None send_message(self, str message="", **Any kwargs)
Definition: notify.py:64
None open(self, **Any kwargs)
Definition: lock.py:86
PushoverNotificationService|None async_get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:43