From 783abc799611b11b86c93af97ee615d1967cfff7 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Mon, 22 May 2017 15:17:15 +0200 Subject: [PATCH] Make 'sender' as requirement for the config (fixes #7698) (#7706) --- homeassistant/components/notify/smtp.py | 30 ++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/notify/smtp.py b/homeassistant/components/notify/smtp.py index d66d024e111..6489345d91c 100644 --- a/homeassistant/components/notify/smtp.py +++ b/homeassistant/components/notify/smtp.py @@ -12,16 +12,17 @@ from email.mime.image import MIMEImage from email.mime.application import MIMEApplication import email.utils import os + import voluptuous as vol +import homeassistant.helpers.config_validation as cv +import homeassistant.util.dt as dt_util from homeassistant.components.notify import ( ATTR_TITLE, ATTR_TITLE_DEFAULT, ATTR_DATA, PLATFORM_SCHEMA, BaseNotificationService) from homeassistant.const import ( CONF_USERNAME, CONF_PASSWORD, CONF_PORT, CONF_TIMEOUT, CONF_SENDER, CONF_RECIPIENT) -import homeassistant.helpers.config_validation as cv -import homeassistant.util.dt as dt_util _LOGGER = logging.getLogger(__name__) @@ -42,10 +43,10 @@ DEFAULT_STARTTLS = False # pylint: disable=no-value-for-parameter PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_RECIPIENT): vol.All(cv.ensure_list, [vol.Email()]), + vol.Required(CONF_SENDER): vol.Email(), vol.Optional(CONF_SERVER, default=DEFAULT_HOST): cv.string, vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int, - vol.Optional(CONF_SENDER): vol.Email(), vol.Optional(CONF_STARTTLS, default=DEFAULT_STARTTLS): cv.boolean, vol.Optional(CONF_USERNAME): cv.string, vol.Optional(CONF_PASSWORD): cv.string, @@ -75,11 +76,11 @@ def get_service(hass, config, discovery_info=None): class MailNotificationService(BaseNotificationService): - """Implement the notification service for E-Mail messages.""" + """Implement the notification service for E-mail messages.""" def __init__(self, server, port, timeout, sender, starttls, username, password, recipients, sender_name, debug): - """Initialize the service.""" + """Initialize the SMTP service.""" self._server = server self._port = port self._timeout = timeout @@ -142,11 +143,11 @@ class MailNotificationService(BaseNotificationService): if data: if ATTR_HTML in data: - msg = _build_html_msg(message, data[ATTR_HTML], - images=data.get(ATTR_IMAGES)) + msg = _build_html_msg( + message, data[ATTR_HTML], images=data.get(ATTR_IMAGES)) else: - msg = _build_multipart_msg(message, - images=data.get(ATTR_IMAGES)) + msg = _build_multipart_msg( + message, images=data.get(ATTR_IMAGES)) else: msg = _build_text_msg(message) @@ -167,8 +168,7 @@ class MailNotificationService(BaseNotificationService): mail = self.connect() for _ in range(self.tries): try: - mail.sendmail(self._sender, self.recipients, - msg.as_string()) + mail.sendmail(self._sender, self.recipients, msg.as_string()) break except smtplib.SMTPServerDisconnected: _LOGGER.warning( @@ -210,7 +210,7 @@ def _build_multipart_msg(message, images): msg.attach(attachment) attachment.add_header('Content-ID', '<{}>'.format(cid)) except TypeError: - _LOGGER.warning("Attachment %s has an unkown MIME type. " + _LOGGER.warning("Attachment %s has an unknown MIME type. " "Falling back to file", atch_name) attachment = MIMEApplication(file_bytes, Name=atch_name) attachment['Content-Disposition'] = ('attachment; ' @@ -226,8 +226,8 @@ def _build_multipart_msg(message, images): def _build_html_msg(text, html, images): - """Build Multipart message with in-line images and rich html (UTF-8).""" - _LOGGER.debug("Building html rich email") + """Build Multipart message with in-line images and rich HTML (UTF-8).""" + _LOGGER.debug("Building HTML rich email") msg = MIMEMultipart('related') alternative = MIMEMultipart('alternative') alternative.attach(MIMEText(text, _charset='utf-8')) @@ -242,6 +242,6 @@ def _build_html_msg(text, html, images): msg.attach(attachment) attachment.add_header('Content-ID', '<{}>'.format(name)) except FileNotFoundError: - _LOGGER.warning('Attachment %s [#%s] not found. Skipping', + _LOGGER.warning("Attachment %s [#%s] not found. Skipping", atch_name, atch_num) return msg