core/homeassistant/components/notify/xmpp.py

98 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
2015-05-07 20:31:49 +00:00
REQUIREMENTS = ['sleekxmpp==1.3.1',
'dnspython3==1.12.0',
'pyasn1==0.1.9',
'pyasn1-modules==0.0.8']
2015-07-07 07:01:46 +00:00
2016-09-02 04:27:28 +00:00
CONF_SENDER = 'sender'
CONF_RECIPIENT = 'recipient'
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,
})
2015-11-29 21:49:05 +00:00
_LOGGER = logging.getLogger(__name__)
2015-05-07 20:31:49 +00:00
def get_service(hass, config):
2016-03-08 10:46:32 +00:00
"""Get the Jabber (XMPP) notification service."""
return XmppNotificationService(
config.get('sender'),
config.get('password'),
config.get('recipient'),
2016-09-02 04:27:28 +00:00
config.get('tls'))
2015-05-07 20:31:49 +00:00
# pylint: disable=too-few-public-methods
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)
2015-11-09 06:15:34 +00:00
data = "{}: {}".format(title, message) if title else message
2015-05-07 20:31:49 +00:00
2015-11-09 06:15:34 +00:00
send_message(self._sender + '/home-assistant', 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):
2016-03-08 10:46:32 +00:00
""""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()