From fe5bb89a686ac7e5d129190a4cb5b610a6ed7b43 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Fri, 9 Oct 2015 14:04:29 +0200 Subject: [PATCH 1/4] Add telegram notifier --- homeassistant/components/notify/telegram.py | 66 +++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 homeassistant/components/notify/telegram.py diff --git a/homeassistant/components/notify/telegram.py b/homeassistant/components/notify/telegram.py new file mode 100644 index 00000000000..c65d9ccf6cb --- /dev/null +++ b/homeassistant/components/notify/telegram.py @@ -0,0 +1,66 @@ +""" +homeassistant.components.notify.telegram +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Telegram platform for notify component. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/notify.xmpp.html +""" +import logging +import urllib + +from homeassistant.helpers import validate_config +from homeassistant.components.notify import ( + DOMAIN, ATTR_TITLE, BaseNotificationService) +from homeassistant.const import CONF_API_KEY + +_LOGGER = logging.getLogger(__name__) +REQUIREMENTS = ['python-telegram-bot==2.8.7'] + + +def get_service(hass, config): + """ Get the Telegram notification service. """ + + if not validate_config(config, + {DOMAIN: [CONF_API_KEY, 'chat_id']}, + _LOGGER): + return None + + try: + import telegram + except ImportError: + _LOGGER.exception( + "Unable to import python-telegram-bot. " + "Did you maybe not install the 'python-telegram-bot' package?") + return None + + try: + bot = telegram.Bot(token=config[DOMAIN][CONF_API_KEY]) + username = bot.getMe()['username'] + _LOGGER.info("Telegram bot is' %s'", username) + except urllib.error.HTTPError: + _LOGGER.error("Please check your access token.") + return None + + return TelegramNotificationService( + config[DOMAIN][CONF_API_KEY], + config[DOMAIN]['chat_id']) + + +# pylint: disable=too-few-public-methods +class TelegramNotificationService(BaseNotificationService): + """ Implements notification service for Telegram. """ + + def __init__(self, api_key, chat_id): + import telegram + self._api_key = api_key + self._chat_id = chat_id + self.bot = telegram.Bot(token=self._api_key) + + def send_message(self, message="", **kwargs): + """ Send a message to a user. """ + + title = kwargs.get(ATTR_TITLE) + + self.bot.sendMessage(chat_id=self._chat_id, + text=title + " " + message) From 3ef5e7c16143fc2d3750ae04a01c0ab08ae3259e Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Fri, 9 Oct 2015 14:12:49 +0200 Subject: [PATCH 2/4] Add telegram --- .coveragerc | 1 + 1 file changed, 1 insertion(+) diff --git a/.coveragerc b/.coveragerc index 26fae1ba3f9..2364cb4d8d1 100644 --- a/.coveragerc +++ b/.coveragerc @@ -65,6 +65,7 @@ omit = homeassistant/components/notify/slack.py homeassistant/components/notify/smtp.py homeassistant/components/notify/syslog.py + homeassistant/components/notify/telegram.py homeassistant/components/notify/xmpp.py homeassistant/components/sensor/arest.py homeassistant/components/sensor/bitcoin.py From 9f6ce868e23e8d28606a144bbfd289ab1d61dd73 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Fri, 9 Oct 2015 14:13:05 +0200 Subject: [PATCH 3/4] Add telegram --- requirements_all.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/requirements_all.txt b/requirements_all.txt index e78effedfb8..5fa48f3c10d 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -143,3 +143,6 @@ pysnmp==4.2.5 # Blinkstick blinkstick==1.1.7 + +# Telegram (notify.telegram) +python-telegram-bot==2.8.7 From f8efe3f00f9d0346e1e9686992ac6c447949c844 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Fri, 9 Oct 2015 14:48:58 +0200 Subject: [PATCH 4/4] Update link to docs --- homeassistant/components/notify/telegram.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/notify/telegram.py b/homeassistant/components/notify/telegram.py index c65d9ccf6cb..23b915baf1e 100644 --- a/homeassistant/components/notify/telegram.py +++ b/homeassistant/components/notify/telegram.py @@ -4,7 +4,7 @@ homeassistant.components.notify.telegram Telegram platform for notify component. For more details about this platform, please refer to the documentation at -https://home-assistant.io/components/notify.xmpp.html +https://home-assistant.io/components/notify.telegram.html """ import logging import urllib