From ef274c691441a228f461fa394cb07e82d2892191 Mon Sep 17 00:00:00 2001 From: Christiaan Blom <g.c.blom14@gmail.com> Date: Tue, 17 Jan 2017 07:58:38 +0100 Subject: [PATCH] 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 --- .coveragerc | 1 + homeassistant/components/notify/discord.py | 51 ++++++++++++++++++++++ requirements_all.txt | 3 ++ 3 files changed, 55 insertions(+) create mode 100644 homeassistant/components/notify/discord.py diff --git a/.coveragerc b/.coveragerc index 91d7e64e79b..f1631b4e99f 100644 --- a/.coveragerc +++ b/.coveragerc @@ -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 diff --git a/homeassistant/components/notify/discord.py b/homeassistant/components/notify/discord.py new file mode 100644 index 00000000000..3d426b22645 --- /dev/null +++ b/homeassistant/components/notify/discord.py @@ -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)) diff --git a/requirements_all.txt b/requirements_all.txt index 41acf7fcabd..4c751e03e1f 100755 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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 -- GitLab