Support multiple keys in ifttt triggers (#21454)

* Support multiple keys in ifttt triggers

* Rename `to` to `target` in ifttt.

Follow PR code review suggestions
pull/21727/head
Sergio Oller 2019-03-07 04:47:13 +01:00 committed by Paulus Schoutsen
parent dbf129dfdd
commit 9f06be750f
1 changed files with 19 additions and 3 deletions

View File

@ -17,6 +17,7 @@ _LOGGER = logging.getLogger(__name__)
EVENT_RECEIVED = 'ifttt_webhook_received'
ATTR_EVENT = 'event'
ATTR_TARGET = 'target'
ATTR_VALUE1 = 'value1'
ATTR_VALUE2 = 'value2'
ATTR_VALUE3 = 'value3'
@ -29,6 +30,7 @@ SERVICE_TRIGGER = 'trigger'
SERVICE_TRIGGER_SCHEMA = vol.Schema({
vol.Required(ATTR_EVENT): cv.string,
vol.Optional(ATTR_TARGET): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(ATTR_VALUE1): cv.string,
vol.Optional(ATTR_VALUE2): cv.string,
vol.Optional(ATTR_VALUE3): cv.string,
@ -36,7 +38,7 @@ SERVICE_TRIGGER_SCHEMA = vol.Schema({
CONFIG_SCHEMA = vol.Schema({
vol.Optional(DOMAIN): vol.Schema({
vol.Required(CONF_KEY): cv.string,
vol.Required(CONF_KEY): vol.Any({cv.string: cv.string}, cv.string),
}),
}, extra=vol.ALLOW_EXTRA)
@ -46,18 +48,32 @@ async def async_setup(hass, config):
if DOMAIN not in config:
return True
key = config[DOMAIN][CONF_KEY]
api_keys = config[DOMAIN][CONF_KEY]
if isinstance(api_keys, str):
api_keys = {"default": api_keys}
def trigger_service(call):
"""Handle IFTTT trigger service calls."""
event = call.data[ATTR_EVENT]
targets = call.data.get(ATTR_TARGET, list(api_keys))
value1 = call.data.get(ATTR_VALUE1)
value2 = call.data.get(ATTR_VALUE2)
value3 = call.data.get(ATTR_VALUE3)
target_keys = dict()
for target in targets:
if target not in api_keys:
_LOGGER.error("No IFTTT api key for %s", target)
continue
target_keys[target] = api_keys[target]
try:
import pyfttt
pyfttt.send_event(key, event, value1, value2, value3)
for target, key in target_keys.items():
res = pyfttt.send_event(key, event, value1, value2, value3)
if res.status_code != 200:
_LOGGER.error("IFTTT reported error sending event to %s.",
target)
except requests.exceptions.RequestException:
_LOGGER.exception("Error communicating with IFTTT")