2018-09-30 12:45:48 +00:00
|
|
|
"""Test the init file of IFTTT."""
|
2018-10-23 09:14:46 +00:00
|
|
|
from unittest.mock import patch
|
2018-09-30 12:45:48 +00:00
|
|
|
|
|
|
|
from homeassistant import data_entry_flow
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.components import ifttt
|
|
|
|
|
|
|
|
|
|
|
|
async def test_config_flow_registers_webhook(hass, aiohttp_client):
|
|
|
|
"""Test setting up IFTTT and sending webhook."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.get_local_ip", return_value="example.com"):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"ifttt", context={"source": "user"}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM, result
|
2018-09-30 12:45:48 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
webhook_id = result["result"].data["webhook_id"]
|
2018-09-30 12:45:48 +00:00
|
|
|
|
|
|
|
ifttt_events = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def handle_event(event):
|
|
|
|
"""Handle IFTTT event."""
|
|
|
|
ifttt_events.append(event)
|
|
|
|
|
|
|
|
hass.bus.async_listen(ifttt.EVENT_RECEIVED, handle_event)
|
|
|
|
|
|
|
|
client = await aiohttp_client(hass.http.app)
|
2019-07-31 19:25:30 +00:00
|
|
|
await client.post("/api/webhook/{}".format(webhook_id), json={"hello": "ifttt"})
|
2018-09-30 12:45:48 +00:00
|
|
|
|
|
|
|
assert len(ifttt_events) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert ifttt_events[0].data["webhook_id"] == webhook_id
|
|
|
|
assert ifttt_events[0].data["hello"] == "ifttt"
|