Added capability to pass a filename to the downloader component (#10059)

* Added capability to pass the filename to the downloader component

* Simplified filename conditions
pull/10178/merge
Marcelo Moreira de Mello 2017-10-27 16:50:02 -04:00 committed by Fabian Affolter
parent 5c168ab551
commit f060dcc0aa
1 changed files with 7 additions and 4 deletions

View File

@ -17,6 +17,7 @@ from homeassistant.util import sanitize_filename
_LOGGER = logging.getLogger(__name__)
ATTR_FILENAME = 'filename'
ATTR_SUBDIR = 'subdir'
ATTR_URL = 'url'
@ -29,6 +30,7 @@ SERVICE_DOWNLOAD_FILE = 'download_file'
SERVICE_DOWNLOAD_FILE_SCHEMA = vol.Schema({
vol.Required(ATTR_URL): cv.url,
vol.Optional(ATTR_SUBDIR): cv.string,
vol.Optional(ATTR_FILENAME): cv.string,
})
CONFIG_SCHEMA = vol.Schema({
@ -62,6 +64,8 @@ def setup(hass, config):
subdir = service.data.get(ATTR_SUBDIR)
filename = service.data.get(ATTR_FILENAME)
if subdir:
subdir = sanitize_filename(subdir)
@ -70,9 +74,9 @@ def setup(hass, config):
req = requests.get(url, stream=True, timeout=10)
if req.status_code == 200:
filename = None
if 'content-disposition' in req.headers:
if filename is None and \
'content-disposition' in req.headers:
match = re.findall(r"filename=(\S+)",
req.headers['content-disposition'])
@ -80,8 +84,7 @@ def setup(hass, config):
filename = match[0].strip("'\" ")
if not filename:
filename = os.path.basename(
url).strip()
filename = os.path.basename(url).strip()
if not filename:
filename = 'ha_download'