Add optional sender name for SendGrid (#21610)

* Set "Home Assistant" as email sender name for SendGrid

* make sender name configurable

* sendgrid tweaks

* fix config
This commit is contained in:
srirams 2019-03-03 15:44:40 -06:00 committed by Rohan Kapoor
parent fa938f5628
commit 818776d2b4

View File

@ -18,33 +18,35 @@ REQUIREMENTS = ['sendgrid==5.6.0']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_SENDER_NAME = 'sender_name'
DEFAULT_SENDER_NAME = 'Home Assistant'
# pylint: disable=no-value-for-parameter # pylint: disable=no-value-for-parameter
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string, vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_SENDER): vol.Email(), vol.Required(CONF_SENDER): vol.Email(),
vol.Required(CONF_RECIPIENT): vol.Email(), vol.Required(CONF_RECIPIENT): vol.Email(),
vol.Optional(CONF_SENDER_NAME, default=DEFAULT_SENDER_NAME): cv.string,
}) })
def get_service(hass, config, discovery_info=None): def get_service(hass, config, discovery_info=None):
"""Get the SendGrid notification service.""" """Get the SendGrid notification service."""
api_key = config.get(CONF_API_KEY) return SendgridNotificationService(config)
sender = config.get(CONF_SENDER)
recipient = config.get(CONF_RECIPIENT)
return SendgridNotificationService(api_key, sender, recipient)
class SendgridNotificationService(BaseNotificationService): class SendgridNotificationService(BaseNotificationService):
"""Implementation the notification service for email via Sendgrid.""" """Implementation the notification service for email via Sendgrid."""
def __init__(self, api_key, sender, recipient): def __init__(self, config):
"""Initialize the service.""" """Initialize the service."""
from sendgrid import SendGridAPIClient from sendgrid import SendGridAPIClient
self.api_key = api_key self.api_key = config[CONF_API_KEY]
self.sender = sender self.sender = config[CONF_SENDER]
self.recipient = recipient self.sender_name = config[CONF_SENDER_NAME]
self.recipient = config[CONF_RECIPIENT]
self._sg = SendGridAPIClient(apikey=self.api_key) self._sg = SendGridAPIClient(apikey=self.api_key)
@ -64,7 +66,8 @@ class SendgridNotificationService(BaseNotificationService):
} }
], ],
"from": { "from": {
"email": self.sender "email": self.sender,
"name": self.sender_name
}, },
"content": [ "content": [
{ {