Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Cisco Webex notify component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 import voluptuous as vol
8 from webexpythonsdk import ApiError, WebexAPI, exceptions
9 
11  ATTR_TITLE,
12  PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
13  BaseNotificationService,
14 )
15 from homeassistant.const import CONF_TOKEN
16 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 CONF_ROOM_ID = "room_id"
23 
24 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
25  {vol.Required(CONF_TOKEN): cv.string, vol.Required(CONF_ROOM_ID): cv.string}
26 )
27 
28 
30  hass: HomeAssistant,
31  config: ConfigType,
32  discovery_info: DiscoveryInfoType | None = None,
33 ) -> CiscoWebexNotificationService | None:
34  """Get the Cisco Webex notification service."""
35  client = WebexAPI(access_token=config[CONF_TOKEN])
36  try:
37  # Validate the token & room_id
38  client.rooms.get(config[CONF_ROOM_ID])
39  except exceptions.ApiError as error:
40  _LOGGER.error(error)
41  return None
42 
43  return CiscoWebexNotificationService(client, config[CONF_ROOM_ID])
44 
45 
46 class CiscoWebexNotificationService(BaseNotificationService):
47  """The Cisco Webex Notification Service."""
48 
49  def __init__(self, client, room):
50  """Initialize the service."""
51  self.roomroom = room
52  self.clientclient = client
53 
54  def send_message(self, message="", **kwargs):
55  """Send a message to a user."""
56 
57  title = ""
58  if kwargs.get(ATTR_TITLE) is not None:
59  title = f"{kwargs.get(ATTR_TITLE)}<br>"
60 
61  try:
62  self.clientclient.messages.create(roomId=self.roomroom, html=f"{title}{message}")
63  except ApiError as api_error:
64  _LOGGER.error(
65  "Could not send Cisco Webex notification. Error: %s", api_error
66  )
CiscoWebexNotificationService|None get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:33