From fd032cf6b7b8793be5b6653c72286d5e5a458d25 Mon Sep 17 00:00:00 2001 From: sfam Date: Fri, 4 Sep 2015 16:28:58 +0100 Subject: [PATCH 1/8] support for trigger Maker IFTTT --- homeassistant/components/ifttt.py | 96 +++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 homeassistant/components/ifttt.py diff --git a/homeassistant/components/ifttt.py b/homeassistant/components/ifttt.py new file mode 100644 index 00000000000..fa12000417f --- /dev/null +++ b/homeassistant/components/ifttt.py @@ -0,0 +1,96 @@ +""" +homeassistant.components.ifttt +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +This component enable you to trigger Maker IFTTT recipes. +Check https://ifttt.com/maker for details. + +Configuration: + +To use Maker IFTTT you will need to add something like the following to your +config/configuration.yaml. + +ifttt: + key: xxxxx-x-xxxxxxxxxxxxx + +Variables: + +key +*Required +Your api key + +""" +import logging +import requests + +from homeassistant.helpers import validate_config + +_LOGGER = logging.getLogger(__name__) + +DOMAIN = "ifttt" + +SERVICE_TRIGGER = 'trigger' + +ATTR_EVENT = 'event' +ATTR_VALUE1 = 'value1' +ATTR_VALUE2 = 'value2' +ATTR_VALUE3 = 'value3' + +DEPENDENCIES = [] + +REQUIREMENTS = ['pyfttt==0.1'] + + +def trigger(hass, event, value1=None, value2=None, value3=None): + """ Trigger a Maker IFTTT recipe """ + data = { + ATTR_EVENT: event, + ATTR_VALUE1: value1, + ATTR_VALUE2: value2, + ATTR_VALUE3: value3, + } + hass.services.call(DOMAIN, SERVICE_TRIGGER, data) + + +def setup(hass, config): + """ Setup the ifttt service component """ + + if not validate_config(config, {DOMAIN: ['key']}, _LOGGER): + return False + + key = config[DOMAIN]['key'] + + def trigger_service(call): + """ Handle ifttt trigger service calls. """ + event = call.data.get(ATTR_EVENT) + value1 = call.data.get(ATTR_VALUE1) + value2 = call.data.get(ATTR_VALUE2) + value3 = call.data.get(ATTR_VALUE3) + if event is None: + return + + try: + import pyfttt as pyfttt + r = pyfttt.send_event(key, event, value1, value2, value3) + except requests.exceptions.ConnectionError: + _LOGGER.error("Could not connect to IFTTT") + except requests.exceptions.HTTPError: + _LOGGER.error("Received invalid response") + except requests.exceptions.Timeout: + _LOGGER.error("Request timed out") + except requests.exceptions.TooManyRedirects: + _LOGGER.error("Too many redirects") + except requests.exceptions.RequestException as e: + _LOGGER.error("{e}".format(e=e)) + + if r.status_code != requests.codes.ok: + try: + j = r.json() + except ValueError: + _LOGGER.error('Could not parse response. Event not sent!') + + for e in j['errors']: + _LOGGER.error('{}'.format(e['message'])) + + hass.services.register(DOMAIN, SERVICE_TRIGGER, trigger_service) + + return True From 1ffb4d9a55a1c0531ca08b54a048de9cb140b8b4 Mon Sep 17 00:00:00 2001 From: sfam Date: Fri, 4 Sep 2015 16:48:12 +0100 Subject: [PATCH 2/8] Add ifttt.py to .coveragerc --- .coveragerc | 1 + 1 file changed, 1 insertion(+) diff --git a/.coveragerc b/.coveragerc index bf2e3c599bd..0aa3b54f8d9 100644 --- a/.coveragerc +++ b/.coveragerc @@ -28,6 +28,7 @@ omit = homeassistant/components/zwave.py homeassistant/components/*/zwave.py + homeassistant/components/ifttt.py homeassistant/components/browser.py homeassistant/components/camera/* homeassistant/components/device_tracker/actiontec.py From b8e2bf6b7e75f876c2a9e09082a75a4d81605324 Mon Sep 17 00:00:00 2001 From: sfam Date: Fri, 4 Sep 2015 16:51:25 +0100 Subject: [PATCH 3/8] Add pyfttt requirement for Maker IFTTT --- requirements_all.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/requirements_all.txt b/requirements_all.txt index 53e830fccde..0bdc690f110 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -119,3 +119,6 @@ https://github.com/bashwork/pymodbus/archive/d7fc4f1cc975631e0a9011390e8017f64b6 # Verisure (verisure) https://github.com/persandstrom/python-verisure/archive/9873c4527f01b1ba1f72ae60f7f35854390d59be.zip + +# Python tools for interacting with IFTTT Maker Channel (ifttt) +pyfttt==0.1 From 14fc4f6f99928a90eccf91a30575f7ea80b5c940 Mon Sep 17 00:00:00 2001 From: sfam Date: Fri, 4 Sep 2015 16:55:55 +0100 Subject: [PATCH 4/8] Support for trigger Maker IFTTT (fix pylint) --- homeassistant/components/ifttt.py | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/ifttt.py b/homeassistant/components/ifttt.py index fa12000417f..422cfa4e058 100644 --- a/homeassistant/components/ifttt.py +++ b/homeassistant/components/ifttt.py @@ -70,26 +70,9 @@ def setup(hass, config): try: import pyfttt as pyfttt - r = pyfttt.send_event(key, event, value1, value2, value3) - except requests.exceptions.ConnectionError: - _LOGGER.error("Could not connect to IFTTT") - except requests.exceptions.HTTPError: - _LOGGER.error("Received invalid response") - except requests.exceptions.Timeout: - _LOGGER.error("Request timed out") - except requests.exceptions.TooManyRedirects: - _LOGGER.error("Too many redirects") - except requests.exceptions.RequestException as e: - _LOGGER.error("{e}".format(e=e)) - - if r.status_code != requests.codes.ok: - try: - j = r.json() - except ValueError: - _LOGGER.error('Could not parse response. Event not sent!') - - for e in j['errors']: - _LOGGER.error('{}'.format(e['message'])) + resp = pyfttt.send_event(key, event, value1, value2, value3) + except: + _LOGGER.exception("Error communicating with IFTTT") hass.services.register(DOMAIN, SERVICE_TRIGGER, trigger_service) From 6c70ef2e6d658fd164e194fe4e0ed7536a8727db Mon Sep 17 00:00:00 2001 From: sfam Date: Fri, 4 Sep 2015 21:57:15 +0100 Subject: [PATCH 5/8] upgrade pyfttt version to 0.3 --- requirements_all.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements_all.txt b/requirements_all.txt index 0bdc690f110..d8a03b63a98 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -121,4 +121,4 @@ https://github.com/bashwork/pymodbus/archive/d7fc4f1cc975631e0a9011390e8017f64b6 https://github.com/persandstrom/python-verisure/archive/9873c4527f01b1ba1f72ae60f7f35854390d59be.zip # Python tools for interacting with IFTTT Maker Channel (ifttt) -pyfttt==0.1 +pyfttt==0.3 From 56151a07a52f1a40af76b1140f5040565c83d287 Mon Sep 17 00:00:00 2001 From: sfam Date: Fri, 4 Sep 2015 21:58:09 +0100 Subject: [PATCH 6/8] Support for trigger Maker IFTTT (pyfttt 0.3) --- homeassistant/components/ifttt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/ifttt.py b/homeassistant/components/ifttt.py index 422cfa4e058..7542f481c0e 100644 --- a/homeassistant/components/ifttt.py +++ b/homeassistant/components/ifttt.py @@ -37,7 +37,7 @@ ATTR_VALUE3 = 'value3' DEPENDENCIES = [] -REQUIREMENTS = ['pyfttt==0.1'] +REQUIREMENTS = ['pyfttt==0.3'] def trigger(hass, event, value1=None, value2=None, value3=None): From f9ad12920ecc5f11ca48c09d74f55d8827de4c68 Mon Sep 17 00:00:00 2001 From: sfam Date: Fri, 4 Sep 2015 22:14:28 +0100 Subject: [PATCH 7/8] Support for trigger Maker IFTTT (fix pylint) --- homeassistant/components/ifttt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/ifttt.py b/homeassistant/components/ifttt.py index 7542f481c0e..f387e3ae3ae 100644 --- a/homeassistant/components/ifttt.py +++ b/homeassistant/components/ifttt.py @@ -70,8 +70,8 @@ def setup(hass, config): try: import pyfttt as pyfttt - resp = pyfttt.send_event(key, event, value1, value2, value3) - except: + pyfttt.send_event(key, event, value1, value2, value3) + except requests.exceptions: _LOGGER.exception("Error communicating with IFTTT") hass.services.register(DOMAIN, SERVICE_TRIGGER, trigger_service) From 5b7dab655694724ac61da6c727d0a28f6e0445ca Mon Sep 17 00:00:00 2001 From: sfam Date: Fri, 4 Sep 2015 22:42:11 +0100 Subject: [PATCH 8/8] Support for trigger Maker IFTTT (fix pylint) --- homeassistant/components/ifttt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/ifttt.py b/homeassistant/components/ifttt.py index f387e3ae3ae..1eacd61bcee 100644 --- a/homeassistant/components/ifttt.py +++ b/homeassistant/components/ifttt.py @@ -71,7 +71,7 @@ def setup(hass, config): try: import pyfttt as pyfttt pyfttt.send_event(key, event, value1, value2, value3) - except requests.exceptions: + except requests.exceptions.RequestException: _LOGGER.exception("Error communicating with IFTTT") hass.services.register(DOMAIN, SERVICE_TRIGGER, trigger_service)