1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-07 13:52:20 +01:00

[external_files] Move common `download_content function to external_files.py` (#6982)

This commit is contained in:
Jesse Hills
2024-06-25 17:42:55 +12:00
committed by GitHub
parent 11b8e2e1af
commit 8a25bedaf9
3 changed files with 30 additions and 57 deletions

View File

@@ -7,6 +7,7 @@ from datetime import datetime
import requests
import esphome.config_validation as cv
from esphome.core import CORE, TimePeriodSeconds
from esphome.const import __version__
_LOGGER = logging.getLogger(__name__)
CODEOWNERS = ["@landonr"]
@@ -75,3 +76,28 @@ def compute_local_file_dir(domain: str) -> Path:
base_directory.mkdir(parents=True, exist_ok=True)
return base_directory
def download_content(url: str, path: Path, timeout=NETWORK_TIMEOUT) -> None:
if not has_remote_file_changed(url, path):
_LOGGER.debug("Remote file has not changed %s", url)
return
_LOGGER.debug(
"Remote file has changed, downloading from %s to %s",
url,
path,
)
try:
req = requests.get(
url,
timeout=timeout,
headers={"User-agent": f"ESPHome/{__version__} (https://esphome.io)"},
)
req.raise_for_status()
except requests.exceptions.RequestException as e:
raise cv.Invalid(f"Could not download from {url}: {e}")
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(req.content)