diff --git a/.coveragerc b/.coveragerc index 91d7e64e79b91a0838f13d33133f1d1f78f81ec3..f1631b4e99f1579abc5904f591fef58949ce9533 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 0000000000000000000000000000000000000000..3d426b226458493325cc0bef12bef2727b416138 --- /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 41acf7fcabd3d6b9cd62ad9f6593b412c65cafaf..4c751e03e1f330afa86826d201f2553af912cf08 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