Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Support for Matrix notifications."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
10  ATTR_DATA,
11  ATTR_MESSAGE,
12  ATTR_TARGET,
13  PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
14  BaseNotificationService,
15 )
16 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
19 
20 from . import RoomID
21 from .const import DOMAIN, SERVICE_SEND_MESSAGE
22 
23 CONF_DEFAULT_ROOM = "default_room"
24 
25 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
26  {vol.Required(CONF_DEFAULT_ROOM): cv.string}
27 )
28 
29 
31  hass: HomeAssistant,
32  config: ConfigType,
33  discovery_info: DiscoveryInfoType | None = None,
34 ) -> MatrixNotificationService:
35  """Get the Matrix notification service."""
36  return MatrixNotificationService(config[CONF_DEFAULT_ROOM])
37 
38 
39 class MatrixNotificationService(BaseNotificationService):
40  """Send notifications to a Matrix room."""
41 
42  def __init__(self, default_room: RoomID) -> None:
43  """Set up the Matrix notification service."""
44  self._default_room_default_room = default_room
45 
46  def send_message(self, message: str = "", **kwargs: Any) -> None:
47  """Send the message to the Matrix server."""
48  target_rooms: list[RoomID] = kwargs.get(ATTR_TARGET) or [self._default_room_default_room]
49  service_data = {ATTR_TARGET: target_rooms, ATTR_MESSAGE: message}
50  if (data := kwargs.get(ATTR_DATA)) is not None:
51  service_data[ATTR_DATA] = data
52  self.hass.services.call(DOMAIN, SERVICE_SEND_MESSAGE, service_data=service_data)
None send_message(self, str message="", **Any kwargs)
Definition: notify.py:46
MatrixNotificationService get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:34