smtp: make smtp component thread safe

Also fix so it does not require ip, port, username and password when using local smtp server.
Add debug config.

Signed-off-by: Robert Marklund <robbelibobban@gmail.com>
pull/971/head
Robert Marklund 2016-01-23 01:29:34 +01:00
parent 0042e7725d
commit 0e7088ce3b
1 changed files with 36 additions and 30 deletions

View File

@ -21,37 +21,38 @@ def get_service(hass, config):
""" Get the mail notification service. """ """ Get the mail notification service. """
if not validate_config({DOMAIN: config}, if not validate_config({DOMAIN: config},
{DOMAIN: ['server', 'port', 'sender', 'username', {DOMAIN: ['recipient']},
'password', 'recipient']},
_LOGGER): _LOGGER):
return None return None
smtp_server = config['server'] smtp_server = config.get('server', 'localhost')
port = int(config['port']) port = int(config.get('port', '25'))
username = config['username'] username = config.get('username', None)
password = config['password'] password = config.get('password', None)
starttls = int(config['starttls']) starttls = int(config.get('starttls', 0))
debug = config.get('debug', 0)
server = None server = None
try: try:
server = smtplib.SMTP(smtp_server, port) server = smtplib.SMTP(smtp_server, port, timeout=5)
server.set_debuglevel(debug)
server.ehlo() server.ehlo()
if starttls == 1: if starttls == 1:
server.starttls() server.starttls()
server.ehlo() server.ehlo()
if username and password:
try: try:
server.login(username, password) server.login(username, password)
except (smtplib.SMTPException, smtplib.SMTPSenderRefused): except (smtplib.SMTPException, smtplib.SMTPSenderRefused):
_LOGGER.exception("Please check your settings.") _LOGGER.exception("Please check your settings.")
return None return None
except smtplib.socket.gaierror: except smtplib.socket.gaierror:
_LOGGER.exception( _LOGGER.exception(
"SMTP server not found. " "SMTP server not found (%s:%s). "
"Please check the IP address or hostname of your SMTP server.") "Please check the IP address or hostname of your SMTP server.",
smtp_server, port)
return None return None
@ -68,7 +69,7 @@ def get_service(hass, config):
return MailNotificationService( return MailNotificationService(
smtp_server, port, config['sender'], starttls, username, password, smtp_server, port, config['sender'], starttls, username, password,
config['recipient']) config['recipient'], debug)
# pylint: disable=too-few-public-methods, too-many-instance-attributes # pylint: disable=too-few-public-methods, too-many-instance-attributes
@ -77,7 +78,7 @@ class MailNotificationService(BaseNotificationService):
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments
def __init__(self, server, port, sender, starttls, username, def __init__(self, server, port, sender, starttls, username,
password, recipient): password, recipient, debug):
self._server = server self._server = server
self._port = port self._port = port
self._sender = sender self._sender = sender
@ -85,24 +86,26 @@ class MailNotificationService(BaseNotificationService):
self.username = username self.username = username
self.password = password self.password = password
self.recipient = recipient self.recipient = recipient
self.debug = debug
self.tries = 2 self.tries = 2
self.mail = None
self.connect()
def connect(self): def connect(self):
""" Connect/Authenticate to SMTP Server """ """ Connect/Authenticate to SMTP Server """
self.mail = smtplib.SMTP(self._server, self._port) mail = smtplib.SMTP(self._server, self._port, timeout=5)
self.mail.ehlo_or_helo_if_needed() mail.set_debuglevel(self.debug)
mail.ehlo_or_helo_if_needed()
if self.starttls == 1: if self.starttls == 1:
self.mail.starttls() mail.starttls()
self.mail.ehlo() mail.ehlo()
self.mail.login(self.username, self.password) if self.username and self.password:
mail.login(self.username, self.password)
return mail
def send_message(self, message="", **kwargs): def send_message(self, message="", **kwargs):
""" Send a message to a user. """ """ Send a message to a user. """
mail = self.connect()
subject = kwargs.get(ATTR_TITLE) subject = kwargs.get(ATTR_TITLE)
msg = MIMEText(message) msg = MIMEText(message)
@ -113,10 +116,13 @@ class MailNotificationService(BaseNotificationService):
for _ in range(self.tries): for _ in range(self.tries):
try: try:
self.mail.sendmail(self._sender, self.recipient, mail.sendmail(self._sender, self.recipient,
msg.as_string()) msg.as_string())
break break
except smtplib.SMTPException: except smtplib.SMTPException:
_LOGGER.warning('SMTPException sending mail: ' _LOGGER.warning('SMTPException sending mail: '
'retrying connection') 'retrying connection')
self.connect() mail.quit()
mail = self.connect()
mail.quit()