2019-04-03 15:40:03 +00:00
|
|
|
"""Discord platform for notify component."""
|
2017-08-23 05:21:09 +00:00
|
|
|
import logging
|
|
|
|
|
2017-01-17 06:58:38 +00:00
|
|
|
import voluptuous as vol
|
2017-08-23 05:21:09 +00:00
|
|
|
|
2017-10-29 16:28:07 +00:00
|
|
|
from homeassistant.const import CONF_TOKEN
|
2019-03-21 05:56:46 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2019-03-28 03:36:13 +00:00
|
|
|
from homeassistant.components.notify import (ATTR_DATA, ATTR_TARGET,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
BaseNotificationService)
|
2017-01-17 06:58:38 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_TOKEN): cv.string
|
|
|
|
})
|
|
|
|
|
2018-09-02 16:58:31 +00:00
|
|
|
ATTR_IMAGES = 'images'
|
|
|
|
|
2017-01-17 06:58:38 +00:00
|
|
|
|
|
|
|
def get_service(hass, config, discovery_info=None):
|
|
|
|
"""Get the Discord notification service."""
|
|
|
|
token = config.get(CONF_TOKEN)
|
|
|
|
return DiscordNotificationService(hass, token)
|
|
|
|
|
2017-03-04 00:31:19 +00:00
|
|
|
|
2017-01-17 06:58:38 +00:00
|
|
|
class DiscordNotificationService(BaseNotificationService):
|
|
|
|
"""Implement the notification service for Discord."""
|
|
|
|
|
|
|
|
def __init__(self, hass, token):
|
|
|
|
"""Initialize the service."""
|
|
|
|
self.token = token
|
|
|
|
self.hass = hass
|
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_send_message(self, message, **kwargs):
|
2017-01-17 06:58:38 +00:00
|
|
|
"""Login to Discord, send message to channel(s) and log out."""
|
|
|
|
import discord
|
2018-11-11 22:16:23 +00:00
|
|
|
|
|
|
|
discord.VoiceClient.warn_nacl = False
|
2017-01-17 06:58:38 +00:00
|
|
|
discord_bot = discord.Client(loop=self.hass.loop)
|
2019-04-30 20:12:39 +00:00
|
|
|
images = None
|
2017-03-04 00:03:10 +00:00
|
|
|
|
2017-08-23 05:21:09 +00:00
|
|
|
if ATTR_TARGET not in kwargs:
|
|
|
|
_LOGGER.error("No target specified")
|
|
|
|
return None
|
|
|
|
|
2019-04-30 20:12:39 +00:00
|
|
|
if ATTR_DATA in kwargs:
|
|
|
|
data = kwargs.get(ATTR_DATA)
|
|
|
|
|
|
|
|
if ATTR_IMAGES in data:
|
|
|
|
import os.path
|
|
|
|
images = list()
|
|
|
|
|
|
|
|
for image in data.get(ATTR_IMAGES):
|
|
|
|
if os.path.isfile(image):
|
|
|
|
images.append(image)
|
|
|
|
else:
|
|
|
|
_LOGGER.warning("Image not found: %s", image)
|
|
|
|
|
2017-08-23 05:21:09 +00:00
|
|
|
# pylint: disable=unused-variable
|
2017-03-03 22:15:03 +00:00
|
|
|
@discord_bot.event
|
2018-10-01 06:58:21 +00:00
|
|
|
async def on_ready():
|
2017-03-04 00:43:59 +00:00
|
|
|
"""Send the messages when the bot is ready."""
|
2017-08-23 05:21:09 +00:00
|
|
|
try:
|
|
|
|
for channelid in kwargs[ATTR_TARGET]:
|
2019-04-30 20:12:39 +00:00
|
|
|
channelid = int(channelid)
|
|
|
|
channel = discord_bot.get_channel(channelid)
|
|
|
|
|
|
|
|
if channel is None:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Channel not found for id: %s",
|
|
|
|
channelid)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Must create new instances of File for each channel.
|
|
|
|
files = None
|
2018-09-02 16:58:31 +00:00
|
|
|
if images:
|
2019-04-30 20:12:39 +00:00
|
|
|
files = list()
|
|
|
|
for image in images:
|
|
|
|
files.append(discord.File(image))
|
|
|
|
|
|
|
|
await channel.send(message, files=files)
|
2017-08-23 05:21:09 +00:00
|
|
|
except (discord.errors.HTTPException,
|
|
|
|
discord.errors.NotFound) as error:
|
|
|
|
_LOGGER.warning("Communication error: %s", error)
|
2018-10-01 06:58:21 +00:00
|
|
|
await discord_bot.logout()
|
|
|
|
await discord_bot.close()
|
2017-01-17 06:58:38 +00:00
|
|
|
|
2019-04-30 20:12:39 +00:00
|
|
|
# Using reconnect=False prevents multiple ready events to be fired.
|
|
|
|
await discord_bot.start(self.token, reconnect=False)
|