From 6892048de0183627e2bd30bfaf2195125978fb77 Mon Sep 17 00:00:00 2001 From: Gopal Date: Thu, 29 Dec 2016 18:19:11 +0530 Subject: [PATCH 1/4] Facebook Notify Started --- homeassistant/components/notify/facebook.py | 61 +++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 homeassistant/components/notify/facebook.py diff --git a/homeassistant/components/notify/facebook.py b/homeassistant/components/notify/facebook.py new file mode 100644 index 00000000000..bf5bf9cf195 --- /dev/null +++ b/homeassistant/components/notify/facebook.py @@ -0,0 +1,61 @@ +""" +Facebook platform for notify component. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/notify.facebook/ +""" +import logging + +import requests + +import voluptuous as vol + +import homeassistant.helpers.config_validation as cv +from homeassistant.components.notify import ( + PLATFORM_SCHEMA, BaseNotificationService) + +_LOGGER = logging.getLogger(__name__) + +CONF_ID = 'id' +CONF_PHONE_NUMBER = 'phone_number' +CONF_PAGE_ACCESS_TOKEN = 'page_access_token' +BASE_URL = 'https://graph.facebook.com/v2.6/me/messages' + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_PHONE_NUMBER): cv.string, + vol.Required(CONF_PAGE_ACCESS_TOKEN): cv.string, +}) + + +def get_service(hass, config): + """Get the Twitter notification service.""" + return FacebookNotificationService( + config[CONF_PHONE_NUMBER], config[CONF_PAGE_ACCESS_TOKEN] + ) + + +class FacebookNotificationService(BaseNotificationService): + """Implementation of a notification service for the Twitter service.""" + + def __init__(self, id, access_token): + """Initialize the service.""" + self.user_id = id + self.page_access_token = access_token + + def send_message(self, message="", **kwargs): + """Send some message.""" + payload = {'access_token': self.page_access_token} + body = { + "recipient": {"phone_number": self.user_id}, + "message": {"text": message} + } + import json + resp = requests.post(BASE_URL, data=json.dumps(body), params=payload, + headers={'Content-Type': 'application/json'}) + if resp.status_code != 200: + obj = resp.json() + error_message = obj['error']['message'] + error_code = obj['error']['code'] + _LOGGER.error("Error %s : %s (Code %s)", resp.status_code, + error_message, + error_code) From 227fb29cabab708f690e30beabdb4740c219d78c Mon Sep 17 00:00:00 2001 From: Gopal Date: Sat, 31 Dec 2016 09:59:44 +0530 Subject: [PATCH 2/4] Facebook notify updated --- homeassistant/components/notify/facebook.py | 52 +++++++++++---------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/notify/facebook.py b/homeassistant/components/notify/facebook.py index bf5bf9cf195..630d5453daf 100644 --- a/homeassistant/components/notify/facebook.py +++ b/homeassistant/components/notify/facebook.py @@ -12,50 +12,52 @@ import voluptuous as vol import homeassistant.helpers.config_validation as cv from homeassistant.components.notify import ( - PLATFORM_SCHEMA, BaseNotificationService) + ATTR_TARGET, PLATFORM_SCHEMA, BaseNotificationService) _LOGGER = logging.getLogger(__name__) -CONF_ID = 'id' -CONF_PHONE_NUMBER = 'phone_number' CONF_PAGE_ACCESS_TOKEN = 'page_access_token' BASE_URL = 'https://graph.facebook.com/v2.6/me/messages' PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ - vol.Required(CONF_PHONE_NUMBER): cv.string, vol.Required(CONF_PAGE_ACCESS_TOKEN): cv.string, }) def get_service(hass, config): - """Get the Twitter notification service.""" - return FacebookNotificationService( - config[CONF_PHONE_NUMBER], config[CONF_PAGE_ACCESS_TOKEN] - ) + """Get the Facebook notification service.""" + return FacebookNotificationService(config[CONF_PAGE_ACCESS_TOKEN]) class FacebookNotificationService(BaseNotificationService): - """Implementation of a notification service for the Twitter service.""" + """Implementation of a notification service for the Facebook service.""" - def __init__(self, id, access_token): + def __init__(self, access_token): """Initialize the service.""" - self.user_id = id self.page_access_token = access_token def send_message(self, message="", **kwargs): """Send some message.""" payload = {'access_token': self.page_access_token} - body = { - "recipient": {"phone_number": self.user_id}, - "message": {"text": message} - } - import json - resp = requests.post(BASE_URL, data=json.dumps(body), params=payload, - headers={'Content-Type': 'application/json'}) - if resp.status_code != 200: - obj = resp.json() - error_message = obj['error']['message'] - error_code = obj['error']['code'] - _LOGGER.error("Error %s : %s (Code %s)", resp.status_code, - error_message, - error_code) + targets = kwargs.get(ATTR_TARGET) + + if not targets: + _LOGGER.info("At least 1 target is required") + return + + for target in targets: + body = { + "recipient": {"phone_number": target}, + "message": {"text": message} + } + import json + resp = requests.post(BASE_URL, data=json.dumps(body), + params=payload, + headers={'Content-Type': 'application/json'}) + if resp.status_code != 200: + obj = resp.json() + error_message = obj['error']['message'] + error_code = obj['error']['code'] + _LOGGER.error("Error %s : %s (Code %s)", resp.status_code, + error_message, + error_code) From e89aa6b2d684045b379e77a517c07b9f15b844fe Mon Sep 17 00:00:00 2001 From: Gopal Date: Sat, 31 Dec 2016 10:11:30 +0530 Subject: [PATCH 3/4] File added to coveragerc --- .coveragerc | 1 + 1 file changed, 1 insertion(+) diff --git a/.coveragerc b/.coveragerc index 34531651358..cb0b70ac84b 100644 --- a/.coveragerc +++ b/.coveragerc @@ -219,6 +219,7 @@ omit = homeassistant/components/notify/aws_lambda.py homeassistant/components/notify/aws_sns.py homeassistant/components/notify/aws_sqs.py + homeassistant/components/notify/facebook.py homeassistant/components/notify/free_mobile.py homeassistant/components/notify/gntp.py homeassistant/components/notify/group.py From d09dcc4b03426e2f0216c64153ca8baedc6e0d90 Mon Sep 17 00:00:00 2001 From: Gopal Date: Wed, 4 Jan 2017 18:23:29 +0530 Subject: [PATCH 4/4] Timeout and Constant added --- homeassistant/components/notify/facebook.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/notify/facebook.py b/homeassistant/components/notify/facebook.py index 630d5453daf..2acabcf02c0 100644 --- a/homeassistant/components/notify/facebook.py +++ b/homeassistant/components/notify/facebook.py @@ -13,6 +13,7 @@ import voluptuous as vol import homeassistant.helpers.config_validation as cv from homeassistant.components.notify import ( ATTR_TARGET, PLATFORM_SCHEMA, BaseNotificationService) +from homeassistant.const import CONTENT_TYPE_JSON _LOGGER = logging.getLogger(__name__) @@ -42,7 +43,7 @@ class FacebookNotificationService(BaseNotificationService): targets = kwargs.get(ATTR_TARGET) if not targets: - _LOGGER.info("At least 1 target is required") + _LOGGER.error("At least 1 target is required") return for target in targets: @@ -53,7 +54,8 @@ class FacebookNotificationService(BaseNotificationService): import json resp = requests.post(BASE_URL, data=json.dumps(body), params=payload, - headers={'Content-Type': 'application/json'}) + headers={'Content-Type': CONTENT_TYPE_JSON}, + timeout=10) if resp.status_code != 200: obj = resp.json() error_message = obj['error']['message']