Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Prowl notification service."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from http import HTTPStatus
7 import logging
8 
9 import voluptuous as vol
10 
12  ATTR_DATA,
13  ATTR_TITLE,
14  ATTR_TITLE_DEFAULT,
15  PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
16  BaseNotificationService,
17 )
18 from homeassistant.const import CONF_API_KEY
19 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.aiohttp_client import async_get_clientsession
22 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
23 
24 _LOGGER = logging.getLogger(__name__)
25 _RESOURCE = "https://api.prowlapp.com/publicapi/"
26 
27 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend({vol.Required(CONF_API_KEY): cv.string})
28 
29 
31  hass: HomeAssistant,
32  config: ConfigType,
33  discovery_info: DiscoveryInfoType | None = None,
34 ) -> ProwlNotificationService:
35  """Get the Prowl notification service."""
36  return ProwlNotificationService(hass, config[CONF_API_KEY])
37 
38 
39 class ProwlNotificationService(BaseNotificationService):
40  """Implement the notification service for Prowl."""
41 
42  def __init__(self, hass, api_key):
43  """Initialize the service."""
44  self._hass_hass = hass
45  self._api_key_api_key = api_key
46 
47  async def async_send_message(self, message, **kwargs):
48  """Send the message to the user."""
49  response = None
50  session = None
51  url = f"{_RESOURCE}add"
52  data = kwargs.get(ATTR_DATA)
53  payload = {
54  "apikey": self._api_key_api_key,
55  "application": "Home-Assistant",
56  "event": kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT),
57  "description": message,
58  "priority": data["priority"] if data and "priority" in data else 0,
59  }
60  if data and data.get("url"):
61  payload["url"] = data["url"]
62 
63  _LOGGER.debug("Attempting call Prowl service at %s", url)
64  session = async_get_clientsession(self._hass_hass)
65 
66  try:
67  async with asyncio.timeout(10):
68  response = await session.post(url, data=payload)
69  result = await response.text()
70 
71  if response.status != HTTPStatus.OK or "error" in result:
72  _LOGGER.error(
73  "Prowl service returned http status %d, response %s",
74  response.status,
75  result,
76  )
77  except TimeoutError:
78  _LOGGER.error("Timeout accessing Prowl at %s", url)
ProwlNotificationService async_get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:34
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)