notify.xmpp - Add support for MUC (#9931)

* Add support for MUC

* Fix two spaces before inline comment
This commit is contained in:
Ludovic 2017-10-18 16:28:37 +02:00 committed by Pascal Vizeli
parent 1bec2c005d
commit 628b9bd8d8

View File

@ -22,6 +22,7 @@ _LOGGER = logging.getLogger(__name__)
CONF_TLS = 'tls' CONF_TLS = 'tls'
CONF_VERIFY = 'verify' CONF_VERIFY = 'verify'
CONF_ROOM = 'room'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_SENDER): cv.string, vol.Required(CONF_SENDER): cv.string,
@ -29,6 +30,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_RECIPIENT): cv.string, vol.Required(CONF_RECIPIENT): cv.string,
vol.Optional(CONF_TLS, default=True): cv.boolean, vol.Optional(CONF_TLS, default=True): cv.boolean,
vol.Optional(CONF_VERIFY, default=True): cv.boolean, vol.Optional(CONF_VERIFY, default=True): cv.boolean,
vol.Optional(CONF_ROOM, default=''): cv.string,
}) })
@ -37,31 +39,33 @@ def get_service(hass, config, discovery_info=None):
return XmppNotificationService( return XmppNotificationService(
config.get(CONF_SENDER), config.get(CONF_PASSWORD), 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)) config.get(CONF_VERIFY), config.get(CONF_ROOM))
class XmppNotificationService(BaseNotificationService): class XmppNotificationService(BaseNotificationService):
"""Implement the notification service for Jabber (XMPP).""" """Implement the notification service for Jabber (XMPP)."""
def __init__(self, sender, password, recipient, tls, verify): def __init__(self, sender, password, recipient, tls, verify, room):
"""Initialize the service.""" """Initialize the service."""
self._sender = sender self._sender = sender
self._password = password self._password = password
self._recipient = recipient self._recipient = recipient
self._tls = tls self._tls = tls
self._verify = verify self._verify = verify
self._room = room
def send_message(self, message="", **kwargs): def send_message(self, message="", **kwargs):
"""Send a message to a user.""" """Send a message to a user."""
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT) title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
data = '{}: {}'.format(title, message) if title else message data = '{}: {}'.format(title, message) if title else message
send_message('{}/home-assistant'.format(self._sender), self._password, send_message('{}/home-assistant'.format(self._sender),
self._recipient, self._tls, self._verify, data) self._password, self._recipient, self._tls,
self._verify, self._room, data)
def send_message(sender, password, recipient, use_tls, def send_message(sender, password, recipient, use_tls,
verify_certificate, message): verify_certificate, room, message):
"""Send a message over XMPP.""" """Send a message over XMPP."""
import sleekxmpp import sleekxmpp
@ -78,6 +82,8 @@ def send_message(sender, password, recipient, use_tls,
self.use_ipv6 = False self.use_ipv6 = False
self.add_event_handler('failed_auth', self.check_credentials) self.add_event_handler('failed_auth', self.check_credentials)
self.add_event_handler('session_start', self.start) self.add_event_handler('session_start', self.start)
if room:
self.register_plugin('xep_0045') # MUC
if not verify_certificate: if not verify_certificate:
self.add_event_handler('ssl_invalid_cert', self.add_event_handler('ssl_invalid_cert',
self.discard_ssl_invalid_cert) self.discard_ssl_invalid_cert)
@ -89,7 +95,13 @@ def send_message(sender, password, recipient, use_tls,
"""Start the communication and sends the message.""" """Start the communication and sends the message."""
self.send_presence() self.send_presence()
self.get_roster() self.get_roster()
self.send_message(mto=recipient, mbody=message, mtype='chat')
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')
self.disconnect(wait=True) self.disconnect(wait=True)
def check_credentials(self, event): def check_credentials(self, event):