core/homeassistant/components/notify/xmpp.py

82 lines
2.6 KiB
Python
Raw Normal View History

2015-05-07 20:31:49 +00:00
"""
homeassistant.components.notify.xmpp
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jabber (XMPP) notification service.
2015-10-13 20:06:29 +00:00
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.xmpp.html
2015-05-07 20:31:49 +00:00
"""
import logging
_LOGGER = logging.getLogger(__name__)
from homeassistant.helpers import validate_config
from homeassistant.components.notify import (
DOMAIN, ATTR_TITLE, BaseNotificationService)
REQUIREMENTS = ['sleekxmpp==1.3.1', 'dnspython3==1.12.0']
2015-07-07 07:01:46 +00:00
2015-05-07 20:31:49 +00:00
def get_service(hass, config):
""" Get the Jabber (XMPP) notification service. """
2015-11-09 06:15:34 +00:00
if not validate_config({DOMAIN: config},
{DOMAIN: ['sender', 'password', 'recipient']},
2015-05-07 20:31:49 +00:00
_LOGGER):
return None
2015-11-09 06:15:34 +00:00
return XmppNotificationService(config['sender'],
config['password'],
config['recipient'])
2015-05-07 20:31:49 +00:00
# pylint: disable=too-few-public-methods
class XmppNotificationService(BaseNotificationService):
""" Implements notification service for Jabber (XMPP). """
def __init__(self, sender, password, recipient):
self._sender = sender
self._password = password
self._recipient = recipient
def send_message(self, message="", **kwargs):
""" Send a message to a user. """
title = kwargs.get(ATTR_TITLE)
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, data)
2015-05-07 20:31:49 +00:00
2015-11-09 06:15:34 +00:00
def send_message(sender, password, recipient, message):
import sleekxmpp
2015-05-07 20:31:49 +00:00
2015-11-09 06:15:34 +00:00
class SendNotificationBot(sleekxmpp.ClientXMPP):
""" 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):
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
2015-11-09 06:15:34 +00:00
self.use_tls = True
self.use_ipv6 = False
self.add_event_handler('failed_auth', self.check_credentials)
self.add_event_handler('session_start', self.start)
self.connect()
self.process()
2015-05-07 20:31:49 +00:00
2015-11-09 06:15:34 +00:00
def start(self, event):
""" Starts the communication and sends the message. """
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. """
self.disconnect()
2015-05-07 20:31:49 +00:00
2015-11-09 06:15:34 +00:00
SendNotificationBot(sender, password, recipient, message)