Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions homeassistant/components/notify/xmpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,46 @@
_LOGGER = logging.getLogger(__name__)

CONF_TLS = 'tls'
CONF_VERIFY = 'verify'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_SENDER): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_RECIPIENT): cv.string,
vol.Optional(CONF_TLS, default=True): cv.boolean,
vol.Optional(CONF_VERIFY, default=True): cv.boolean,
})


def get_service(hass, config, discovery_info=None):
"""Get the Jabber (XMPP) notification service."""
return XmppNotificationService(
config.get(CONF_SENDER), config.get(CONF_PASSWORD),
config.get(CONF_RECIPIENT), config.get(CONF_TLS))
config.get(CONF_RECIPIENT), config.get(CONF_TLS),
config.get(CONF_VERIFY))


class XmppNotificationService(BaseNotificationService):
"""Implement the notification service for Jabber (XMPP)."""

def __init__(self, sender, password, recipient, tls):
def __init__(self, sender, password, recipient, tls, verify):
"""Initialize the service."""
self._sender = sender
self._password = password
self._recipient = recipient
self._tls = tls
self._verify = verify

def send_message(self, message="", **kwargs):
"""Send a message to a user."""
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
data = '{}: {}'.format(title, message) if title else message

send_message('{}/home-assistant'.format(self._sender), self._password,
self._recipient, self._tls, data)
self._recipient, self._tls, self._verify, data)


def send_message(sender, password, recipient, use_tls, message):
def send_message(sender, password, recipient, use_tls, verify_certificate, message):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (84 > 79 characters)

"""Send a message over XMPP."""
import sleekxmpp

Expand All @@ -73,6 +77,9 @@ def __init__(self):
self.use_ipv6 = False
self.add_event_handler('failed_auth', self.check_credentials)
self.add_event_handler('session_start', self.start)
if not verify_certificate:
self.add_event_handler('ssl_invalid_cert', self.discard_ssl_invalid_cert)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (89 > 79 characters)


self.connect(use_tls=self.use_tls, use_ssl=False)
self.process()

Expand All @@ -87,4 +94,9 @@ def check_credentials(self, event):
"""Disconnect from the server if credentials are invalid."""
self.disconnect()

def discard_ssl_invalid_cert(self, event):
"""Do nothing if ssl certificate is invalid."""
_LOGGER.info('Ignoring invalid ssl certificate as requested.')
return

SendNotificationBot()