mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Catch exceptions (#9085)
* Catch exceptions * Fix pylint disable * Move check for emtpy target list
This commit is contained in:
parent
55a44b0a1c
commit
2355216f61
@ -4,9 +4,11 @@ Discord platform for notify component.
|
|||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/notify.discord/
|
https://home-assistant.io/components/notify.discord/
|
||||||
"""
|
"""
|
||||||
import logging
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.components.notify import (
|
from homeassistant.components.notify import (
|
||||||
PLATFORM_SCHEMA, BaseNotificationService, ATTR_TARGET)
|
PLATFORM_SCHEMA, BaseNotificationService, ATTR_TARGET)
|
||||||
@ -42,13 +44,22 @@ class DiscordNotificationService(BaseNotificationService):
|
|||||||
import discord
|
import discord
|
||||||
discord_bot = discord.Client(loop=self.hass.loop)
|
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
|
@discord_bot.event
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def on_ready(): # pylint: disable=unused-variable
|
def on_ready():
|
||||||
"""Send the messages when the bot is ready."""
|
"""Send the messages when the bot is ready."""
|
||||||
for channelid in kwargs[ATTR_TARGET]:
|
try:
|
||||||
channel = discord.Object(id=channelid)
|
for channelid in kwargs[ATTR_TARGET]:
|
||||||
yield from discord_bot.send_message(channel, message)
|
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.logout()
|
||||||
yield from discord_bot.close()
|
yield from discord_bot.close()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user