1 """Support for the Mailgun mail notifications."""
3 from __future__
import annotations
7 from pymailgunner
import (
9 MailgunCredentialsError,
13 import voluptuous
as vol
19 PLATFORM_SCHEMA
as NOTIFY_PLATFORM_SCHEMA,
20 BaseNotificationService,
26 from .
import CONF_SANDBOX, DOMAIN
as MAILGUN_DOMAIN
28 _LOGGER = logging.getLogger(__name__)
31 ATTR_IMAGES =
"images"
33 DEFAULT_SANDBOX =
False
35 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
36 {vol.Required(CONF_RECIPIENT): vol.Email(), vol.Optional(CONF_SENDER): vol.Email()}
43 discovery_info: DiscoveryInfoType |
None =
None,
44 ) -> MailgunNotificationService |
None:
45 """Get the Mailgun notification service."""
46 data = hass.data[MAILGUN_DOMAIN]
48 data.get(CONF_DOMAIN),
49 data.get(CONF_SANDBOX),
50 data.get(CONF_API_KEY),
51 config.get(CONF_SENDER),
52 config.get(CONF_RECIPIENT),
54 if mailgun_service.connection_is_valid():
55 return mailgun_service
61 """Implement a notification service for the Mailgun mail service."""
63 def __init__(self, domain, sandbox, api_key, sender, recipient):
64 """Initialize the service."""
73 """Initialize the connection to Mailgun."""
76 _LOGGER.debug(
"Mailgun domain: %s", self.
_client_client.domain)
79 self.
_sender_sender = f
"hass@{self._domain}"
82 """Check whether the provided credentials are valid."""
86 except MailgunCredentialsError:
87 _LOGGER.exception(
"Invalid credentials")
89 except MailgunDomainError:
90 _LOGGER.exception(
"Unexpected exception")
95 """Send a mail to the recipient."""
97 subject = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
98 data = kwargs.get(ATTR_DATA)
99 files = data.get(ATTR_IMAGES)
if data
else None
103 if self.
_client_client
is None:
105 resp = self.
_client_client.send_mail(
112 _LOGGER.debug(
"Message sent: %s", resp)
114 _LOGGER.exception(
"Failed to send message")
def connection_is_valid(self)
def send_message(self, message="", **kwargs)
def __init__(self, domain, sandbox, api_key, sender, recipient)
def initialize_client(self)
MailgunNotificationService|None get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)