Update mailgun (#7984)

* add mailgun component

* add to coveragerc
pull/8004/head
happyleavesaoc 2017-06-12 01:19:10 -04:00 committed by Paulus Schoutsen
parent 401309c3b2
commit 6e33c12008
3 changed files with 64 additions and 14 deletions

View File

@ -77,6 +77,9 @@ omit =
homeassistant/components/lutron_caseta.py
homeassistant/components/*/lutron_caseta.py
homeassistant/components/mailgun.py
homeassistant/components/*/mailgun.py
homeassistant/components/modbus.py
homeassistant/components/*/modbus.py
@ -347,7 +350,6 @@ omit =
homeassistant/components/notify/kodi.py
homeassistant/components/notify/lannouncer.py
homeassistant/components/notify/llamalab_automate.py
homeassistant/components/notify/mailgun.py
homeassistant/components/notify/matrix.py
homeassistant/components/notify/message_bird.py
homeassistant/components/notify/nfandroidtv.py

View File

@ -0,0 +1,51 @@
"""
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
from homeassistant.core import callback
from homeassistant.components.http import HomeAssistantView
DOMAIN = 'mailgun'
API_PATH = '/api/{}'.format(DOMAIN)
DATA_MAILGUN = DOMAIN
DEPENDENCIES = ['http']
MESSAGE_RECEIVED = '{}_message_received'.format(DOMAIN)
CONF_SANDBOX = 'sandbox'
DEFAULT_SANDBOX = False
CONFIG_SCHEMA = vol.Schema({
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
}),
}, extra=vol.ALLOW_EXTRA)
def setup(hass, config):
"""Set up the Mailgun component."""
hass.data[DATA_MAILGUN] = config[DOMAIN]
hass.http.register_view(MailgunReceiveMessageView())
return True
class MailgunReceiveMessageView(HomeAssistantView):
"""Handle data from Mailgun inbound messages."""
url = API_PATH
name = 'api:{}'.format(DOMAIN)
@callback
def post(self, request): # pylint: disable=no-self-use
"""Handle Mailgun message POST."""
hass = request.app['hass']
data = yield from request.post()
hass.bus.async_fire(MESSAGE_RECEIVED, dict(data))
return

View File

@ -8,40 +8,37 @@ import logging
import voluptuous as vol
from homeassistant.components.mailgun import CONF_SANDBOX, DATA_MAILGUN
from homeassistant.components.notify import (
PLATFORM_SCHEMA, BaseNotificationService, ATTR_TITLE, ATTR_TITLE_DEFAULT,
ATTR_DATA)
from homeassistant.const import (
CONF_TOKEN, CONF_DOMAIN, CONF_RECIPIENT, CONF_SENDER)
import homeassistant.helpers.config_validation as cv
CONF_API_KEY, CONF_DOMAIN, CONF_RECIPIENT, CONF_SENDER)
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['mailgun']
REQUIREMENTS = ['pymailgunner==1.4']
# Images to attach to notification
ATTR_IMAGES = 'images'
CONF_SANDBOX = 'sandbox'
DEFAULT_SENDER = 'hass@{domain}'
DEFAULT_SANDBOX = False
# pylint: disable=no-value-for-parameter
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_TOKEN): cv.string,
vol.Required(CONF_RECIPIENT): vol.Email(),
vol.Optional(CONF_DOMAIN): cv.string,
vol.Optional(CONF_SENDER): vol.Email(),
vol.Optional(CONF_SANDBOX, default=DEFAULT_SANDBOX): cv.boolean,
vol.Optional(CONF_SENDER): vol.Email()
})
def get_service(hass, config, discovery_info=None):
"""Get the Mailgun notification service."""
data = hass.data[DATA_MAILGUN]
mailgun_service = MailgunNotificationService(
config.get(CONF_DOMAIN), config.get(CONF_SANDBOX),
config.get(CONF_TOKEN), config.get(CONF_SENDER),
data.get(CONF_DOMAIN), data.get(CONF_SANDBOX),
data.get(CONF_API_KEY), config.get(CONF_SENDER),
config.get(CONF_RECIPIENT))
if mailgun_service.connection_is_valid():
return mailgun_service
@ -52,19 +49,19 @@ def get_service(hass, config, discovery_info=None):
class MailgunNotificationService(BaseNotificationService):
"""Implement a notification service for the Mailgun mail service."""
def __init__(self, domain, sandbox, token, sender, recipient):
def __init__(self, domain, sandbox, api_key, sender, recipient):
"""Initialize the service."""
self._client = None # Mailgun API client
self._domain = domain
self._sandbox = sandbox
self._token = token
self._api_key = api_key
self._sender = sender
self._recipient = recipient
def initialize_client(self):
"""Initialize the connection to Mailgun."""
from pymailgunner import Client
self._client = Client(self._token, self._domain, self._sandbox)
self._client = Client(self._api_key, self._domain, self._sandbox)
_LOGGER.debug("Mailgun domain: %s", self._client.domain)
self._domain = self._client.domain
if not self._sender: