core/homeassistant/components/notify/xmpp.py

91 lines
3.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
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',
2017-03-13 20:01:45 +00:00
'pyasn1==0.2.3',
'pyasn1-modules==0.0.9']
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'
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,
})
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))
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):
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
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
2017-06-03 20:33:12 +00:00
send_message('{}/home-assistant'.format(self._sender), self._password,
self._recipient, self._tls, data)
2015-05-07 20:31:49 +00:00
def send_message(sender, password, recipient, use_tls, 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
2015-11-09 06:15:34 +00:00
logging.basicConfig(level=logging.ERROR)
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)
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()
self.send_message(mto=recipient, mbody=message, mtype='chat')
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
2015-11-09 06:21:02 +00:00
SendNotificationBot()