2015-09-04 15:28:58 +00:00
|
|
|
"""
|
2016-03-07 17:49:31 +00:00
|
|
|
Support to trigger Maker IFTTT recipes.
|
2015-09-04 15:28:58 +00:00
|
|
|
|
2015-10-25 14:06:10 +00:00
|
|
|
For more details about this component, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/ifttt/
|
2015-09-04 15:28:58 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2015-09-04 15:28:58 +00:00
|
|
|
import requests
|
2016-04-13 03:08:20 +00:00
|
|
|
import voluptuous as vol
|
2015-09-04 15:28:58 +00:00
|
|
|
|
2016-04-13 03:08:20 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-09-04 15:28:58 +00:00
|
|
|
|
2016-09-02 04:34:42 +00:00
|
|
|
REQUIREMENTS = ['pyfttt==0.3']
|
2015-09-04 15:28:58 +00:00
|
|
|
|
2016-09-02 04:34:42 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-09-04 15:28:58 +00:00
|
|
|
|
|
|
|
ATTR_EVENT = 'event'
|
|
|
|
ATTR_VALUE1 = 'value1'
|
|
|
|
ATTR_VALUE2 = 'value2'
|
|
|
|
ATTR_VALUE3 = 'value3'
|
|
|
|
|
2016-09-02 04:34:42 +00:00
|
|
|
CONF_KEY = 'key'
|
|
|
|
|
|
|
|
DOMAIN = 'ifttt'
|
|
|
|
|
|
|
|
SERVICE_TRIGGER = 'trigger'
|
2015-09-04 15:28:58 +00:00
|
|
|
|
2016-04-13 03:08:20 +00:00
|
|
|
SERVICE_TRIGGER_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(ATTR_EVENT): cv.string,
|
|
|
|
vol.Optional(ATTR_VALUE1): cv.string,
|
|
|
|
vol.Optional(ATTR_VALUE2): cv.string,
|
|
|
|
vol.Optional(ATTR_VALUE3): cv.string,
|
|
|
|
})
|
|
|
|
|
2016-09-02 04:34:42 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Required(CONF_KEY): cv.string,
|
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2015-09-04 15:28:58 +00:00
|
|
|
|
|
|
|
def trigger(hass, event, value1=None, value2=None, value3=None):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Trigger a Maker IFTTT recipe."""
|
2015-09-04 15:28:58 +00:00
|
|
|
data = {
|
|
|
|
ATTR_EVENT: event,
|
|
|
|
ATTR_VALUE1: value1,
|
|
|
|
ATTR_VALUE2: value2,
|
|
|
|
ATTR_VALUE3: value3,
|
|
|
|
}
|
|
|
|
hass.services.call(DOMAIN, SERVICE_TRIGGER, data)
|
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Setup the IFTTT service component."""
|
2016-09-02 04:34:42 +00:00
|
|
|
key = config[DOMAIN][CONF_KEY]
|
2015-09-04 15:28:58 +00:00
|
|
|
|
|
|
|
def trigger_service(call):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Handle IFTTT trigger service calls."""
|
2016-04-13 03:08:20 +00:00
|
|
|
event = call.data[ATTR_EVENT]
|
2015-09-04 15:28:58 +00:00
|
|
|
value1 = call.data.get(ATTR_VALUE1)
|
|
|
|
value2 = call.data.get(ATTR_VALUE2)
|
|
|
|
value3 = call.data.get(ATTR_VALUE3)
|
|
|
|
|
|
|
|
try:
|
|
|
|
import pyfttt as pyfttt
|
2015-09-04 21:14:28 +00:00
|
|
|
pyfttt.send_event(key, event, value1, value2, value3)
|
2015-09-04 21:42:11 +00:00
|
|
|
except requests.exceptions.RequestException:
|
2015-09-04 15:55:55 +00:00
|
|
|
_LOGGER.exception("Error communicating with IFTTT")
|
2015-09-04 15:28:58 +00:00
|
|
|
|
2016-04-13 03:08:20 +00:00
|
|
|
hass.services.register(DOMAIN, SERVICE_TRIGGER, trigger_service,
|
|
|
|
schema=SERVICE_TRIGGER_SCHEMA)
|
2015-09-04 15:28:58 +00:00
|
|
|
|
|
|
|
return True
|