2018-09-30 14:45:48 +02:00
|
|
|
"""Test the init file of IFTTT."""
|
2024-03-08 14:50:04 +01:00
|
|
|
|
2021-04-25 12:27:40 +03:00
|
|
|
from homeassistant import config_entries, data_entry_flow
|
2018-09-30 14:45:48 +02:00
|
|
|
from homeassistant.components import ifttt
|
2021-02-10 14:31:11 +01:00
|
|
|
from homeassistant.config import async_process_ha_core_config
|
2023-02-08 18:12:56 +01:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2018-09-30 14:45:48 +02:00
|
|
|
|
2023-02-08 18:12:56 +01:00
|
|
|
from tests.typing import ClientSessionGenerator
|
2018-09-30 14:45:48 +02:00
|
|
|
|
2023-02-08 18:12:56 +01:00
|
|
|
|
|
|
|
async def test_config_flow_registers_webhook(
|
|
|
|
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
|
|
|
|
) -> None:
|
2018-09-30 14:45:48 +02:00
|
|
|
"""Test setting up IFTTT and sending webhook."""
|
2021-02-10 14:31:11 +01:00
|
|
|
await async_process_ha_core_config(
|
|
|
|
hass,
|
|
|
|
{"internal_url": "http://example.local:8123"},
|
|
|
|
)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 12:27:40 +03:00
|
|
|
"ifttt", context={"source": config_entries.SOURCE_USER}
|
2021-02-10 14:31:11 +01:00
|
|
|
)
|
2022-07-07 18:57:36 +02:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM, result
|
2018-09-30 14:45:48 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
2022-07-07 18:57:36 +02:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2019-07-31 12:25:30 -07:00
|
|
|
webhook_id = result["result"].data["webhook_id"]
|
2018-09-30 14:45:48 +02:00
|
|
|
|
|
|
|
ifttt_events = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def handle_event(event):
|
|
|
|
"""Handle IFTTT event."""
|
|
|
|
ifttt_events.append(event)
|
|
|
|
|
|
|
|
hass.bus.async_listen(ifttt.EVENT_RECEIVED, handle_event)
|
|
|
|
|
2021-09-02 14:49:40 +02:00
|
|
|
client = await hass_client_no_auth()
|
2020-04-05 00:33:07 +02:00
|
|
|
await client.post(f"/api/webhook/{webhook_id}", json={"hello": "ifttt"})
|
2018-09-30 14:45:48 +02:00
|
|
|
|
|
|
|
assert len(ifttt_events) == 1
|
2019-07-31 12:25:30 -07:00
|
|
|
assert ifttt_events[0].data["webhook_id"] == webhook_id
|
|
|
|
assert ifttt_events[0].data["hello"] == "ifttt"
|
2020-02-29 20:37:06 -08:00
|
|
|
|
|
|
|
# Invalid JSON
|
2020-04-05 00:33:07 +02:00
|
|
|
await client.post(f"/api/webhook/{webhook_id}", data="not a dict")
|
2020-02-29 20:37:06 -08:00
|
|
|
assert len(ifttt_events) == 1
|
|
|
|
|
|
|
|
# Not a dict
|
2020-04-05 00:33:07 +02:00
|
|
|
await client.post(f"/api/webhook/{webhook_id}", json="not a dict")
|
2020-02-29 20:37:06 -08:00
|
|
|
assert len(ifttt_events) == 1
|