From 415ed9ce0b33a47addf72956d10d9839370729bd Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Tue, 22 Aug 2017 09:12:44 +0200 Subject: [PATCH 1/3] Catch exceptions --- homeassistant/components/notify/discord.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/notify/discord.py b/homeassistant/components/notify/discord.py index 691ff158012ef..62319f71a8ffb 100644 --- a/homeassistant/components/notify/discord.py +++ b/homeassistant/components/notify/discord.py @@ -4,9 +4,11 @@ For more details about this platform, please refer to the documentation at https://home-assistant.io/components/notify.discord/ """ -import logging import asyncio +import logging + import voluptuous as vol + import homeassistant.helpers.config_validation as cv from homeassistant.components.notify import ( PLATFORM_SCHEMA, BaseNotificationService, ATTR_TARGET) @@ -42,13 +44,21 @@ def async_send_message(self, message, **kwargs): import discord discord_bot = discord.Client(loop=self.hass.loop) + # pylint: disable=unused-variable @discord_bot.event @asyncio.coroutine - def on_ready(): # pylint: disable=unused-variable + def on_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) + try: + for channelid in kwargs[ATTR_TARGET]: + channel = discord.Object(id=channelid) + yield from discord_bot.send_message(channel, message) + except (discord.errors.HTTPException, + discord.errors.NotFound) as error: + _LOGGER.warning("Communication error: %s", error) + # pylint: disable=too-broad-exception + except KeyError: + _LOGGER.warning("No target specified") yield from discord_bot.logout() yield from discord_bot.close() From c8f0b2f485eaa0487611ad020cc1f8b328d088b6 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Tue, 22 Aug 2017 10:27:35 +0200 Subject: [PATCH 2/3] Fix pylint disable --- 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 62319f71a8ffb..a9af1df49f90d 100644 --- a/homeassistant/components/notify/discord.py +++ b/homeassistant/components/notify/discord.py @@ -56,7 +56,7 @@ def on_ready(): except (discord.errors.HTTPException, discord.errors.NotFound) as error: _LOGGER.warning("Communication error: %s", error) - # pylint: disable=too-broad-exception + # pylint: disable=broad-except except KeyError: _LOGGER.warning("No target specified") yield from discord_bot.logout() From f024abbf5f1fa42734fc2d621eee235119a8d1b0 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Tue, 22 Aug 2017 23:47:16 +0200 Subject: [PATCH 3/3] Move check for emtpy target list --- homeassistant/components/notify/discord.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/notify/discord.py b/homeassistant/components/notify/discord.py index a9af1df49f90d..003d657fcd47b 100644 --- a/homeassistant/components/notify/discord.py +++ b/homeassistant/components/notify/discord.py @@ -44,6 +44,10 @@ def async_send_message(self, message, **kwargs): import discord discord_bot = discord.Client(loop=self.hass.loop) + if ATTR_TARGET not in kwargs: + _LOGGER.error("No target specified") + return None + # pylint: disable=unused-variable @discord_bot.event @asyncio.coroutine @@ -56,9 +60,6 @@ def on_ready(): except (discord.errors.HTTPException, discord.errors.NotFound) as error: _LOGGER.warning("Communication error: %s", error) - # pylint: disable=broad-except - except KeyError: - _LOGGER.warning("No target specified") yield from discord_bot.logout() yield from discord_bot.close()