Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Simplepush notification service."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from simplepush import BadRequest, UnknownError, send
9 
11  ATTR_DATA,
12  ATTR_TITLE,
13  ATTR_TITLE_DEFAULT,
14  BaseNotificationService,
15 )
16 from homeassistant.const import CONF_EVENT, CONF_PASSWORD
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
19 
20 from .const import ATTR_ATTACHMENTS, ATTR_EVENT, CONF_DEVICE_KEY, CONF_SALT
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 
26  hass: HomeAssistant,
27  config: ConfigType,
28  discovery_info: DiscoveryInfoType | None = None,
29 ) -> SimplePushNotificationService | None:
30  """Get the Simplepush notification service."""
31  if discovery_info:
32  return SimplePushNotificationService(discovery_info)
33  return None
34 
35 
36 class SimplePushNotificationService(BaseNotificationService):
37  """Implementation of the notification service for Simplepush."""
38 
39  def __init__(self, config: dict[str, Any]) -> None:
40  """Initialize the Simplepush notification service."""
41  self._device_key: str = config[CONF_DEVICE_KEY]
42  self._event: str | None = config.get(CONF_EVENT)
43  self._password: str | None = config.get(CONF_PASSWORD)
44  self._salt: str | None = config.get(CONF_SALT)
45 
46  def send_message(self, message: str, **kwargs: Any) -> None:
47  """Send a message to a Simplepush user."""
48  title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
49 
50  attachments = None
51  # event can now be passed in the service data
52  event = None
53  if data := kwargs.get(ATTR_DATA):
54  event = data.get(ATTR_EVENT)
55 
56  attachments_data = data.get(ATTR_ATTACHMENTS)
57  if isinstance(attachments_data, list):
58  attachments = []
59  for attachment in attachments_data:
60  if not (
61  isinstance(attachment, dict)
62  and (
63  "image" in attachment
64  or "video" in attachment
65  or ("video" in attachment and "thumbnail" in attachment)
66  )
67  ):
68  _LOGGER.error("Attachment format is incorrect")
69  return
70 
71  if "video" in attachment and "thumbnail" in attachment:
72  attachments.append(attachment)
73  elif "video" in attachment:
74  attachments.append(attachment["video"])
75  elif "image" in attachment:
76  attachments.append(attachment["image"])
77 
78  # use event from config until YAML config is removed
79  event = event or self._event
80 
81  try:
82  if self._password:
83  send(
84  key=self._device_key,
85  password=self._password,
86  salt=self._salt,
87  title=title,
88  message=message,
89  attachments=attachments,
90  event=event,
91  )
92  else:
93  send(
94  key=self._device_key,
95  title=title,
96  message=message,
97  attachments=attachments,
98  event=event,
99  )
100 
101  except BadRequest:
102  _LOGGER.error("Bad request. Title or message are too long")
103  except UnknownError:
104  _LOGGER.error("Failed to send the notification")
SimplePushNotificationService|None async_get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:29