[micro_wake_word] Remove duplicated download code (#7401)

This commit is contained in:
Jesse Hills 2024-09-05 12:49:01 +12:00 committed by GitHub
parent 71a7f6383f
commit dc4e60526c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 28 deletions

View file

@ -4,8 +4,6 @@ import logging
from pathlib import Path
from urllib.parse import urljoin
import requests
from esphome import automation, external_files, git
from esphome.automation import register_action, register_condition
import esphome.codegen as cg
@ -26,7 +24,6 @@ from esphome.const import (
CONF_USERNAME,
TYPE_GIT,
TYPE_LOCAL,
__version__,
)
from esphome.core import CORE, HexInt
@ -179,26 +176,6 @@ def _convert_manifest_v1_to_v2(v1_manifest):
return v2_manifest
def _download_file(url: str, path: Path) -> bytes:
if not external_files.has_remote_file_changed(url, path):
_LOGGER.debug("Remote file has not changed, skipping download")
return path.read_bytes()
try:
req = requests.get(
url,
timeout=external_files.NETWORK_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 file from {url}: {e}") from e
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(req.content)
return req.content
def _validate_manifest_version(manifest_data):
if manifest_version := manifest_data.get(KEY_VERSION):
if manifest_version == 1:
@ -223,7 +200,7 @@ def _process_http_source(config):
json_path = path / "manifest.json"
json_contents = _download_file(url, json_path)
json_contents = external_files.download_content(url, json_path)
manifest_data = json.loads(json_contents)
if not isinstance(manifest_data, dict):
@ -234,7 +211,7 @@ def _process_http_source(config):
model_path = path / model
_download_file(str(model_url), model_path)
external_files.download_content(str(model_url), model_path)
return config

View file

@ -80,10 +80,10 @@ def compute_local_file_dir(domain: str) -> Path:
return base_directory
def download_content(url: str, path: Path, timeout=NETWORK_TIMEOUT) -> None:
def download_content(url: str, path: Path, timeout=NETWORK_TIMEOUT) -> bytes:
if not has_remote_file_changed(url, path):
_LOGGER.debug("Remote file has not changed %s", url)
return
return path.read_bytes()
_LOGGER.debug(
"Remote file has changed, downloading from %s to %s",
@ -102,4 +102,6 @@ def download_content(url: str, path: Path, timeout=NETWORK_TIMEOUT) -> None:
raise cv.Invalid(f"Could not download from {url}: {e}")
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(req.content)
data = req.content
path.write_bytes(data)
return data