2024-04-13 12:27:07 +00:00
|
|
|
"""Demo notification entity."""
|
2024-03-08 13:15:26 +00:00
|
|
|
|
2022-08-26 09:34:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-05-03 09:17:28 +00:00
|
|
|
from homeassistant.components.notify import DOMAIN, NotifyEntity, NotifyEntityFeature
|
2024-04-13 12:27:07 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-08-26 09:34:38 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-04-13 12:27:07 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2015-03-17 05:20:31 +00:00
|
|
|
|
|
|
|
EVENT_NOTIFY = "notify"
|
|
|
|
|
|
|
|
|
2024-04-13 12:27:07 +00:00
|
|
|
async def async_setup_entry(
|
2022-08-26 09:34:38 +00:00
|
|
|
hass: HomeAssistant,
|
2024-04-13 12:27:07 +00:00
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the demo entity platform."""
|
|
|
|
async_add_entities([DemoNotifyEntity(unique_id="notify", device_name="Notifier")])
|
|
|
|
|
|
|
|
|
|
|
|
class DemoNotifyEntity(NotifyEntity):
|
|
|
|
"""Implement demo notification platform."""
|
|
|
|
|
|
|
|
_attr_has_entity_name = True
|
|
|
|
_attr_name = None
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
unique_id: str,
|
|
|
|
device_name: str,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the Demo button entity."""
|
|
|
|
self._attr_unique_id = unique_id
|
2024-05-03 09:17:28 +00:00
|
|
|
self._attr_supported_features = NotifyEntityFeature.TITLE
|
2024-04-13 12:27:07 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, unique_id)},
|
|
|
|
name=device_name,
|
|
|
|
)
|
|
|
|
|
2024-05-03 09:17:28 +00:00
|
|
|
async def async_send_message(self, message: str, title: str | None = None) -> None:
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Send a message to a user."""
|
2024-05-03 09:17:28 +00:00
|
|
|
event_notification = {"message": message}
|
|
|
|
if title is not None:
|
|
|
|
event_notification["title"] = title
|
|
|
|
self.hass.bus.async_fire(EVENT_NOTIFY, event_notification)
|