-
-
Notifications
You must be signed in to change notification settings - Fork 37.2k
Migrate twilio webhooks to the webhook component #17715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
64491e5
Migrate twilio webhooks to the webhook component
rohankapoorcom 5b7f3f5
Fix typos in twilio
rohankapoorcom d40aec0
Mock out twilio in the tests
rohankapoorcom bcaea2d
Lint
rohankapoorcom d36dc43
Fix regression in twilio response
rohankapoorcom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "config": { | ||
| "title": "Twilio", | ||
| "step": { | ||
| "user": { | ||
| "title": "Set up the Twilio Webhook", | ||
| "description": "Are you sure you want to set up Twilio?" | ||
| } | ||
| }, | ||
| "abort": { | ||
| "one_instance_allowed": "Only a single instance is necessary.", | ||
| "not_internet_accessible": "Your Home Assistant instance needs to be accessible from the internet to receive Twilio messages." | ||
| }, | ||
| "create_entry": { | ||
| "default": "To send events to Home Assistant, you will need to setup [Webhooks with Twilio]({twilio_url}).\n\nFill in the following info:\n\n- URL: `{webhook_url}`\n- Method: POST\n- Content Type: application/x-www-form-urlencoded\n\nSee [the documentation]({docs_url}) on how to configure automations to handle incoming data." | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """ | ||
| Support for Twilio. | ||
|
|
||
| For more details about this component, please refer to the documentation at | ||
| https://home-assistant.io/components/twilio/ | ||
| """ | ||
| import voluptuous as vol | ||
|
|
||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.const import CONF_WEBHOOK_ID | ||
| from homeassistant.helpers import config_entry_flow | ||
|
|
||
| REQUIREMENTS = ['twilio==6.19.1'] | ||
| DEPENDENCIES = ['webhook'] | ||
|
|
||
| DOMAIN = 'twilio' | ||
|
|
||
| CONF_ACCOUNT_SID = 'account_sid' | ||
| CONF_AUTH_TOKEN = 'auth_token' | ||
|
|
||
| DATA_TWILIO = DOMAIN | ||
|
|
||
| RECEIVED_DATA = '{}_data_received'.format(DOMAIN) | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema({ | ||
| vol.Optional(DOMAIN): vol.Schema({ | ||
| vol.Required(CONF_ACCOUNT_SID): cv.string, | ||
| vol.Required(CONF_AUTH_TOKEN): cv.string, | ||
| }), | ||
| }, extra=vol.ALLOW_EXTRA) | ||
|
|
||
|
|
||
| async def async_setup(hass, config): | ||
| """Set up the Twilio component.""" | ||
| from twilio.rest import TwilioRestClient | ||
| if DOMAIN not in config: | ||
| return True | ||
|
|
||
| conf = config[DOMAIN] | ||
| hass.data[DATA_TWILIO] = TwilioRestClient( | ||
| conf.get(CONF_ACCOUNT_SID), conf.get(CONF_AUTH_TOKEN)) | ||
| return True | ||
|
|
||
|
|
||
| async def handle_webhook(hass, webhook_id, request): | ||
| """Handle incoming webhook from Twilio for inbound messages and calls.""" | ||
| from twilio.twiml import TwiML | ||
|
|
||
| data = dict(await request.post()) | ||
| data['webhook_id'] = webhook_id | ||
| hass.bus.async_fire(RECEIVED_DATA, dict(data)) | ||
|
|
||
| return TwiML().to_xml() | ||
|
|
||
|
|
||
| async def async_setup_entry(hass, entry): | ||
| """Configure based on config entry.""" | ||
| hass.components.webhook.async_register( | ||
| entry.data[CONF_WEBHOOK_ID], handle_webhook) | ||
| return True | ||
|
|
||
|
|
||
| async def async_unload_entry(hass, entry): | ||
| """Unload a config entry.""" | ||
| hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID]) | ||
| return True | ||
|
|
||
| config_entry_flow.register_webhook_flow( | ||
| DOMAIN, | ||
| 'Twilio Webhook', | ||
| { | ||
| 'twilio_url': | ||
| 'https://www.twilio.com/docs/glossary/what-is-a-webhook', | ||
| 'docs_url': 'https://www.home-assistant.io/components/twilio/' | ||
| } | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "config": { | ||
| "title": "Twilio", | ||
| "step": { | ||
| "user": { | ||
| "title": "Set up the Twilio Webhook", | ||
| "description": "Are you sure you want to set up Twilio?" | ||
| } | ||
| }, | ||
| "abort": { | ||
| "one_instance_allowed": "Only a single instance is necessary.", | ||
| "not_internet_accessible": "Your Home Assistant instance needs to be accessible from the internet to receive Twilio messages." | ||
| }, | ||
| "create_entry": { | ||
| "default": "To send events to Home Assistant, you will need to setup [Webhooks with Twilio]({twilio_url}).\n\nFill in the following info:\n\n- URL: `{webhook_url}`\n- Method: POST\n- Content Type: application/x-www-form-urlencoded\n\nSee [the documentation]({docs_url}) on how to configure automations to handle incoming data." | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Tests for the Twilio component.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """Test the init file of Twilio.""" | ||
| from unittest.mock import patch | ||
|
|
||
| from homeassistant import data_entry_flow | ||
| from homeassistant.components import twilio | ||
| from homeassistant.core import callback | ||
| from tests.common import MockDependency | ||
|
|
||
|
|
||
| @MockDependency('twilio', 'rest') | ||
| @MockDependency('twilio', 'twiml') | ||
| async def test_config_flow_registers_webhook(hass, aiohttp_client): | ||
| """Test setting up Twilio and sending webhook.""" | ||
| with patch('homeassistant.util.get_local_ip', return_value='example.com'): | ||
| result = await hass.config_entries.flow.async_init('twilio', context={ | ||
| 'source': 'user' | ||
| }) | ||
| assert result['type'] == data_entry_flow.RESULT_TYPE_FORM, result | ||
|
|
||
| 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'] | ||
|
|
||
| twilio_events = [] | ||
|
|
||
| @callback | ||
| def handle_event(event): | ||
| """Handle Twilio event.""" | ||
| twilio_events.append(event) | ||
|
|
||
| hass.bus.async_listen(twilio.RECEIVED_DATA, handle_event) | ||
|
|
||
| client = await aiohttp_client(hass.http.app) | ||
| await client.post('/api/webhook/{}'.format(webhook_id), data={ | ||
| 'hello': 'twilio' | ||
| }) | ||
|
|
||
| assert len(twilio_events) == 1 | ||
| assert twilio_events[0].data['webhook_id'] == webhook_id | ||
| assert twilio_events[0].data['hello'] == 'twilio' | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.