From 0e9685e55f7acc7525ffc3f39a32ed9b460895cd Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 7 May 2015 22:27:03 +0200 Subject: [PATCH 1/5] add requirements for xmpp notifications --- requirements.txt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 3afa9c39a22..b270cdc53e3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -41,11 +41,16 @@ PyISY>=1.0.2 # sensor.systemmonitor psutil>=2.2.1 -#pushover notifications +# pushover notifications python-pushover>=0.2 # Transmission Torrent Client transmissionrpc>=0.11 # OpenWeatherMap Web API -pyowm>=2.2.0 \ No newline at end of file +pyowm>=2.2.0 + +# XMPP Bindings (notify.xmpp) +sleekxmpp>=1.3.1 +pyasn1>=0.1.7 +pyasn1_modules>=0.0.5 From 07b5f3d5977df695bb4f08648d659da4418697c1 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 7 May 2015 22:31:49 +0200 Subject: [PATCH 2/5] initial xmpp nofication platform --- homeassistant/components/notify/xmpp.py | 127 ++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 homeassistant/components/notify/xmpp.py diff --git a/homeassistant/components/notify/xmpp.py b/homeassistant/components/notify/xmpp.py new file mode 100644 index 00000000000..769e9a89844 --- /dev/null +++ b/homeassistant/components/notify/xmpp.py @@ -0,0 +1,127 @@ +""" +homeassistant.components.notify.xmpp +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Jabber (XMPP) notification service. + +Configuration: + +To use the Jabber notifier you will need to add something like the following +to your config/configuration.yaml + +notify: + platform: xmpp + sender: YOUR_JID + password: YOUR_JABBER_ACCOUNT_PASSWORD + recipient: YOUR_RECIPIENT + +VARIABLES: + +sender +*Required +The Jabber ID (JID) that will act as origin of the messages. Add your JID +including the domain, e.g. your_name@jabber.org. + +password +*Required +The password for your given Jabber account. + +recipient +*Required +The Jabber ID (JID) that will receive the messages. + +""" +import logging + +_LOGGER = logging.getLogger(__name__) + +try: + import sleekxmpp + +except ImportError: + _LOGGER.exception( + "Unable to import sleekxmpp. " + "Did you maybe not install the 'SleekXMPP' package?") + +from homeassistant.helpers import validate_config +from homeassistant.components.notify import ( + DOMAIN, ATTR_TITLE, BaseNotificationService) + + +def get_service(hass, config): + """ Get the Jabber (XMPP) notification service. """ + + if not validate_config(config, + {DOMAIN: ['sender', + 'password', + 'recipient']}, + _LOGGER): + return None + + try: + SendNotificationBot(config[DOMAIN]['sender'] + '/home-assistant', + config[DOMAIN]['password'], + config[DOMAIN]['recipient'], + '') + except ImportError: + _LOGGER.exception( + "Unable to contact jabber server." + "Please check your credentials.") + + return None + + return XmppNotificationService(config[DOMAIN]['sender'], + config[DOMAIN]['password'], + config[DOMAIN]['recipient']) + + +# 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) + data = title + ": " + message + + SendNotificationBot(self._sender + '/home-assistant', + self._password, + self._recipient, + data) + + +class SendNotificationBot(sleekxmpp.ClientXMPP): + """ Service for sending Jabber (XMPP) messages. """ + + def __init__(self, jid, password, recipient, msg): + + super(SendNotificationBot, self).__init__(jid, password) + + logging.basicConfig(level=logging.ERROR) + + self.recipient = recipient + self.msg = msg + + 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(threaded=False) + + def start(self, event): + """ Starts the communication and sends the message. """ + self.send_presence() + self.get_roster() + self.send_message(mto=self.recipient, mbody=self.msg, mtype='chat') + self.disconnect(wait=True) + + def check_credentials(self, event): + """" Disconnect from the server if credentials are invalid. """ + self.disconnect() From 7b0765e064b7c96d22b589e4e7536870531b901c Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 7 May 2015 22:36:37 +0200 Subject: [PATCH 3/5] add xmpp.py --- .coveragerc | 1 + 1 file changed, 1 insertion(+) diff --git a/.coveragerc b/.coveragerc index 6ba5e2f9364..d97befce096 100644 --- a/.coveragerc +++ b/.coveragerc @@ -35,6 +35,7 @@ omit = homeassistant/components/notify/nma.py homeassistant/components/notify/pushbullet.py homeassistant/components/notify/pushover.py + homeassistant/components/notify/xmpp.py homeassistant/components/sensor/mysensors.py homeassistant/components/sensor/openweathermap.py homeassistant/components/sensor/sabnzbd.py From f103d5964e801c029ea970b5dd3989304200abfe Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Sun, 10 May 2015 23:20:36 +0200 Subject: [PATCH 4/5] remove deps of sleekxmpp --- requirements.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index b270cdc53e3..8bc2c4616ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -52,5 +52,4 @@ pyowm>=2.2.0 # XMPP Bindings (notify.xmpp) sleekxmpp>=1.3.1 -pyasn1>=0.1.7 -pyasn1_modules>=0.0.5 + From 76fd70657ad1523eb2ef2797765d37bb97d48c58 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Sun, 10 May 2015 23:22:06 +0200 Subject: [PATCH 5/5] remove deprecated parameter --- homeassistant/components/notify/xmpp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/notify/xmpp.py b/homeassistant/components/notify/xmpp.py index 769e9a89844..e6718bdfffe 100644 --- a/homeassistant/components/notify/xmpp.py +++ b/homeassistant/components/notify/xmpp.py @@ -113,7 +113,7 @@ class SendNotificationBot(sleekxmpp.ClientXMPP): self.add_event_handler("failed_auth", self.check_credentials) self.add_event_handler('session_start', self.start) self.connect() - self.process(threaded=False) + self.process(block=False) def start(self, event): """ Starts the communication and sends the message. """