mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 09:47:52 +00:00
Cloudhooks for webhook config flows (#22611)
This commit is contained in:
parent
c96804954c
commit
42e3e878df
@ -79,6 +79,11 @@ async def async_unload_entry(hass, entry):
|
||||
hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID])
|
||||
return True
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
async_remove_entry = config_entry_flow.webhook_async_remove_entry
|
||||
|
||||
|
||||
config_entry_flow.register_webhook_flow(
|
||||
DOMAIN,
|
||||
'Dialogflow Webhook',
|
||||
|
@ -133,6 +133,11 @@ async def async_unload_entry(hass, entry):
|
||||
await hass.config_entries.async_forward_entry_unload(entry, DEVICE_TRACKER)
|
||||
return True
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
async_remove_entry = config_entry_flow.webhook_async_remove_entry
|
||||
|
||||
|
||||
config_entry_flow.register_webhook_flow(
|
||||
DOMAIN,
|
||||
'Geofency Webhook',
|
||||
|
@ -104,6 +104,11 @@ async def async_unload_entry(hass, entry):
|
||||
await hass.config_entries.async_forward_entry_unload(entry, DEVICE_TRACKER)
|
||||
return True
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
async_remove_entry = config_entry_flow.webhook_async_remove_entry
|
||||
|
||||
|
||||
config_entry_flow.register_webhook_flow(
|
||||
DOMAIN,
|
||||
'GPSLogger Webhook',
|
||||
|
@ -108,6 +108,11 @@ async def async_unload_entry(hass, entry):
|
||||
hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID])
|
||||
return True
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
async_remove_entry = config_entry_flow.webhook_async_remove_entry
|
||||
|
||||
|
||||
config_entry_flow.register_webhook_flow(
|
||||
DOMAIN,
|
||||
'IFTTT Webhook',
|
||||
|
@ -141,10 +141,14 @@ async def async_setup_entry(hass, entry):
|
||||
async def async_unload_entry(hass, entry):
|
||||
"""Unload a config entry."""
|
||||
hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID])
|
||||
|
||||
await hass.config_entries.async_forward_entry_unload(entry, DEVICE_TRACKER)
|
||||
return True
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
async_remove_entry = config_entry_flow.webhook_async_remove_entry
|
||||
|
||||
|
||||
config_entry_flow.register_webhook_flow(
|
||||
DOMAIN,
|
||||
'Locative Webhook',
|
||||
|
@ -88,6 +88,11 @@ async def async_unload_entry(hass, entry):
|
||||
hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID])
|
||||
return True
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
async_remove_entry = config_entry_flow.webhook_async_remove_entry
|
||||
|
||||
|
||||
config_entry_flow.register_webhook_flow(
|
||||
DOMAIN,
|
||||
'Mailgun Webhook',
|
||||
|
@ -60,6 +60,11 @@ async def async_unload_entry(hass, entry):
|
||||
hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID])
|
||||
return True
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
async_remove_entry = config_entry_flow.webhook_async_remove_entry
|
||||
|
||||
|
||||
config_entry_flow.register_webhook_flow(
|
||||
DOMAIN,
|
||||
'Twilio Webhook',
|
||||
|
@ -118,15 +118,35 @@ class WebhookFlowHandler(config_entries.ConfigFlow):
|
||||
)
|
||||
|
||||
webhook_id = self.hass.components.webhook.async_generate_id()
|
||||
|
||||
if self.hass.components.cloud.async_active_subscription():
|
||||
webhook_url = \
|
||||
await self.hass.components.cloud.async_create_cloudhook(
|
||||
webhook_id
|
||||
)
|
||||
cloudhook = True
|
||||
else:
|
||||
webhook_url = \
|
||||
self.hass.components.webhook.async_generate_url(webhook_id)
|
||||
cloudhook = False
|
||||
|
||||
self._description_placeholder['webhook_url'] = webhook_url
|
||||
|
||||
return self.async_create_entry(
|
||||
title=self._title,
|
||||
data={
|
||||
'webhook_id': webhook_id
|
||||
'webhook_id': webhook_id,
|
||||
'cloudhook': cloudhook,
|
||||
},
|
||||
description_placeholders=self._description_placeholder
|
||||
)
|
||||
|
||||
|
||||
async def webhook_async_remove_entry(hass, entry) -> None:
|
||||
"""Remove a webhook config entry."""
|
||||
if (not entry.data.get('cloudhook') or
|
||||
'cloud' not in hass.config.components):
|
||||
return
|
||||
|
||||
await hass.components.cloud.async_delete_cloudhook(
|
||||
entry.data['webhook_id'])
|
||||
|
@ -3,9 +3,9 @@ from unittest.mock import patch, Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries, data_entry_flow, loader
|
||||
from homeassistant import config_entries, data_entry_flow, loader, setup
|
||||
from homeassistant.helpers import config_entry_flow
|
||||
from tests.common import MockConfigEntry, MockModule
|
||||
from tests.common import MockConfigEntry, MockModule, mock_coro
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@ -193,3 +193,51 @@ async def test_webhook_config_flow_registers_webhook(hass, webhook_flow_conf):
|
||||
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result['data']['webhook_id'] is not None
|
||||
|
||||
|
||||
async def test_webhook_create_cloudhook(hass, webhook_flow_conf):
|
||||
"""Test only a single entry is allowed."""
|
||||
assert await setup.async_setup_component(hass, 'cloud', {})
|
||||
|
||||
async_setup_entry = Mock(return_value=mock_coro(True))
|
||||
async_unload_entry = Mock(return_value=mock_coro(True))
|
||||
|
||||
loader.set_component(hass, 'test_single', MockModule(
|
||||
'test_single',
|
||||
async_setup_entry=async_setup_entry,
|
||||
async_unload_entry=async_unload_entry,
|
||||
async_remove_entry=config_entry_flow.webhook_async_remove_entry,
|
||||
))
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
'test_single', context={'source': config_entries.SOURCE_USER})
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
coro = mock_coro({
|
||||
'cloudhook_url': 'https://example.com'
|
||||
})
|
||||
|
||||
with patch('hass_nabucasa.cloudhooks.Cloudhooks.async_create',
|
||||
return_value=coro) as mock_create, \
|
||||
patch('homeassistant.components.cloud.async_active_subscription',
|
||||
return_value=True), \
|
||||
patch('homeassistant.components.cloud.async_is_logged_in',
|
||||
return_value=True):
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result['flow_id'], {})
|
||||
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result['description_placeholders']['webhook_url'] == \
|
||||
'https://example.com'
|
||||
assert len(mock_create.mock_calls) == 1
|
||||
assert len(async_setup_entry.mock_calls) == 1
|
||||
|
||||
with patch('hass_nabucasa.cloudhooks.Cloudhooks.async_delete',
|
||||
return_value=coro) as mock_delete:
|
||||
|
||||
result = \
|
||||
await hass.config_entries.async_remove(result['result'].entry_id)
|
||||
|
||||
assert len(mock_delete.mock_calls) == 1
|
||||
assert result['require_restart'] is False
|
||||
|
Loading…
x
Reference in New Issue
Block a user