"""
SendGrid notification service.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.sendgrid/
"""
import logging

import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.components.notify import (
    ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, BaseNotificationService)
from homeassistant.const import CONF_API_KEY, CONF_SENDER, CONF_RECIPIENT

REQUIREMENTS = ['sendgrid==3.2.10']
_LOGGER = logging.getLogger(__name__)


PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
    vol.Required(CONF_API_KEY): cv.string,
    vol.Required(CONF_SENDER): vol.Email,
    vol.Required(CONF_RECIPIENT): cv.string,
})


def get_service(hass, config):
    """Get the SendGrid notification service."""
    api_key = config[CONF_API_KEY]
    sender = config[CONF_SENDER]
    recipient = config[CONF_RECIPIENT]

    return SendgridNotificationService(api_key, sender, recipient)


# pylint: disable=too-few-public-methods
class SendgridNotificationService(BaseNotificationService):
    """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

        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, ATTR_TITLE_DEFAULT)

        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')

@@ -208,61 +208,8 @@
 ol%0A%0A
-import homeassistant.helpers.config_validation as cv%0A
 from
@@ -360,16 +360,17 @@
  import 
+(
 CONF_API
@@ -402,16 +402,70 @@
 ECIPIENT
+)%0Aimport homeassistant.helpers.config_validation as cv
 %0A%0AREQUIR
@@ -494,16 +494,17 @@
 .2.10'%5D%0A
+%0A
 _LOGGER 
@@ -534,16 +534,56 @@
 ame__)%0A%0A
+# pylint: disable=no-value-for-parameter
 %0APLATFOR
@@ -701,16 +701,18 @@
 ol.Email
+()
 ,%0A    vo
@@ -739,25 +739,27 @@
 PIENT): 
-cv.string
+vol.Email()
 ,%0A%7D)%0A%0A%0Ad
@@ -853,25 +853,29 @@
 key = config
-%5B
+.get(
 CONF_API_KEY
@@ -874,17 +874,17 @@
 _API_KEY
-%5D
+)
 %0A    sen
@@ -891,25 +891,29 @@
 der = config
-%5B
+.get(
 CONF_SENDER%5D
@@ -911,17 +911,17 @@
 F_SENDER
-%5D
+)
 %0A    rec
@@ -935,17 +935,21 @@
 = config
-%5B
+.get(
 CONF_REC
@@ -954,17 +954,17 @@
 ECIPIENT
-%5D
+)
 %0A%0A    re

Fix email validation (fixes #3138) (#3227)