Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/discord/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Discord",
"documentation": "https://www.home-assistant.io/components/discord",
"requirements": [
"discord.py==0.16.12"
"discord.py==1.0.1"
],
"dependencies": [],
"codeowners": []
Expand Down
41 changes: 32 additions & 9 deletions homeassistant/components/discord/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,53 @@ async def async_send_message(self, message, **kwargs):

discord.VoiceClient.warn_nacl = False
discord_bot = discord.Client(loop=self.hass.loop)
images = None

if ATTR_TARGET not in kwargs:
_LOGGER.error("No target specified")
return None

if ATTR_DATA in kwargs:
data = kwargs.get(ATTR_DATA)

if ATTR_IMAGES in data:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to have caused an unintended breaking change. If data is not in the service call it throws a NoneType error here. At minimum you must have data:{} now in order for the service call to be successful. Reported in issue #23948

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I saw that only after I updated do 0.93, and was waiting for the weekend to fix it

import os.path
images = list()

for image in data.get(ATTR_IMAGES):
if os.path.isfile(image):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does I/O and we're in a coroutine. That combo is not allowed. Use hass.async_add_executor_job to schedule the I/O on the executor thread pool.

images.append(image)
else:
_LOGGER.warning("Image not found: %s", image)

# pylint: disable=unused-variable
@discord_bot.event
async def on_ready():
"""Send the messages when the bot is ready."""
try:
data = kwargs.get(ATTR_DATA)
images = None
if data:
images = data.get(ATTR_IMAGES)
for channelid in kwargs[ATTR_TARGET]:
channel = discord.Object(id=channelid)
await discord_bot.send_message(channel, message)
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
if images:
for anum, f_name in enumerate(images):
await discord_bot.send_file(channel, f_name)
files = list()
for image in images:
files.append(discord.File(image))

await channel.send(message, files=files)
except (discord.errors.HTTPException,
discord.errors.NotFound) as error:
_LOGGER.warning("Communication error: %s", error)
await discord_bot.logout()
await discord_bot.close()

await discord_bot.start(self.token)
# Using reconnect=False prevents multiple ready events to be fired.
await discord_bot.start(self.token, reconnect=False)
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ directpy==0.5
discogs_client==2.2.1

# homeassistant.components.discord
discord.py==0.16.12
discord.py==1.0.1

# homeassistant.components.updater
distro==1.4.0
Expand Down