Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """LlamaLab Automate notification service."""
2 
3 from __future__ import annotations
4 
5 from http import HTTPStatus
6 import logging
7 
8 import requests
9 import voluptuous as vol
10 
12  ATTR_DATA,
13  PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
14  BaseNotificationService,
15 )
16 from homeassistant.const import CONF_API_KEY, CONF_DEVICE
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers import config_validation as cv
19 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
20 
21 _LOGGER = logging.getLogger(__name__)
22 _RESOURCE = "https://llamalab.com/automate/cloud/message"
23 
24 ATTR_PRIORITY = "priority"
25 
26 CONF_TO = "to"
27 
28 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
29  {
30  vol.Required(CONF_API_KEY): cv.string,
31  vol.Required(CONF_TO): cv.string,
32  vol.Optional(CONF_DEVICE): cv.string,
33  }
34 )
35 
36 
38  hass: HomeAssistant,
39  config: ConfigType,
40  discovery_info: DiscoveryInfoType | None = None,
41 ) -> AutomateNotificationService:
42  """Get the LlamaLab Automate notification service."""
43  secret = config.get(CONF_API_KEY)
44  recipient = config.get(CONF_TO)
45  device = config.get(CONF_DEVICE)
46 
47  return AutomateNotificationService(secret, recipient, device)
48 
49 
50 class AutomateNotificationService(BaseNotificationService):
51  """Implement the notification service for LlamaLab Automate."""
52 
53  def __init__(self, secret, recipient, device=None):
54  """Initialize the service."""
55  self._secret_secret = secret
56  self._recipient_recipient = recipient
57  self._device_device = device
58 
59  def send_message(self, message="", **kwargs):
60  """Send a message to a user."""
61 
62  # Extract params from data dict
63  data = dict(kwargs.get(ATTR_DATA) or {})
64  priority = data.get(ATTR_PRIORITY, "normal").lower()
65 
66  _LOGGER.debug(
67  "Sending to: %s, %s, prio: %s", self._recipient_recipient, str(self._device_device), priority
68  )
69 
70  data = {
71  "secret": self._secret_secret,
72  "to": self._recipient_recipient,
73  "device": self._device_device,
74  "priority": priority,
75  "payload": message,
76  }
77 
78  response = requests.post(_RESOURCE, json=data, timeout=10)
79  if response.status_code != HTTPStatus.OK:
80  _LOGGER.error("Error sending message: %s", response)
AutomateNotificationService get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:41