Skip to content
Merged
Changes from all commits
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
23 changes: 19 additions & 4 deletions homeassistant/components/notify/xmpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,47 @@
_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):
"""Send a message over XMPP."""
import sleekxmpp

Expand All @@ -73,6 +78,10 @@ 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)

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

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

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

SendNotificationBot()