Add option to overwrite file to the downloader component (#10298)

* Add option to overwrite file to the downloader component

* Cleanup

* Address Paulus's comments
This commit is contained in:
Alok Saboo 2017-11-03 16:02:38 -04:00 committed by Paulus Schoutsen
parent a4dec0b6d2
commit 96657841c8

View File

@ -20,6 +20,7 @@ _LOGGER = logging.getLogger(__name__)
ATTR_FILENAME = 'filename'
ATTR_SUBDIR = 'subdir'
ATTR_URL = 'url'
ATTR_OVERWRITE = 'overwrite'
CONF_DOWNLOAD_DIR = 'download_dir'
@ -31,6 +32,7 @@ SERVICE_DOWNLOAD_FILE_SCHEMA = vol.Schema({
vol.Required(ATTR_URL): cv.url,
vol.Optional(ATTR_SUBDIR): cv.string,
vol.Optional(ATTR_FILENAME): cv.string,
vol.Optional(ATTR_OVERWRITE, default=False): cv.boolean,
})
CONFIG_SCHEMA = vol.Schema({
@ -66,6 +68,8 @@ def setup(hass, config):
filename = service.data.get(ATTR_FILENAME)
overwrite = service.data.get(ATTR_OVERWRITE)
if subdir:
subdir = sanitize_filename(subdir)
@ -109,12 +113,13 @@ def setup(hass, config):
# If file exist append a number.
# We test filename, filename_2..
tries = 1
final_path = path + ext
while os.path.isfile(final_path):
tries += 1
if not overwrite:
tries = 1
final_path = path + ext
while os.path.isfile(final_path):
tries += 1
final_path = "{}_{}.{}".format(path, tries, ext)
final_path = "{}_{}.{}".format(path, tries, ext)
_LOGGER.info("%s -> %s", url, final_path)