1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-25 06:32:22 +01:00

Allow dashboard import to pull complete file from github (#3982)

This commit is contained in:
Jesse Hills
2022-12-07 07:29:56 +13:00
committed by GitHub
parent 2053b02c61
commit 9370ff3dfa
4 changed files with 107 additions and 36 deletions

View File

@@ -1,4 +1,5 @@
from pathlib import Path
import requests
import esphome.codegen as cg
import esphome.config_validation as cv
@@ -6,6 +7,7 @@ from esphome.components.packages import validate_source_shorthand
from esphome.const import CONF_WIFI
from esphome.wizard import wizard_file
from esphome.yaml_util import dump
from esphome import git
dashboard_import_ns = cg.esphome_ns.namespace("dashboard_import")
@@ -25,9 +27,12 @@ def validate_import_url(value):
CONF_PACKAGE_IMPORT_URL = "package_import_url"
CONF_IMPORT_FULL_CONFIG = "import_full_config"
CONFIG_SCHEMA = cv.Schema(
{
cv.Required(CONF_PACKAGE_IMPORT_URL): validate_import_url,
cv.Optional(CONF_IMPORT_FULL_CONFIG, default=False): cv.boolean,
}
)
@@ -41,6 +46,9 @@ wifi:
async def to_code(config):
cg.add_define("USE_DASHBOARD_IMPORT")
url = config[CONF_PACKAGE_IMPORT_URL]
if config[CONF_IMPORT_FULL_CONFIG]:
url += "?full_config"
cg.add(dashboard_import_ns.set_package_import_url(config[CONF_PACKAGE_IMPORT_URL]))
@@ -64,17 +72,30 @@ def import_config(
encoding="utf8",
)
else:
config = {
"substitutions": {"name": name},
"packages": {project_name: import_url},
"esphome": {
"name": "${name}",
"name_add_mac_suffix": False,
},
}
output = dump(config)
git_file = git.GitFile.from_shorthand(import_url)
if network == CONF_WIFI:
output += WIFI_CONFIG
if git_file.query and "full_config" in git_file.query:
url = git_file.raw_url
try:
req = requests.get(url, timeout=30)
req.raise_for_status()
except requests.exceptions.RequestException as e:
raise ValueError(f"Error while fetching {url}: {e}") from e
p.write_text(output, encoding="utf8")
p.write_text(req.text, encoding="utf8")
else:
config = {
"substitutions": {"name": name},
"packages": {project_name: import_url},
"esphome": {
"name": "${name}",
"name_add_mac_suffix": False,
},
}
output = dump(config)
if network == CONF_WIFI:
output += WIFI_CONFIG
p.write_text(output, encoding="utf8")