Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Clickatell platform for notify component."""
2 
3 from __future__ import annotations
4 
5 from http import HTTPStatus
6 import logging
7 from typing import Any
8 
9 import requests
10 import voluptuous as vol
11 
13  PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
14  BaseNotificationService,
15 )
16 from homeassistant.const import CONF_API_KEY, CONF_RECIPIENT
17 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 DEFAULT_NAME = "clickatell"
24 
25 BASE_API_URL = "https://platform.clickatell.com/messages/http/send"
26 
27 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
28  {vol.Required(CONF_API_KEY): cv.string, vol.Required(CONF_RECIPIENT): cv.string}
29 )
30 
31 
33  hass: HomeAssistant,
34  config: ConfigType,
35  discovery_info: DiscoveryInfoType | None = None,
36 ) -> ClickatellNotificationService:
37  """Get the Clickatell notification service."""
38  return ClickatellNotificationService(config)
39 
40 
41 class ClickatellNotificationService(BaseNotificationService):
42  """Implementation of a notification service for the Clickatell service."""
43 
44  def __init__(self, config: ConfigType) -> None:
45  """Initialize the service."""
46  self.api_key: str = config[CONF_API_KEY]
47  self.recipient: str = config[CONF_RECIPIENT]
48 
49  def send_message(self, message: str = "", **kwargs: Any) -> None:
50  """Send a message to a user."""
51  data = {"apiKey": self.api_key, "to": self.recipient, "content": message}
52 
53  resp = requests.get(BASE_API_URL, params=data, timeout=5)
54  if resp.status_code not in (HTTPStatus.OK, HTTPStatus.ACCEPTED):
55  _LOGGER.error("Error %s : %s", resp.status_code, resp.text)
None send_message(self, str message="", **Any kwargs)
Definition: notify.py:49
ClickatellNotificationService get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:36