1 """SendGrid notification service."""
3 from __future__
import annotations
5 from http
import HTTPStatus
8 from sendgrid
import SendGridAPIClient
9 import voluptuous
as vol
14 PLATFORM_SCHEMA
as NOTIFY_PLATFORM_SCHEMA,
15 BaseNotificationService,
21 CONTENT_TYPE_TEXT_PLAIN,
27 _LOGGER = logging.getLogger(__name__)
29 CONF_SENDER_NAME =
"sender_name"
31 DEFAULT_SENDER_NAME =
"Home Assistant"
33 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
35 vol.Required(CONF_API_KEY): cv.string,
36 vol.Required(CONF_SENDER): vol.Email(),
37 vol.Required(CONF_RECIPIENT): vol.Email(),
38 vol.Optional(CONF_SENDER_NAME, default=DEFAULT_SENDER_NAME): cv.string,
46 discovery_info: DiscoveryInfoType |
None =
None,
47 ) -> SendgridNotificationService:
48 """Get the SendGrid notification service."""
53 """Implementation the notification service for email via Sendgrid."""
56 """Initialize the service."""
57 self.
api_keyapi_key = config[CONF_API_KEY]
58 self.
sendersender = config[CONF_SENDER]
65 """Send an email to a user via SendGrid."""
66 subject = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
70 {
"to": [{
"email": self.
recipientrecipient}],
"subject": subject}
73 "content": [{
"type": CONTENT_TYPE_TEXT_PLAIN,
"value": message}],
76 response = self.
_sg_sg.client.mail.send.post(request_body=data)
77 if response.status_code != HTTPStatus.ACCEPTED:
78 _LOGGER.error(
"Unable to send notification")
def __init__(self, config)
def send_message(self, message="", **kwargs)
SendgridNotificationService get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)