2019-02-14 04:35:12 +00:00
|
|
|
"""Support for KNX/IP notification services."""
|
2021-03-18 12:07:04 +00:00
|
|
|
from __future__ import annotations
|
2020-08-30 18:13:47 +00:00
|
|
|
|
2021-03-19 09:16:27 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2021-05-16 10:07:44 +00:00
|
|
|
from xknx import XKNX
|
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
|
2021-05-16 10:07:44 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2021-03-27 21:20:11 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2021-05-16 10:07:44 +00:00
|
|
|
from .const import DOMAIN, KNX_ADDRESS
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2021-03-19 09:16:27 +00:00
|
|
|
async def async_get_service(
|
2021-03-27 21:20:11 +00:00
|
|
|
hass: HomeAssistant,
|
2021-03-19 09:16:27 +00:00
|
|
|
config: ConfigType,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> KNXNotificationService | None:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Get the KNX notification service."""
|
2021-05-16 10:07:44 +00:00
|
|
|
if not discovery_info or not discovery_info["platform_config"]:
|
|
|
|
return None
|
|
|
|
|
|
|
|
platform_config = discovery_info["platform_config"]
|
|
|
|
xknx: XKNX = hass.data[DOMAIN].xknx
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
notification_devices = []
|
2021-05-16 10:07:44 +00:00
|
|
|
for device_config in platform_config:
|
|
|
|
notification_devices.append(
|
|
|
|
XknxNotification(
|
|
|
|
xknx,
|
|
|
|
name=device_config[CONF_NAME],
|
|
|
|
group_address=device_config[KNX_ADDRESS],
|
|
|
|
)
|
|
|
|
)
|
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."""
|
|
|
|
|
2021-03-19 09:16:27 +00:00
|
|
|
def __init__(self, devices: list[XknxNotification]) -> None:
|
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
|
2021-03-19 09:16:27 +00:00
|
|
|
def targets(self) -> dict[str, str]:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""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
|
|
|
|
|
2021-03-19 09:16:27 +00:00
|
|
|
async def async_send_message(self, message: str = "", **kwargs: Any) -> None:
|
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
|
|
|
|
2021-03-19 09:16:27 +00:00
|
|
|
async def _async_send_to_all_devices(self, message: str) -> None:
|
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
|
|
|
|
2021-03-19 09:16:27 +00:00
|
|
|
async def _async_send_to_device(self, message: str, names: str) -> None:
|
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)
|