diff --git a/homeassistant/components/notify/sendgrid.py b/homeassistant/components/notify/sendgrid.py index ac3fc3deaab..6811fdbd55b 100644 --- a/homeassistant/components/notify/sendgrid.py +++ b/homeassistant/components/notify/sendgrid.py @@ -10,7 +10,7 @@ from homeassistant.components.notify import ( ATTR_TITLE, DOMAIN, BaseNotificationService) from homeassistant.helpers import validate_config -REQUIREMENTS = ['sendgrid>=1.6.0,<1.7.0'] +REQUIREMENTS = ['sendgrid==3.0.7'] _LOGGER = logging.getLogger(__name__) @@ -24,27 +24,50 @@ def get_service(hass, config): 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): - """Implement the notification service for email via Sendgrid.""" + """Implementation the notification service for email via Sendgrid.""" def __init__(self, api_key, sender, recipient): """Initialize the service.""" + from sendgrid import SendGridAPIClient + self.api_key = api_key self.sender = sender self.recipient = recipient - from sendgrid import SendGridClient - self._sg = SendGridClient(self.api_key) + self._sg = SendGridAPIClient(apikey=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) + data = { + "personalizations": [ + { + "to": [ + { + "email": self.recipient + } + ], + "subject": subject + } + ], + "from": { + "email": self.sender + }, + "content": [ + { + "type": "text/plain", + "value": message + } + ] + } + + response = self._sg.client.mail.send.post(request_body=data) + if response.status_code is not 202: + _LOGGER.error('Unable to send notification with SendGrid') diff --git a/requirements_all.txt b/requirements_all.txt index 2a230c55d6c..3ec4fc3712f 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -378,7 +378,7 @@ schiene==0.17 scsgate==0.1.0 # homeassistant.components.notify.sendgrid -sendgrid>=1.6.0,<1.7.0 +sendgrid==3.0.7 # homeassistant.components.notify.slack slacker==0.9.24