From f6017a17b25a27c489cda09ba44f3afac27eae76 Mon Sep 17 00:00:00 2001
From: hydreliox <hydreliox@gmail.com>
Date: Thu, 21 Jan 2016 02:02:32 +0100
Subject: [PATCH] Add Twitter as a notification platform

---
 .coveragerc                                |  1 +
 homeassistant/components/notify/twitter.py | 59 ++++++++++++++++++++++
 requirements_all.txt                       |  3 ++
 3 files changed, 63 insertions(+)
 create mode 100644 homeassistant/components/notify/twitter.py

diff --git a/.coveragerc b/.coveragerc
index 57c3d89d647..9242d5e3745 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -94,6 +94,7 @@ omit =
     homeassistant/components/notify/smtp.py
     homeassistant/components/notify/syslog.py
     homeassistant/components/notify/telegram.py
+	homeassistant/components/notify/twitter.py
     homeassistant/components/notify/xmpp.py
     homeassistant/components/sensor/arest.py
     homeassistant/components/sensor/bitcoin.py
diff --git a/homeassistant/components/notify/twitter.py b/homeassistant/components/notify/twitter.py
new file mode 100644
index 00000000000..7a9941ce7df
--- /dev/null
+++ b/homeassistant/components/notify/twitter.py
@@ -0,0 +1,59 @@
+"""
+homeassistant.components.notify.free_mobile
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Twitter platform for notify component.
+
+For more details about this platform, please refer to the documentation at
+https://home-assistant.io/components/notify.twitter/
+"""
+import logging
+from homeassistant.helpers import validate_config
+from homeassistant.components.notify import (
+    DOMAIN, BaseNotificationService)
+from homeassistant.const import CONF_ACCESS_TOKEN
+
+_LOGGER = logging.getLogger(__name__)
+REQUIREMENTS = ['TwitterAPI==2.3.6']
+
+CONF_CONSUMER_KEY = "consumer_key"
+CONF_CONSUMER_SECRET = "consumer_secret"
+CONF_ACCESS_TOKEN_SECRET = "access_token_secret"
+
+
+def get_service(hass, config):
+    """ Get the Twitter notification service. """
+
+    if not validate_config({DOMAIN: config},
+                           {DOMAIN: [CONF_CONSUMER_KEY, CONF_CONSUMER_SECRET,
+                                     CONF_ACCESS_TOKEN,
+                                     CONF_ACCESS_TOKEN_SECRET]},
+                           _LOGGER):
+        return None
+
+    return TwitterNotificationService(config[CONF_CONSUMER_KEY],
+                                      config[CONF_CONSUMER_SECRET],
+                                      config[CONF_ACCESS_TOKEN],
+                                      config[CONF_ACCESS_TOKEN_SECRET])
+
+
+# pylint: disable=too-few-public-methods
+class TwitterNotificationService(BaseNotificationService):
+    """ Implements notification service for the Twitter service. """
+
+    def __init__(self, consumer_key, consumer_secret, access_token_key,
+                 access_token_secret):
+        from TwitterAPI import TwitterAPI
+        self.api = TwitterAPI(consumer_key, consumer_secret, access_token_key,
+                              access_token_secret)
+
+    def send_message(self, message="", **kwargs):
+        """ Tweet some message. """
+        resp = self.api.request('statuses/update', {'status': message})
+        if resp.status_code != 200:
+            import json
+            obj = json.loads(resp.text)
+            error_message = obj['errors'][0]['message']
+            error_code = obj['errors'][0]['code']
+            _LOGGER.error("Error %s : %s (Code %s)", resp.status_code,
+                          error_message,
+                          error_code)
diff --git a/requirements_all.txt b/requirements_all.txt
index b6d0af08ee0..0a05e573699 100644
--- a/requirements_all.txt
+++ b/requirements_all.txt
@@ -116,6 +116,9 @@ slacker==0.6.8
 # homeassistant.components.notify.telegram
 python-telegram-bot==2.8.7
 
+# homeassistant.components.notify.twitter
+TwitterAPI==2.3.6
+
 # homeassistant.components.notify.xmpp
 sleekxmpp==1.3.1
 
-- 
GitLab