Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """SynologyChat platform for notify component."""
2 
3 from __future__ import annotations
4 
5 from http import HTTPStatus
6 import json
7 import logging
8 
9 import requests
10 import voluptuous as vol
11 
13  ATTR_DATA,
14  PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
15  BaseNotificationService,
16 )
17 from homeassistant.const import CONF_RESOURCE, CONF_VERIFY_SSL
18 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
21 
22 ATTR_FILE_URL = "file_url"
23 
24 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
25  {
26  vol.Required(CONF_RESOURCE): cv.url,
27  vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean,
28  }
29 )
30 
31 _LOGGER = logging.getLogger(__name__)
32 
33 
35  hass: HomeAssistant,
36  config: ConfigType,
37  discovery_info: DiscoveryInfoType | None = None,
38 ) -> SynologyChatNotificationService:
39  """Get the Synology Chat notification service."""
40  resource = config.get(CONF_RESOURCE)
41  verify_ssl = config.get(CONF_VERIFY_SSL)
42 
43  return SynologyChatNotificationService(resource, verify_ssl)
44 
45 
46 class SynologyChatNotificationService(BaseNotificationService):
47  """Implementation of a notification service for Synology Chat."""
48 
49  def __init__(self, resource, verify_ssl):
50  """Initialize the service."""
51  self._resource_resource = resource
52  self._verify_ssl_verify_ssl = verify_ssl
53 
54  def send_message(self, message="", **kwargs):
55  """Send a message to a user."""
56  data = {"text": message}
57 
58  extended_data = kwargs.get(ATTR_DATA)
59  file_url = extended_data.get(ATTR_FILE_URL) if extended_data else None
60 
61  if file_url:
62  data["file_url"] = file_url
63 
64  to_send = f"payload={json.dumps(data)}"
65 
66  response = requests.post(
67  self._resource_resource, data=to_send, timeout=10, verify=self._verify_ssl_verify_ssl
68  )
69 
70  if response.status_code not in (HTTPStatus.OK, HTTPStatus.CREATED):
71  _LOGGER.exception(
72  "Error sending message. Response %d: %s:",
73  response.status_code,
74  response.reason,
75  )
SynologyChatNotificationService get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:38