diff --git a/.coveragerc b/.coveragerc index 2e992fad9a1..b9ef057bc21 100644 --- a/.coveragerc +++ b/.coveragerc @@ -116,6 +116,7 @@ omit = homeassistant/components/notify/pushetta.py homeassistant/components/notify/pushover.py homeassistant/components/notify/rest.py + homeassistant/components/notify/sendgrid.py homeassistant/components/notify/slack.py homeassistant/components/notify/smtp.py homeassistant/components/notify/syslog.py diff --git a/homeassistant/components/notify/sendgrid.py b/homeassistant/components/notify/sendgrid.py new file mode 100644 index 00000000000..f56bbb59d3e --- /dev/null +++ b/homeassistant/components/notify/sendgrid.py @@ -0,0 +1,51 @@ +""" +homeassistant.components.notify.sendgrid +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +SendGrid notification service. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/notify.sendgrid/ +""" +import logging + +from homeassistant.components.notify import ( + ATTR_TITLE, DOMAIN, BaseNotificationService) +from homeassistant.helpers import validate_config + +REQUIREMENTS = ['sendgrid>=1.6.0,<1.7.0'] +_LOGGER = logging.getLogger(__name__) + + +def get_service(hass, config): + """ Get the SendGrid notification service """ + if not validate_config({DOMAIN: config}, + {DOMAIN: ['api_key', 'sender', 'recipient']}, + _LOGGER): + return None + + api_key = config['api_key'] + sender = config['sender'] + recipient = config['recipient'] + return SendgridNotificationService(api_key, sender, recipient) + + +# pylint: disable=too-few-public-methods +class SendgridNotificationService(BaseNotificationService): + """ Implements the notification service for email via Sendgrid. """ + + def __init__(self, api_key, sender, recipient): + self.api_key = api_key + self.sender = sender + self.recipient = recipient + + from sendgrid import SendGridClient + self._sg = SendGridClient(self.api_key) + + def send_message(self, message='', **kwargs): + """ Send an email to a user via SendGrid. """ + subject = kwargs.get(ATTR_TITLE) + + from sendgrid import Mail + mail = Mail(from_email=self.sender, to=self.recipient, + html=message, text=message, subject=subject) + self._sg.send(mail) diff --git a/requirements_all.txt b/requirements_all.txt index 0337da10a41..7af8c763e8d 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -232,6 +232,9 @@ schiene==0.14 # homeassistant.components.scsgate scsgate==0.1.0 +# homeassistant.components.notify.sendgrid +sendgrid>=1.6.0,<1.7.0 + # homeassistant.components.notify.slack slacker==0.6.8