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 pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
@ -73,6 +75,7 @@ def import_config(
|
||||||
project_name: str,
|
project_name: str,
|
||||||
import_url: str,
|
import_url: str,
|
||||||
network: str = CONF_WIFI,
|
network: str = CONF_WIFI,
|
||||||
|
encryption: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
p = Path(path)
|
p = Path(path)
|
||||||
|
|
||||||
|
@ -80,15 +83,21 @@ def import_config(
|
||||||
raise FileExistsError
|
raise FileExistsError
|
||||||
|
|
||||||
if project_name == "esphome.web":
|
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(
|
p.write_text(
|
||||||
wizard_file(
|
wizard_file(**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",
|
|
||||||
),
|
|
||||||
encoding="utf8",
|
encoding="utf8",
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
@ -115,6 +124,11 @@ def import_config(
|
||||||
"packages": {project_name: import_url},
|
"packages": {project_name: import_url},
|
||||||
"esphome": esphome_core,
|
"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)
|
output = dump(config)
|
||||||
|
|
||||||
if network == CONF_WIFI:
|
if network == CONF_WIFI:
|
||||||
|
|
|
@ -433,6 +433,7 @@ class ImportRequestHandler(BaseHandler):
|
||||||
try:
|
try:
|
||||||
name = args["name"]
|
name = args["name"]
|
||||||
friendly_name = args.get("friendly_name")
|
friendly_name = args.get("friendly_name")
|
||||||
|
encryption = args.get("encryption", False)
|
||||||
|
|
||||||
imported_device = next(
|
imported_device = next(
|
||||||
(res for res in IMPORT_RESULT.values() if res.device_name == name), None
|
(res for res in IMPORT_RESULT.values() if res.device_name == name), None
|
||||||
|
@ -452,6 +453,7 @@ class ImportRequestHandler(BaseHandler):
|
||||||
args["project_name"],
|
args["project_name"],
|
||||||
args["package_import_url"],
|
args["package_import_url"],
|
||||||
network,
|
network,
|
||||||
|
encryption,
|
||||||
)
|
)
|
||||||
except FileExistsError:
|
except FileExistsError:
|
||||||
self.set_status(500)
|
self.set_status(500)
|
||||||
|
|
Loading…
Reference in a new issue