mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 01:07:45 +01:00
Allow dashboard import to specify if api encryption key should be generated (#4393)
This commit is contained in:
parent
93ddce2e79
commit
3facfa5c21
2 changed files with 24 additions and 8 deletions
|
@ -1,3 +1,5 @@
|
|||
import base64
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
|
@ -73,6 +75,7 @@ def import_config(
|
|||
project_name: str,
|
||||
import_url: str,
|
||||
network: str = CONF_WIFI,
|
||||
encryption: bool = False,
|
||||
) -> None:
|
||||
p = Path(path)
|
||||
|
||||
|
@ -80,15 +83,21 @@ def import_config(
|
|||
raise FileExistsError
|
||||
|
||||
if project_name == "esphome.web":
|
||||
kwargs = {
|
||||
"name": name,
|
||||
"friendly_name": friendly_name,
|
||||
"platform": "ESP32" if "esp32" in import_url else "ESP8266",
|
||||
"board": "esp32dev" if "esp32" in import_url else "esp01_1m",
|
||||
"ssid": "!secret wifi_ssid",
|
||||
"psk": "!secret wifi_password",
|
||||
}
|
||||
if encryption:
|
||||
noise_psk = secrets.token_bytes(32)
|
||||
key = base64.b64encode(noise_psk).decode()
|
||||
kwargs["api_encryption_key"] = key
|
||||
|
||||
p.write_text(
|
||||
wizard_file(
|
||||
name=name,
|
||||
friendly_name=friendly_name,
|
||||
platform="ESP32" if "esp32" in import_url else "ESP8266",
|
||||
board="esp32dev" if "esp32" in import_url else "esp01_1m",
|
||||
ssid="!secret wifi_ssid",
|
||||
psk="!secret wifi_password",
|
||||
),
|
||||
wizard_file(**kwargs),
|
||||
encoding="utf8",
|
||||
)
|
||||
else:
|
||||
|
@ -115,6 +124,11 @@ def import_config(
|
|||
"packages": {project_name: import_url},
|
||||
"esphome": esphome_core,
|
||||
}
|
||||
if encryption:
|
||||
noise_psk = secrets.token_bytes(32)
|
||||
key = base64.b64encode(noise_psk).decode()
|
||||
config["api"] = {"encryption": {"key": key}}
|
||||
|
||||
output = dump(config)
|
||||
|
||||
if network == CONF_WIFI:
|
||||
|
|
|
@ -433,6 +433,7 @@ class ImportRequestHandler(BaseHandler):
|
|||
try:
|
||||
name = args["name"]
|
||||
friendly_name = args.get("friendly_name")
|
||||
encryption = args.get("encryption", False)
|
||||
|
||||
imported_device = next(
|
||||
(res for res in IMPORT_RESULT.values() if res.device_name == name), None
|
||||
|
@ -452,6 +453,7 @@ class ImportRequestHandler(BaseHandler):
|
|||
args["project_name"],
|
||||
args["package_import_url"],
|
||||
network,
|
||||
encryption,
|
||||
)
|
||||
except FileExistsError:
|
||||
self.set_status(500)
|
||||
|
|
Loading…
Reference in a new issue