From 2355216f61674371432ffd40f900d6ca09a1751e Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Wed, 23 Aug 2017 07:21:09 +0200 Subject: [PATCH] Catch exceptions (#9085) * Catch exceptions * Fix pylint disable * Move check for emtpy target list --- homeassistant/components/notify/discord.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/notify/discord.py b/homeassistant/components/notify/discord.py index 7a8e51a9a8b..a4ce304167f 100644 --- a/homeassistant/components/notify/discord.py +++ b/homeassistant/components/notify/discord.py @@ -4,9 +4,11 @@ Discord platform for notify component. 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,22 @@ class DiscordNotificationService(BaseNotificationService): 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 - 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) yield from discord_bot.logout() yield from discord_bot.close()