mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
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:
parent
a4dec0b6d2
commit
96657841c8
@ -20,6 +20,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
ATTR_FILENAME = 'filename'
|
ATTR_FILENAME = 'filename'
|
||||||
ATTR_SUBDIR = 'subdir'
|
ATTR_SUBDIR = 'subdir'
|
||||||
ATTR_URL = 'url'
|
ATTR_URL = 'url'
|
||||||
|
ATTR_OVERWRITE = 'overwrite'
|
||||||
|
|
||||||
CONF_DOWNLOAD_DIR = 'download_dir'
|
CONF_DOWNLOAD_DIR = 'download_dir'
|
||||||
|
|
||||||
@ -31,6 +32,7 @@ SERVICE_DOWNLOAD_FILE_SCHEMA = vol.Schema({
|
|||||||
vol.Required(ATTR_URL): cv.url,
|
vol.Required(ATTR_URL): cv.url,
|
||||||
vol.Optional(ATTR_SUBDIR): cv.string,
|
vol.Optional(ATTR_SUBDIR): cv.string,
|
||||||
vol.Optional(ATTR_FILENAME): cv.string,
|
vol.Optional(ATTR_FILENAME): cv.string,
|
||||||
|
vol.Optional(ATTR_OVERWRITE, default=False): cv.boolean,
|
||||||
})
|
})
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema({
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
@ -66,6 +68,8 @@ def setup(hass, config):
|
|||||||
|
|
||||||
filename = service.data.get(ATTR_FILENAME)
|
filename = service.data.get(ATTR_FILENAME)
|
||||||
|
|
||||||
|
overwrite = service.data.get(ATTR_OVERWRITE)
|
||||||
|
|
||||||
if subdir:
|
if subdir:
|
||||||
subdir = sanitize_filename(subdir)
|
subdir = sanitize_filename(subdir)
|
||||||
|
|
||||||
@ -109,12 +113,13 @@ def setup(hass, config):
|
|||||||
|
|
||||||
# If file exist append a number.
|
# If file exist append a number.
|
||||||
# We test filename, filename_2..
|
# We test filename, filename_2..
|
||||||
tries = 1
|
if not overwrite:
|
||||||
final_path = path + ext
|
tries = 1
|
||||||
while os.path.isfile(final_path):
|
final_path = path + ext
|
||||||
tries += 1
|
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)
|
_LOGGER.info("%s -> %s", url, final_path)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user