From b038a1650e96773ecea7e9378b8e445f3cec676a Mon Sep 17 00:00:00 2001 From: Christiaan Blom Date: Fri, 3 Mar 2017 23:15:03 +0100 Subject: [PATCH 1/6] Resolved issue #5688 --- homeassistant/components/notify/discord.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/notify/discord.py b/homeassistant/components/notify/discord.py index e6c4b3bad96..fa8aa72cef3 100644 --- a/homeassistant/components/notify/discord.py +++ b/homeassistant/components/notify/discord.py @@ -35,18 +35,21 @@ class DiscordNotificationService(BaseNotificationService): """Initialize the service.""" self.token = token self.hass = hass + self.loggedin = 0 @asyncio.coroutine def async_send_message(self, message, **kwargs): """Login to Discord, send message to channel(s) and log out.""" import discord discord_bot = discord.Client(loop=self.hass.loop) + + @discord_bot.event + @asyncio.coroutine + def on_ready(): + for channelid in kwargs[ATTR_TARGET]: + channel = discord.Object(id=channelid) + yield from discord_bot.send_message(channel, message) + yield from discord_bot.logout() - yield from discord_bot.login(self.token) + yield from discord_bot.start(self.token) - for channelid in kwargs[ATTR_TARGET]: - channel = discord.Object(id=channelid) - yield from discord_bot.send_message(channel, message) - - yield from discord_bot.logout() - yield from discord_bot.close() From a444df3fdea41cb8be7e80a71c12832a830b501f Mon Sep 17 00:00:00 2001 From: Christiaan Blom Date: Sat, 4 Mar 2017 01:03:10 +0100 Subject: [PATCH 2/6] tweaks --- homeassistant/components/notify/discord.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/homeassistant/components/notify/discord.py b/homeassistant/components/notify/discord.py index fa8aa72cef3..07b595b3945 100644 --- a/homeassistant/components/notify/discord.py +++ b/homeassistant/components/notify/discord.py @@ -27,7 +27,6 @@ def get_service(hass, config, discovery_info=None): token = config.get(CONF_TOKEN) return DiscordNotificationService(hass, token) - class DiscordNotificationService(BaseNotificationService): """Implement the notification service for Discord.""" @@ -35,14 +34,13 @@ class DiscordNotificationService(BaseNotificationService): """Initialize the service.""" self.token = token self.hass = hass - self.loggedin = 0 @asyncio.coroutine def async_send_message(self, message, **kwargs): """Login to Discord, send message to channel(s) and log out.""" import discord discord_bot = discord.Client(loop=self.hass.loop) - + @discord_bot.event @asyncio.coroutine def on_ready(): From 887b53b794c409d1b6cf287f038e90b6502066c7 Mon Sep 17 00:00:00 2001 From: Christiaan Blom Date: Sat, 4 Mar 2017 01:31:19 +0100 Subject: [PATCH 3/6] Changes for Travis bot. Unused variable 'on_ready' will likely remain reported --- homeassistant/components/notify/discord.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/notify/discord.py b/homeassistant/components/notify/discord.py index 07b595b3945..8647ea8792e 100644 --- a/homeassistant/components/notify/discord.py +++ b/homeassistant/components/notify/discord.py @@ -27,6 +27,7 @@ def get_service(hass, config, discovery_info=None): token = config.get(CONF_TOKEN) return DiscordNotificationService(hass, token) + class DiscordNotificationService(BaseNotificationService): """Implement the notification service for Discord.""" @@ -44,10 +45,10 @@ class DiscordNotificationService(BaseNotificationService): @discord_bot.event @asyncio.coroutine def on_ready(): + """sends the messages when the bot is ready""" for channelid in kwargs[ATTR_TARGET]: channel = discord.Object(id=channelid) yield from discord_bot.send_message(channel, message) yield from discord_bot.logout() yield from discord_bot.start(self.token) - From c1f3ce78e13c4dea98fb762ea7b86e7e4059a94e Mon Sep 17 00:00:00 2001 From: Christiaan Blom Date: Sat, 4 Mar 2017 01:43:59 +0100 Subject: [PATCH 4/6] Edit docstring --- homeassistant/components/notify/discord.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/notify/discord.py b/homeassistant/components/notify/discord.py index 8647ea8792e..7e1a83cdcb4 100644 --- a/homeassistant/components/notify/discord.py +++ b/homeassistant/components/notify/discord.py @@ -45,7 +45,7 @@ class DiscordNotificationService(BaseNotificationService): @discord_bot.event @asyncio.coroutine def on_ready(): - """sends the messages when the bot is ready""" + """Send the messages when the bot is ready.""" for channelid in kwargs[ATTR_TARGET]: channel = discord.Object(id=channelid) yield from discord_bot.send_message(channel, message) From b952cfe705c54f79d101ad425a1daa7dc7046271 Mon Sep 17 00:00:00 2001 From: Christiaan Blom Date: Fri, 10 Mar 2017 00:21:26 +0100 Subject: [PATCH 5/6] Added pylint ignore message and re-added .close() --- homeassistant/components/notify/discord.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/notify/discord.py b/homeassistant/components/notify/discord.py index 7e1a83cdcb4..e061492801b 100644 --- a/homeassistant/components/notify/discord.py +++ b/homeassistant/components/notify/discord.py @@ -44,11 +44,12 @@ class DiscordNotificationService(BaseNotificationService): @discord_bot.event @asyncio.coroutine - def on_ready(): + def on_ready(): # pylint: disable=unused-variable """Send the messages when the bot is ready.""" for channelid in kwargs[ATTR_TARGET]: channel = discord.Object(id=channelid) yield from discord_bot.send_message(channel, message) yield from discord_bot.logout() + yield from discord_bot.close() yield from discord_bot.start(self.token) From 3f7dd7ed9a2377be40f1a458881d8ace116921e9 Mon Sep 17 00:00:00 2001 From: Christiaan Blom Date: Fri, 10 Mar 2017 00:33:29 +0100 Subject: [PATCH 6/6] Added an extra space --- homeassistant/components/notify/discord.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/notify/discord.py b/homeassistant/components/notify/discord.py index e061492801b..189aa0d02bb 100644 --- a/homeassistant/components/notify/discord.py +++ b/homeassistant/components/notify/discord.py @@ -44,7 +44,7 @@ class DiscordNotificationService(BaseNotificationService): @discord_bot.event @asyncio.coroutine - def on_ready(): # pylint: disable=unused-variable + def on_ready(): # pylint: disable=unused-variable """Send the messages when the bot is ready.""" for channelid in kwargs[ATTR_TARGET]: channel = discord.Object(id=channelid)