From 07f20676cbcd54771b39f62bb0e4aaa1fe8f4360 Mon Sep 17 00:00:00 2001
From: engrbm87 <engrbm87@gmail.com>
Date: Thu, 15 Mar 2018 01:03:40 +0200
Subject: [PATCH] Add notifications to downloader.py (#12961)

* Update downloader.py

Add persistent notification to alert when download is finished or in case of download failure.

* Update downloader.py

* Update downloader.py

* Update downloader.py

* Fire and event when download is requested

Added 2 events to represent download completed and download failed. This will allow the user to trigger an automation based on the status of the download.

* Update downloader.py

* Update downloader.py

replaced . with _

* Update downloader.py

fixed linting errors
---
 homeassistant/components/downloader.py | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/homeassistant/components/downloader.py b/homeassistant/components/downloader.py
index b7354b4f0a7..0d57740a83d 100644
--- a/homeassistant/components/downloader.py
+++ b/homeassistant/components/downloader.py
@@ -25,6 +25,8 @@ ATTR_OVERWRITE = 'overwrite'
 CONF_DOWNLOAD_DIR = 'download_dir'
 
 DOMAIN = 'downloader'
+DOWNLOAD_FAILED_EVENT = 'download_failed'
+DOWNLOAD_COMPLETED_EVENT = 'download_completed'
 
 SERVICE_DOWNLOAD_FILE = 'download_file'
 
@@ -133,9 +135,19 @@ def setup(hass, config):
                             fil.write(chunk)
 
                     _LOGGER.debug("Downloading of %s done", url)
+                    hass.bus.fire(
+                        "{}_{}".format(DOMAIN, DOWNLOAD_COMPLETED_EVENT), {
+                            'url': url,
+                            'filename': filename
+                            })
 
             except requests.exceptions.ConnectionError:
                 _LOGGER.exception("ConnectionError occurred for %s", url)
+                hass.bus.fire(
+                    "{}_{}".format(DOMAIN, DOWNLOAD_FAILED_EVENT), {
+                        'url': url,
+                        'filename': filename
+                        })
 
                 # Remove file if we started downloading but failed
                 if final_path and os.path.isfile(final_path):
-- 
GitLab