Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Support for Join notifications."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from pyjoin import get_devices, send_notification
8 import voluptuous as vol
9 
11  ATTR_DATA,
12  ATTR_TITLE,
13  ATTR_TITLE_DEFAULT,
14  PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
15  BaseNotificationService,
16 )
17 from homeassistant.const import CONF_API_KEY, CONF_DEVICE_ID
18 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 CONF_DEVICE_IDS = "device_ids"
25 CONF_DEVICE_NAMES = "device_names"
26 
27 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
28  {
29  vol.Required(CONF_API_KEY): cv.string,
30  vol.Optional(CONF_DEVICE_ID): cv.string,
31  vol.Optional(CONF_DEVICE_IDS): cv.string,
32  vol.Optional(CONF_DEVICE_NAMES): cv.string,
33  }
34 )
35 
36 
38  hass: HomeAssistant,
39  config: ConfigType,
40  discovery_info: DiscoveryInfoType | None = None,
41 ) -> JoinNotificationService | None:
42  """Get the Join notification service."""
43  api_key = config.get(CONF_API_KEY)
44  device_id = config.get(CONF_DEVICE_ID)
45  device_ids = config.get(CONF_DEVICE_IDS)
46  device_names = config.get(CONF_DEVICE_NAMES)
47  if api_key and not get_devices(api_key):
48  _LOGGER.error("Error connecting to Join. Check the API key")
49  return None
50  if device_id is None and device_ids is None and device_names is None:
51  _LOGGER.error(
52  "No device was provided. Please specify device_id"
53  ", device_ids, or device_names"
54  )
55  return None
56  return JoinNotificationService(api_key, device_id, device_ids, device_names)
57 
58 
59 class JoinNotificationService(BaseNotificationService):
60  """Implement the notification service for Join."""
61 
62  def __init__(self, api_key, device_id, device_ids, device_names):
63  """Initialize the service."""
64  self._api_key_api_key = api_key
65  self._device_id_device_id = device_id
66  self._device_ids_device_ids = device_ids
67  self._device_names_device_names = device_names
68 
69  def send_message(self, message="", **kwargs):
70  """Send a message to a user."""
71  title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
72  data = kwargs.get(ATTR_DATA) or {}
73  send_notification(
74  device_id=self._device_id_device_id,
75  device_ids=self._device_ids_device_ids,
76  device_names=self._device_names_device_names,
77  text=message,
78  title=title,
79  icon=data.get("icon"),
80  smallicon=data.get("smallicon"),
81  image=data.get("image"),
82  sound=data.get("sound"),
83  notification_id=data.get("notification_id"),
84  category=data.get("category"),
85  url=data.get("url"),
86  tts=data.get("tts"),
87  tts_language=data.get("tts_language"),
88  vibration=data.get("vibration"),
89  actions=data.get("actions"),
90  api_key=self._api_key_api_key,
91  )
def __init__(self, api_key, device_id, device_ids, device_names)
Definition: notify.py:62
JoinNotificationService|None get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:41