core/homeassistant/components/notify/xmpp.py

115 lines
4.0 KiB
Python
Raw Normal View History

2015-05-07 20:31:49 +00:00
"""
Jabber (XMPP) notification service.
2015-10-13 20:06:29 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 17:33:11 +00:00
https://home-assistant.io/components/notify.xmpp/
2015-05-07 20:31:49 +00:00
"""
import logging
2016-09-02 04:27:28 +00:00
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
2015-05-07 20:31:49 +00:00
from homeassistant.components.notify import (
2016-09-02 04:27:28 +00:00
ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, BaseNotificationService)
from homeassistant.const import (
CONF_PASSWORD, CONF_SENDER, CONF_RECIPIENT, CONF_ROOM)
2015-05-07 20:31:49 +00:00
2017-03-24 20:44:04 +00:00
REQUIREMENTS = ['sleekxmpp==1.3.2',
2016-10-11 06:24:10 +00:00
'dnspython3==1.15.0',
'pyasn1==0.3.7',
'pyasn1-modules==0.1.5']
2015-07-07 07:01:46 +00:00
2016-10-11 06:24:10 +00:00
_LOGGER = logging.getLogger(__name__)
2016-09-02 04:27:28 +00:00
CONF_TLS = 'tls'
CONF_VERIFY = 'verify'
2016-09-02 04:27:28 +00:00
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,
vol.Optional(CONF_ROOM, default=''): cv.string,
2016-09-02 04:27:28 +00:00
})
def get_service(hass, config, discovery_info=None):
2016-03-08 10:46:32 +00:00
"""Get the Jabber (XMPP) notification service."""
return XmppNotificationService(
2017-06-03 20:33:12 +00:00
config.get(CONF_SENDER), config.get(CONF_PASSWORD),
config.get(CONF_RECIPIENT), config.get(CONF_TLS),
config.get(CONF_VERIFY), config.get(CONF_ROOM))
2015-05-07 20:31:49 +00:00
class XmppNotificationService(BaseNotificationService):
2016-03-08 10:46:32 +00:00
"""Implement the notification service for Jabber (XMPP)."""
2015-05-07 20:31:49 +00:00
def __init__(self, sender, password, recipient, tls, verify, room):
2016-03-08 10:46:32 +00:00
"""Initialize the service."""
2015-05-07 20:31:49 +00:00
self._sender = sender
self._password = password
self._recipient = recipient
self._tls = tls
self._verify = verify
self._room = room
2015-05-07 20:31:49 +00:00
def send_message(self, message="", **kwargs):
2016-03-08 10:46:32 +00:00
"""Send a message to a user."""
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
data = '{}: {}'.format(title, message) if title else message
2015-05-07 20:31:49 +00:00
send_message('{}/home-assistant'.format(self._sender),
self._password, self._recipient, self._tls,
self._verify, self._room, data)
2015-05-07 20:31:49 +00:00
def send_message(sender, password, recipient, use_tls,
verify_certificate, room, message):
2016-03-08 10:46:32 +00:00
"""Send a message over XMPP."""
2015-11-09 06:15:34 +00:00
import sleekxmpp
2015-05-07 20:31:49 +00:00
2015-11-09 06:15:34 +00:00
class SendNotificationBot(sleekxmpp.ClientXMPP):
2016-03-08 10:46:32 +00:00
"""Service for sending Jabber (XMPP) messages."""
2015-05-07 20:31:49 +00:00
2015-11-09 06:15:34 +00:00
def __init__(self):
2016-03-08 10:46:32 +00:00
"""Initialize the Jabber Bot."""
2015-11-09 06:15:34 +00:00
super(SendNotificationBot, self).__init__(sender, password)
2015-05-07 20:31:49 +00:00
self.use_tls = use_tls
2015-11-09 06:15:34 +00:00
self.use_ipv6 = False
self.add_event_handler('failed_auth', self.check_credentials)
self.add_event_handler('session_start', self.start)
if room:
self.register_plugin('xep_0045') # MUC
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)
2015-11-09 06:15:34 +00:00
self.process()
2015-05-07 20:31:49 +00:00
2015-11-09 06:15:34 +00:00
def start(self, event):
2016-03-08 10:46:32 +00:00
"""Start the communication and sends the message."""
2015-11-09 06:15:34 +00:00
self.send_presence()
self.get_roster()
if room:
_LOGGER.debug("Joining room %s.", room)
self.plugin['xep_0045'].joinMUC(room, sender, wait=True)
self.send_message(mto=room, mbody=message, mtype='groupchat')
else:
self.send_message(mto=recipient, mbody=message, mtype='chat')
2015-11-09 06:15:34 +00:00
self.disconnect(wait=True)
2015-05-07 20:31:49 +00:00
2015-11-09 06:15:34 +00:00
def check_credentials(self, event):
"""Disconnect from the server if credentials are invalid."""
2015-11-09 06:15:34 +00:00
self.disconnect()
2015-05-07 20:31:49 +00:00
@staticmethod
def discard_ssl_invalid_cert(event):
"""Do nothing if ssl certificate is invalid."""
_LOGGER.info('Ignoring invalid ssl certificate as requested.')
2015-11-09 06:21:02 +00:00
SendNotificationBot()