2024-04-24 05:51:02 +00:00
|
|
|
"""Support for KNX/IP notifications."""
|
2024-03-08 14:01:29 +00:00
|
|
|
|
2021-03-18 12:07:04 +00:00
|
|
|
from __future__ import annotations
|
2020-08-30 18:13:47 +00:00
|
|
|
|
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
|
|
|
|
2024-04-24 05:51:02 +00:00
|
|
|
from homeassistant import config_entries
|
2024-10-11 18:19:15 +00:00
|
|
|
from homeassistant.components.notify import NotifyEntity
|
2024-04-24 05:51:02 +00:00
|
|
|
from homeassistant.const import CONF_ENTITY_CATEGORY, CONF_NAME, CONF_TYPE, Platform
|
2021-03-27 21:20:11 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-04-24 05:51:02 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2024-10-11 18:19:15 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2024-08-02 10:53:39 +00:00
|
|
|
from . import KNXModule
|
2024-10-11 18:19:15 +00:00
|
|
|
from .const import KNX_ADDRESS, KNX_MODULE_KEY
|
2024-09-17 08:15:26 +00:00
|
|
|
from .entity import KnxYamlEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2024-04-24 05:51:02 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: config_entries.ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up notify(s) for KNX platform."""
|
2024-09-09 07:01:21 +00:00
|
|
|
knx_module = hass.data[KNX_MODULE_KEY]
|
|
|
|
config: list[ConfigType] = knx_module.config_yaml[Platform.NOTIFY]
|
2024-04-24 05:51:02 +00:00
|
|
|
|
2024-08-02 10:53:39 +00:00
|
|
|
async_add_entities(KNXNotify(knx_module, entity_config) for entity_config in config)
|
2024-04-24 05:51:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _create_notification_instance(xknx: XKNX, config: ConfigType) -> XknxNotification:
|
|
|
|
"""Return a KNX Notification to be used within XKNX."""
|
|
|
|
return XknxNotification(
|
|
|
|
xknx,
|
|
|
|
name=config[CONF_NAME],
|
|
|
|
group_address=config[KNX_ADDRESS],
|
|
|
|
value_type=config[CONF_TYPE],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-08-05 10:34:48 +00:00
|
|
|
class KNXNotify(KnxYamlEntity, NotifyEntity):
|
2024-04-24 05:51:02 +00:00
|
|
|
"""Representation of a KNX notification entity."""
|
|
|
|
|
|
|
|
_device: XknxNotification
|
|
|
|
|
2024-08-02 10:53:39 +00:00
|
|
|
def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
|
2024-04-24 05:51:02 +00:00
|
|
|
"""Initialize a KNX notification."""
|
2024-08-02 10:53:39 +00:00
|
|
|
super().__init__(
|
|
|
|
knx_module=knx_module,
|
|
|
|
device=_create_notification_instance(knx_module.xknx, config),
|
|
|
|
)
|
2024-04-24 05:51:02 +00:00
|
|
|
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
|
|
|
|
self._attr_unique_id = str(self._device.remote_value.group_address)
|
|
|
|
|
2024-05-03 09:17:28 +00:00
|
|
|
async def async_send_message(self, message: str, title: str | None = None) -> None:
|
2024-04-24 05:51:02 +00:00
|
|
|
"""Send a notification to knx bus."""
|
|
|
|
await self._device.set(message)
|