-
-
Notifications
You must be signed in to change notification settings - Fork 37.9k
Migrate Mailgun to use the webhook component #17464
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 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a9a0a79
Switch mailgun to use webhook api
rohankapoorcom 1ad5dad
Generalize webhook_config_entry_flow
rohankapoorcom 64b6fa8
Add tests for webhook_config_entry_flow
rohankapoorcom bfc03b5
Add tests for mailgun
rohankapoorcom 5a1cd6d
Remove old mailgun file from .coveragerc
rohankapoorcom 4981223
Refactor WebhookFlowHandler into config_entry_flow
rohankapoorcom 8af38cb
Remove test of helper func from IFTTT
balloob f1c734b
Lint
balloob 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": "Mailgun", | ||
| "step": { | ||
| "user": { | ||
| "title": "Set up the Mailgun Webhook", | ||
| "description": "Are you sure you want to set up Mailgun?" | ||
| } | ||
| }, | ||
| "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 Mailgun messages." | ||
| }, | ||
| "create_entry": { | ||
| "default": "To send events to Home Assistant, you will need to setup [Webhooks with Mailgun]({mailgun_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,69 @@ | ||
| """ | ||
| Support for Mailgun. | ||
|
|
||
| For more details about this component, please refer to the documentation at | ||
| https://home-assistant.io/components/mailgun/ | ||
| """ | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.const import CONF_API_KEY, CONF_DOMAIN, CONF_WEBHOOK_ID | ||
| from homeassistant.helpers import webhook_config_entry_flow | ||
|
|
||
| DOMAIN = 'mailgun' | ||
| API_PATH = '/api/{}'.format(DOMAIN) | ||
| DEPENDENCIES = ['webhook'] | ||
| MESSAGE_RECEIVED = '{}_message_received'.format(DOMAIN) | ||
| CONF_SANDBOX = 'sandbox' | ||
| DEFAULT_SANDBOX = False | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema({ | ||
| vol.Optional(DOMAIN): vol.Schema({ | ||
| vol.Required(CONF_API_KEY): cv.string, | ||
| vol.Required(CONF_DOMAIN): cv.string, | ||
| vol.Optional(CONF_SANDBOX, default=DEFAULT_SANDBOX): cv.boolean, | ||
| vol.Optional(CONF_WEBHOOK_ID): cv.string, | ||
| }), | ||
| }, extra=vol.ALLOW_EXTRA) | ||
|
|
||
|
|
||
| async def async_setup(hass, config): | ||
| """Set up the Mailgun component.""" | ||
| if DOMAIN not in config: | ||
| return True | ||
|
|
||
| hass.data[DOMAIN] = config[DOMAIN] | ||
| return True | ||
|
|
||
|
|
||
| async def handle_webhook(hass, webhook_id, request): | ||
| """Handle incoming webhook with Mailgun inbound messages.""" | ||
| data = await request.post() | ||
|
|
||
| if isinstance(data, dict): | ||
| data[CONF_WEBHOOK_ID] = webhook_id | ||
| hass.bus.async_fire(MESSAGE_RECEIVED, data) | ||
|
|
||
|
|
||
| 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 | ||
|
|
||
| webhook_config_entry_flow.register_webhook_flow( | ||
| DOMAIN, | ||
| 'Mailgun Webhook', | ||
| { | ||
| 'mailgun_url': | ||
| 'https://www.mailgun.com/blog/a-guide-to-using-mailguns-webhooks', | ||
| 'docs_url': 'https://www.home-assistant.io/components/mailgun/' | ||
| } | ||
| ) |
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": "Mailgun", | ||
| "step": { | ||
| "user": { | ||
| "title": "Set up the Mailgun Webhook", | ||
| "description": "Are you sure you want to set up Mailgun?" | ||
| } | ||
| }, | ||
| "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 Mailgun messages." | ||
| }, | ||
| "create_entry": { | ||
| "default": "To send events to Home Assistant, you will need to setup [Webhooks with Mailgun]({mailgun_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
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,61 @@ | ||
| """Helpers for data entry flows for config entries that use webhooks.""" | ||
| from functools import partial | ||
| from urllib.parse import urlparse | ||
|
|
||
| from ipaddress import ip_address | ||
|
|
||
| from homeassistant import config_entries | ||
| from homeassistant.util.network import is_local | ||
|
|
||
|
|
||
| def register_webhook_flow(domain, title, description_placeholder): | ||
| """Register flow for webhook integrations..""" | ||
| config_entries.HANDLERS.register(domain)( | ||
| partial(WebhookFlowHandler, domain, title, description_placeholder)) | ||
|
|
||
|
|
||
| class WebhookFlowHandler(config_entries.ConfigFlow): | ||
| """Handle a webhook config flow.""" | ||
|
|
||
| VERSION = 1 | ||
|
|
||
| def __init__(self, domain, title, description_placeholder, | ||
| allow_multiple=False): | ||
| """Initialize the discovery config flow.""" | ||
| self._domain = domain | ||
| self._title = title | ||
| self._description_placeholder = description_placeholder | ||
| self._allow_multiple = allow_multiple | ||
|
|
||
| async def async_step_user(self, user_input=None): | ||
| """Handle a user initiated set up flow to create a webhook.""" | ||
| if not self._allow_multiple and self._async_current_entries(): | ||
| return self.async_abort(reason='one_instance_allowed') | ||
|
|
||
| try: | ||
| url_parts = urlparse(self.hass.config.api.base_url) | ||
|
|
||
| if is_local(ip_address(url_parts.hostname)): | ||
| return self.async_abort(reason='not_internet_accessible') | ||
| except ValueError: | ||
| # If it's not an IP address, it's very likely publicly accessible | ||
| pass | ||
|
|
||
| if user_input is None: | ||
| return self.async_show_form( | ||
| step_id='user', | ||
| ) | ||
|
|
||
| webhook_id = self.hass.components.webhook.async_generate_id() | ||
| webhook_url = \ | ||
| self.hass.components.webhook.async_generate_url(webhook_id) | ||
|
|
||
| self._description_placeholder['webhook_url'] = webhook_url | ||
|
|
||
| return self.async_create_entry( | ||
| title=self._title, | ||
| data={ | ||
| 'webhook_id': webhook_id | ||
| }, | ||
| description_placeholders=self._description_placeholder | ||
| ) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you merge this file with
helpers/config_entry_flow.py