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
+33 -12
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")
+10 -19
View File
@@ -1,23 +1,22 @@
import re
from pathlib import Path
from esphome.core import EsphomeError
from esphome.config_helpers import merge_config
import esphome.config_validation as cv
from esphome import git, yaml_util
from esphome.config_helpers import merge_config
from esphome.const import (
CONF_ESPHOME,
CONF_FILE,
CONF_FILES,
CONF_MIN_VERSION,
CONF_PACKAGES,
CONF_PASSWORD,
CONF_REF,
CONF_REFRESH,
CONF_URL,
CONF_USERNAME,
CONF_PASSWORD,
__version__ as ESPHOME_VERSION,
)
import esphome.config_validation as cv
from esphome.const import __version__ as ESPHOME_VERSION
from esphome.core import EsphomeError
DOMAIN = CONF_PACKAGES
@@ -55,23 +54,15 @@ def validate_source_shorthand(value):
if not isinstance(value, str):
raise cv.Invalid("Shorthand only for strings")
m = re.match(
r"github://([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-\._]+)/([a-zA-Z0-9\-_.\./]+)(?:@([a-zA-Z0-9\-_.\./]+))?",
value,
)
if m is None:
raise cv.Invalid(
"Source is not a file system path or in expected github://username/name/[sub-folder/]file-path.yml[@branch-or-tag] format!"
)
git_file = git.GitFile.from_shorthand(value)
conf = {
CONF_URL: f"https://github.com/{m.group(1)}/{m.group(2)}.git",
CONF_FILE: m.group(3),
CONF_URL: git_file.git_url,
CONF_FILE: git_file.filename,
}
if m.group(4):
conf[CONF_REF] = m.group(4)
if git_file.ref:
conf[CONF_REF] = git_file.ref
# print(conf)
return BASE_SCHEMA(conf)