New Discord notification component (#5330)
* Initial commit of discord notification component * Fixed error where script added extra entries to .coveragerc * Cleaned up code * Compliance to PEP8 * removed dependencies * readded dependencies * changed name of client id to token for configuration * Changes for Hound * Incorporated Review Feedback * Review feedback * Updated requirements file * Check compliance
This commit is contained in:
parent
b915cf776b
commit
ef274c6914
3 changed files with 55 additions and 0 deletions
|
@ -231,6 +231,7 @@ omit =
|
|||
homeassistant/components/notify/aws_lambda.py
|
||||
homeassistant/components/notify/aws_sns.py
|
||||
homeassistant/components/notify/aws_sqs.py
|
||||
homeassistant/components/notify/discord.py
|
||||
homeassistant/components/notify/facebook.py
|
||||
homeassistant/components/notify/free_mobile.py
|
||||
homeassistant/components/notify/gntp.py
|
||||
|
|
51
homeassistant/components/notify/discord.py
Normal file
51
homeassistant/components/notify/discord.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
"""Discord platform for notify component."""
|
||||
import logging
|
||||
import asyncio
|
||||
import voluptuous as vol
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.notify import (
|
||||
PLATFORM_SCHEMA, BaseNotificationService)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
REQUIREMENTS = ['discord.py==0.16.0']
|
||||
|
||||
CONF_TOKEN = 'token'
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_TOKEN): cv.string
|
||||
})
|
||||
|
||||
|
||||
def get_service(hass, config, discovery_info=None):
|
||||
"""Get the Discord notification service."""
|
||||
token = config.get(CONF_TOKEN)
|
||||
return DiscordNotificationService(hass, token)
|
||||
|
||||
|
||||
class DiscordNotificationService(BaseNotificationService):
|
||||
"""Implement the notification service for Discord."""
|
||||
|
||||
def __init__(self, hass, token):
|
||||
"""Initialize the service."""
|
||||
self.token = token
|
||||
self.hass = hass
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_send_message(self, message, target):
|
||||
"""Login to Discord, send message to channel(s) and log out."""
|
||||
import discord
|
||||
discord_bot = discord.Client(loop=self.hass.loop)
|
||||
|
||||
yield from discord_bot.login(self.token)
|
||||
|
||||
for channelid in target:
|
||||
channel = discord.Object(id=channelid)
|
||||
yield from discord_bot.send_message(channel, message)
|
||||
|
||||
yield from discord_bot.logout()
|
||||
yield from discord_bot.close()
|
||||
|
||||
def send_message(self, message=None, target=None, **kwargs):
|
||||
"""Send a message using Discord."""
|
||||
self.hass.async_add_job(self.async_send_message(message, target))
|
|
@ -87,6 +87,9 @@ denonavr==0.3.0
|
|||
# homeassistant.components.media_player.directv
|
||||
directpy==0.1
|
||||
|
||||
# homeassistant.components.notify.discord
|
||||
discord.py==0.16.0
|
||||
|
||||
# homeassistant.components.updater
|
||||
distro==1.0.2
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue