Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Notification support for Homematic."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
8  ATTR_DATA,
9  PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
10  BaseNotificationService,
11 )
12 from homeassistant.core import HomeAssistant
14 import homeassistant.helpers.template as template_helper
15 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
16 
17 from .const import (
18  ATTR_ADDRESS,
19  ATTR_CHANNEL,
20  ATTR_INTERFACE,
21  ATTR_PARAM,
22  ATTR_VALUE,
23  DOMAIN,
24  SERVICE_SET_DEVICE_VALUE,
25 )
26 
27 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
28  {
29  vol.Required(ATTR_ADDRESS): vol.All(cv.string, vol.Upper),
30  vol.Required(ATTR_CHANNEL): vol.Coerce(int),
31  vol.Required(ATTR_PARAM): vol.All(cv.string, vol.Upper),
32  vol.Required(ATTR_VALUE): cv.match_all,
33  vol.Optional(ATTR_INTERFACE): cv.string,
34  }
35 )
36 
37 
39  hass: HomeAssistant,
40  config: ConfigType,
41  discovery_info: DiscoveryInfoType | None = None,
42 ) -> HomematicNotificationService:
43  """Get the Homematic notification service."""
44  data = {
45  ATTR_ADDRESS: config[ATTR_ADDRESS],
46  ATTR_CHANNEL: config[ATTR_CHANNEL],
47  ATTR_PARAM: config[ATTR_PARAM],
48  ATTR_VALUE: config[ATTR_VALUE],
49  }
50  if ATTR_INTERFACE in config:
51  data[ATTR_INTERFACE] = config[ATTR_INTERFACE]
52 
53  return HomematicNotificationService(hass, data)
54 
55 
56 class HomematicNotificationService(BaseNotificationService):
57  """Implement the notification service for Homematic."""
58 
59  def __init__(self, hass, data):
60  """Initialize the service."""
61  self.hasshass = hass
62  self.datadata = data
63 
64  def send_message(self, message="", **kwargs):
65  """Send a notification to the device."""
66  data = {**self.datadata, **kwargs.get(ATTR_DATA, {})}
67 
68  if data.get(ATTR_VALUE) is not None:
69  templ = template_helper.Template(self.datadata[ATTR_VALUE], self.hasshass)
70  data[ATTR_VALUE] = template_helper.render_complex(templ, None)
71 
72  self.hasshass.services.call(DOMAIN, SERVICE_SET_DEVICE_VALUE, data)
HomematicNotificationService get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:42