forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate demo notify platform (home-assistant#115448)
* Migrate demo notify platform * Update homeassistant/components/demo/notify.py Co-authored-by: Paulus Schoutsen <[email protected]> * Remove no needed tests * Cleanup redundant attribute assignment --------- Co-authored-by: Paulus Schoutsen <[email protected]>
- Loading branch information
Showing
3 changed files
with
68 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
|
||
class DemoNotificationService(BaseNotificationService): | ||
"""Implement demo notification service.""" | ||
|
||
def __init__(self, hass: HomeAssistant) -> None: | ||
"""Initialize the service.""" | ||
self.hass = hass | ||
|
||
@property | ||
def targets(self) -> dict[str, str]: | ||
"""Return a dictionary of registered targets.""" | ||
return {"test target name": "test target id"} | ||
|
||
def send_message(self, message: str = "", **kwargs: Any) -> None: | ||
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 | ||
self._attr_device_info = DeviceInfo( | ||
identifiers={(DOMAIN, unique_id)}, | ||
name=device_name, | ||
) | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters