2019-02-14 04:35:12 +00:00
|
|
|
"""Support for KNX/IP notification services."""
|
2019-10-22 05:38:21 +00:00
|
|
|
from xknx.devices import Notification as XknxNotification
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2020-08-26 16:03:03 +00:00
|
|
|
from homeassistant.components.notify import BaseNotificationService
|
2017-09-07 07:11:55 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import ATTR_DISCOVER_DEVICES, DATA_KNX
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def async_get_service(hass, config, discovery_info=None):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Get the KNX notification service."""
|
2020-08-26 16:03:03 +00:00
|
|
|
if discovery_info is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
async_get_service_discovery(hass, discovery_info)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_get_service_discovery(hass, discovery_info):
|
|
|
|
"""Set up notifications for KNX platform configured via xknx.yaml."""
|
|
|
|
notification_devices = []
|
|
|
|
for device_name in discovery_info[ATTR_DISCOVER_DEVICES]:
|
|
|
|
device = hass.data[DATA_KNX].xknx.devices[device_name]
|
|
|
|
notification_devices.append(device)
|
2019-07-31 19:25:30 +00:00
|
|
|
return (
|
|
|
|
KNXNotificationService(notification_devices) if notification_devices else None
|
|
|
|
)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KNXNotificationService(BaseNotificationService):
|
|
|
|
"""Implement demo notification service."""
|
|
|
|
|
2020-08-26 16:03:03 +00:00
|
|
|
def __init__(self, devices: XknxNotification):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Initialize the service."""
|
2018-08-26 19:25:39 +00:00
|
|
|
self.devices = devices
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def targets(self):
|
|
|
|
"""Return a dictionary of registered targets."""
|
|
|
|
ret = {}
|
2018-08-26 19:25:39 +00:00
|
|
|
for device in self.devices:
|
2017-09-07 07:11:55 +00:00
|
|
|
ret[device.name] = device.name
|
|
|
|
return ret
|
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def async_send_message(self, message="", **kwargs):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Send a notification to knx bus."""
|
|
|
|
if "target" in kwargs:
|
2018-02-24 18:24:33 +00:00
|
|
|
await self._async_send_to_device(message, kwargs["target"])
|
2017-09-07 07:11:55 +00:00
|
|
|
else:
|
2018-02-24 18:24:33 +00:00
|
|
|
await self._async_send_to_all_devices(message)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def _async_send_to_all_devices(self, message):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Send a notification to knx bus to all connected devices."""
|
2018-08-26 19:25:39 +00:00
|
|
|
for device in self.devices:
|
2018-02-24 18:24:33 +00:00
|
|
|
await device.set(message)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def _async_send_to_device(self, message, names):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Send a notification to knx bus to device with given names."""
|
2018-08-26 19:25:39 +00:00
|
|
|
for device in self.devices:
|
2017-09-07 07:11:55 +00:00
|
|
|
if device.name in names:
|
2018-02-24 18:24:33 +00:00
|
|
|
await device.set(message)
|