From 09b0defcfc0a3e091b7a2609d046788064ed952e Mon Sep 17 00:00:00 2001 From: cyrosy Date: Wed, 1 May 2019 10:50:33 +0200 Subject: [PATCH 1/4] Move I/O to executor thread pool --- homeassistant/components/discord/notify.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/discord/notify.py b/homeassistant/components/discord/notify.py index 42f54086145a02..d5ace647213df7 100644 --- a/homeassistant/components/discord/notify.py +++ b/homeassistant/components/discord/notify.py @@ -33,6 +33,11 @@ def __init__(self, hass, token): self.token = token self.hass = hass + def file_exists(self, filename): + import os.path + + return os.path.isfile(filename) + async def async_send_message(self, message, **kwargs): """Login to Discord, send message to channel(s) and log out.""" import discord @@ -49,11 +54,14 @@ async def async_send_message(self, message, **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): + image_exists = await self.hass.async_add_executor_job( + self.file_exists, + image) + + if image_exists: images.append(image) else: _LOGGER.warning("Image not found: %s", image) From a5bc3d810de5a11868af09558015e20066fcc0c0 Mon Sep 17 00:00:00 2001 From: cyrosy Date: Wed, 1 May 2019 11:41:57 +0200 Subject: [PATCH 2/4] Check if image is in whitelist_external_dir --- homeassistant/components/discord/notify.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/homeassistant/components/discord/notify.py b/homeassistant/components/discord/notify.py index d5ace647213df7..6016eb68c1ae40 100644 --- a/homeassistant/components/discord/notify.py +++ b/homeassistant/components/discord/notify.py @@ -34,8 +34,12 @@ def __init__(self, hass, token): self.hass = hass def file_exists(self, filename): + """Check if a file exists on disk and is in authorized path.""" import os.path + if not self.hass.config.is_allowed_path(filename): + return False + return os.path.isfile(filename) async def async_send_message(self, message, **kwargs): From 86cdfaf8edaf53b0a1b70374db2d9abd614e8dcc Mon Sep 17 00:00:00 2001 From: cyrosy Date: Wed, 1 May 2019 13:02:59 +0200 Subject: [PATCH 3/4] Move import to top of file --- homeassistant/components/discord/notify.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/homeassistant/components/discord/notify.py b/homeassistant/components/discord/notify.py index 6016eb68c1ae40..dcc048bb8192d0 100644 --- a/homeassistant/components/discord/notify.py +++ b/homeassistant/components/discord/notify.py @@ -1,5 +1,6 @@ """Discord platform for notify component.""" import logging +import os.path import voluptuous as vol @@ -35,8 +36,6 @@ def __init__(self, hass, token): def file_exists(self, filename): """Check if a file exists on disk and is in authorized path.""" - import os.path - if not self.hass.config.is_allowed_path(filename): return False From 1a193b811d63ed67958534d8e5446d230fe3a300 Mon Sep 17 00:00:00 2001 From: cyrosy Date: Wed, 1 May 2019 15:47:24 +0200 Subject: [PATCH 4/4] Fix bad indentation --- homeassistant/components/discord/notify.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/discord/notify.py b/homeassistant/components/discord/notify.py index dcc048bb8192d0..5a9cb77877dc03 100644 --- a/homeassistant/components/discord/notify.py +++ b/homeassistant/components/discord/notify.py @@ -61,8 +61,8 @@ async def async_send_message(self, message, **kwargs): for image in data.get(ATTR_IMAGES): image_exists = await self.hass.async_add_executor_job( - self.file_exists, - image) + self.file_exists, + image) if image_exists: images.append(image)