mirror of
https://github.com/home-assistant/core.git
synced 2025-12-07 00:18:03 +00:00
59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
"""Support for Tibber notifications."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, cast
|
|
|
|
from tibber import Tibber
|
|
|
|
from homeassistant.components.notify import (
|
|
ATTR_TITLE_DEFAULT,
|
|
NotifyEntity,
|
|
NotifyEntityFeature,
|
|
)
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|
|
|
from .const import DOMAIN
|
|
|
|
if TYPE_CHECKING:
|
|
from . import TibberRuntimeData
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: ConfigEntry,
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
|
) -> None:
|
|
"""Set up the Tibber notification entity."""
|
|
async_add_entities([TibberNotificationEntity(entry.entry_id)])
|
|
|
|
|
|
class TibberNotificationEntity(NotifyEntity):
|
|
"""Implement the notification entity service for Tibber."""
|
|
|
|
_attr_supported_features = NotifyEntityFeature.TITLE
|
|
_attr_name = DOMAIN
|
|
_attr_icon = "mdi:message-flash"
|
|
|
|
def __init__(self, unique_id: str) -> None:
|
|
"""Initialize Tibber notify entity."""
|
|
self._attr_unique_id = unique_id
|
|
|
|
async def async_send_message(self, message: str, title: str | None = None) -> None:
|
|
"""Send a message to Tibber devices."""
|
|
runtime = cast("TibberRuntimeData | None", self.hass.data.get(DOMAIN))
|
|
if runtime is None:
|
|
raise HomeAssistantError("Tibber integration is not initialized")
|
|
tibber_connection: Tibber = runtime.tibber_connection
|
|
try:
|
|
await tibber_connection.send_notification(
|
|
title or ATTR_TITLE_DEFAULT, message
|
|
)
|
|
except TimeoutError as exc:
|
|
raise HomeAssistantError(
|
|
translation_domain=DOMAIN, translation_key="send_message_timeout"
|
|
) from exc
|