1 """Support to trigger Maker IFTTT recipes."""
3 from __future__
import annotations
5 from http
import HTTPStatus
9 from aiohttp
import web
12 import voluptuous
as vol
22 from .const
import DOMAIN
24 _LOGGER = logging.getLogger(__name__)
26 EVENT_RECEIVED =
"ifttt_webhook_received"
29 ATTR_TARGET =
"target"
30 ATTR_VALUE1 =
"value1"
31 ATTR_VALUE2 =
"value2"
32 ATTR_VALUE3 =
"value3"
36 SERVICE_PUSH_ALARM_STATE =
"push_alarm_state"
37 SERVICE_TRIGGER =
"trigger"
39 SERVICE_TRIGGER_SCHEMA = vol.Schema(
41 vol.Required(ATTR_EVENT): cv.string,
42 vol.Optional(ATTR_TARGET): vol.All(cv.ensure_list, [cv.string]),
43 vol.Optional(ATTR_VALUE1): cv.string,
44 vol.Optional(ATTR_VALUE2): cv.string,
45 vol.Optional(ATTR_VALUE3): cv.string,
49 CONFIG_SCHEMA = vol.Schema(
51 vol.Optional(DOMAIN): vol.Schema(
52 {vol.Required(CONF_KEY): vol.Any({cv.string: cv.string}, cv.string)}
55 extra=vol.ALLOW_EXTRA,
59 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
60 """Set up the IFTTT service component."""
61 if DOMAIN
not in config:
64 api_keys = config[DOMAIN][CONF_KEY]
65 if isinstance(api_keys, str):
66 api_keys = {
"default": api_keys}
68 def trigger_service(call: ServiceCall) ->
None:
69 """Handle IFTTT trigger service calls."""
70 event = call.data[ATTR_EVENT]
71 targets = call.data.get(ATTR_TARGET,
list(api_keys))
72 value1 = call.data.get(ATTR_VALUE1)
73 value2 = call.data.get(ATTR_VALUE2)
74 value3 = call.data.get(ATTR_VALUE3)
77 for target
in targets:
78 if target
not in api_keys:
79 _LOGGER.error(
"No IFTTT api key for %s", target)
81 target_keys[target] = api_keys[target]
84 for target, key
in target_keys.items():
85 res = pyfttt.send_event(key, event, value1, value2, value3)
86 if res.status_code != HTTPStatus.OK:
87 _LOGGER.error(
"IFTTT reported error sending event to %s", target)
88 except requests.exceptions.RequestException:
89 _LOGGER.exception(
"Error communicating with IFTTT")
91 hass.services.async_register(
92 DOMAIN, SERVICE_TRIGGER, trigger_service, schema=SERVICE_TRIGGER_SCHEMA
99 hass: HomeAssistant, webhook_id: str, request: web.Request
101 """Handle webhook callback."""
102 body = await request.text()
104 data = json.loads(body)
if body
else {}
107 "Received invalid data from IFTTT. Data needs to be formatted as JSON: %s",
112 if not isinstance(data, dict):
114 "Received invalid data from IFTTT. Data needs to be a dictionary: %s", data
118 data[
"webhook_id"] = webhook_id
119 hass.bus.async_fire(EVENT_RECEIVED, data)
123 """Configure based on config entry."""
124 webhook.async_register(
125 hass, DOMAIN,
"IFTTT", entry.data[CONF_WEBHOOK_ID], handle_webhook
131 """Unload a config entry."""
132 webhook.async_unregister(hass, entry.data[CONF_WEBHOOK_ID])
136 async_remove_entry = config_entry_flow.webhook_async_remove_entry
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None handle_webhook(HomeAssistant hass, str webhook_id, web.Request request)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)