Add Flock notification platform (#14533)

* Add Flock notification platform

* Use async syntax and move session and loop
This commit is contained in:
Fabian Affolter 2018-05-31 23:07:50 +02:00 committed by GitHub
parent 60f692c7bb
commit 14ee6178f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 1 deletions

View File

@ -523,9 +523,10 @@ omit =
homeassistant/components/notify/aws_sqs.py
homeassistant/components/notify/ciscospark.py
homeassistant/components/notify/clickatell.py
homeassistant/components/notify/clicksend_tts.py
homeassistant/components/notify/clicksend.py
homeassistant/components/notify/clicksend_tts.py
homeassistant/components/notify/discord.py
homeassistant/components/notify/flock.py
homeassistant/components/notify/free_mobile.py
homeassistant/components/notify/gntp.py
homeassistant/components/notify/group.py

View File

@ -0,0 +1,61 @@
"""
Flock platform for notify component.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.flock/
"""
import asyncio
import logging
import async_timeout
import voluptuous as vol
from homeassistant.components.notify import (
PLATFORM_SCHEMA, BaseNotificationService)
from homeassistant.const import CONF_ACCESS_TOKEN
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
_RESOURCE = 'https://api.flock.com/hooks/sendMessage/'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_ACCESS_TOKEN): cv.string,
})
async def get_service(hass, config, discovery_info=None):
"""Get the Flock notification service."""
access_token = config.get(CONF_ACCESS_TOKEN)
url = '{}{}'.format(_RESOURCE, access_token)
session = async_get_clientsession(hass)
return FlockNotificationService(url, session, hass.loop)
class FlockNotificationService(BaseNotificationService):
"""Implement the notification service for Flock."""
def __init__(self, url, session, loop):
"""Initialize the Flock notification service."""
self._loop = loop
self._url = url
self._session = session
async def async_send_message(self, message, **kwargs):
"""Send the message to the user."""
payload = {'text': message}
_LOGGER.debug("Attempting to call Flock at %s", self._url)
try:
with async_timeout.timeout(10, loop=self._loop):
response = await self._session.post(self._url, json=payload)
result = await response.json()
if response.status != 200 or 'error' in result:
_LOGGER.error(
"Flock service returned HTTP status %d, response %s",
response.status, result)
except asyncio.TimeoutError:
_LOGGER.error("Timeout accessing Flock at %s", self._url)