Migrate demo notify platform (#115448)

* Migrate demo notify platform

* Update homeassistant/components/demo/notify.py

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>

* Remove no needed tests

* Cleanup redundant attribute assignment

---------

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Jan Bouwhuis
2024-04-13 14:27:07 +02:00
committed by GitHub
parent 5e8b46c670
commit 36bdda5669
3 changed files with 67 additions and 184 deletions

View File

@@ -1,38 +1,44 @@
"""Demo notification service."""
"""Demo notification entity."""
from __future__ import annotations
from typing import Any
from homeassistant.components.notify import BaseNotificationService
from homeassistant.components.notify import DOMAIN, NotifyEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
EVENT_NOTIFY = "notify"
def get_service(
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigType,
discovery_info: DiscoveryInfoType | None = None,
) -> BaseNotificationService:
"""Get the demo notification service."""
return DemoNotificationService(hass)
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 DemoNotificationService(BaseNotificationService):
"""Implement demo notification service."""
class DemoNotifyEntity(NotifyEntity):
"""Implement demo notification platform."""
def __init__(self, hass: HomeAssistant) -> None:
"""Initialize the service."""
self.hass = hass
_attr_has_entity_name = True
_attr_name = None
@property
def targets(self) -> dict[str, str]:
"""Return a dictionary of registered targets."""
return {"test target name": "test target id"}
def __init__(
self,
unique_id: str,
device_name: str,
) -> None:
"""Initialize the Demo button entity."""
self._attr_unique_id = unique_id
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
name=device_name,
)
def send_message(self, message: str = "", **kwargs: Any) -> None:
async def async_send_message(self, message: str) -> None:
"""Send a message to a user."""
kwargs["message"] = message
self.hass.bus.fire(EVENT_NOTIFY, kwargs)
event_notitifcation = {"message": message}
self.hass.bus.async_fire(EVENT_NOTIFY, event_notitifcation)