2018-04-07 01:23:03 +02:00
|
|
|
import argparse
|
2019-05-12 23:04:36 +02:00
|
|
|
import functools
|
2018-04-07 01:23:03 +02:00
|
|
|
import logging
|
|
|
|
import os
|
2022-04-22 07:08:01 +02:00
|
|
|
import re
|
2018-04-07 01:23:03 +02:00
|
|
|
import sys
|
2022-10-20 05:50:39 +02:00
|
|
|
import time
|
2019-04-17 12:06:00 +02:00
|
|
|
from datetime import datetime
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
from esphome import const, writer, yaml_util
|
2019-05-12 23:04:36 +02:00
|
|
|
import esphome.codegen as cg
|
2019-04-17 12:06:00 +02:00
|
|
|
from esphome.config import iter_components, read_config, strip_default_ids
|
2021-03-07 20:03:16 +01:00
|
|
|
from esphome.const import (
|
2022-04-22 07:08:01 +02:00
|
|
|
ALLOWED_NAME_CHARS,
|
2021-03-07 20:03:16 +01:00
|
|
|
CONF_BAUD_RATE,
|
|
|
|
CONF_BROKER,
|
2021-08-10 21:55:36 +02:00
|
|
|
CONF_DEASSERT_RTS_DTR,
|
2021-03-07 20:03:16 +01:00
|
|
|
CONF_LOGGER,
|
2022-04-22 07:08:01 +02:00
|
|
|
CONF_NAME,
|
2021-03-07 20:03:16 +01:00
|
|
|
CONF_OTA,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_ESPHOME,
|
|
|
|
CONF_PLATFORMIO_OPTIONS,
|
2022-04-22 07:08:01 +02:00
|
|
|
CONF_SUBSTITUTIONS,
|
2022-10-20 05:50:39 +02:00
|
|
|
PLATFORM_ESP32,
|
|
|
|
PLATFORM_ESP8266,
|
|
|
|
PLATFORM_RP2040,
|
2021-11-15 20:06:55 +01:00
|
|
|
SECRETS_FILES,
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
2021-05-17 07:14:15 +02:00
|
|
|
from esphome.core import CORE, EsphomeError, coroutine
|
2021-04-08 13:58:01 +02:00
|
|
|
from esphome.helpers import indent
|
2021-03-07 20:03:16 +01:00
|
|
|
from esphome.util import (
|
|
|
|
run_external_command,
|
|
|
|
run_external_process,
|
|
|
|
safe_print,
|
|
|
|
list_yaml_files,
|
|
|
|
get_serial_ports,
|
|
|
|
)
|
2021-04-08 13:58:01 +02:00
|
|
|
from esphome.log import color, setup_log, Fore
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-12-24 14:15:24 +01:00
|
|
|
def choose_prompt(options):
|
|
|
|
if not options:
|
2021-03-07 20:03:16 +01:00
|
|
|
raise EsphomeError(
|
|
|
|
"Found no valid options for upload/logging, please make sure relevant "
|
|
|
|
"sections (ota, api, mqtt, ...) are in your configuration and/or the "
|
|
|
|
"device is plugged in."
|
|
|
|
)
|
2018-12-24 14:15:24 +01:00
|
|
|
|
|
|
|
if len(options) == 1:
|
|
|
|
return options[0][1]
|
|
|
|
|
2019-12-07 18:28:55 +01:00
|
|
|
safe_print("Found multiple options, please choose one:")
|
2018-12-24 14:15:24 +01:00
|
|
|
for i, (desc, _) in enumerate(options):
|
2019-12-07 18:28:55 +01:00
|
|
|
safe_print(f" [{i+1}] {desc}")
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2018-04-10 20:17:20 +02:00
|
|
|
while True:
|
2021-03-07 20:03:16 +01:00
|
|
|
opt = input("(number): ")
|
2018-12-24 14:15:24 +01:00
|
|
|
if opt in options:
|
|
|
|
opt = options.index(opt)
|
2018-04-10 20:17:20 +02:00
|
|
|
break
|
|
|
|
try:
|
|
|
|
opt = int(opt)
|
2018-12-24 14:15:24 +01:00
|
|
|
if opt < 1 or opt > len(options):
|
2018-04-10 20:17:20 +02:00
|
|
|
raise ValueError
|
|
|
|
break
|
|
|
|
except ValueError:
|
2021-04-08 13:58:01 +02:00
|
|
|
safe_print(color(Fore.RED, f"Invalid option: '{opt}'"))
|
2018-12-24 14:15:24 +01:00
|
|
|
return options[opt - 1][1]
|
|
|
|
|
|
|
|
|
|
|
|
def choose_upload_log_host(default, check_default, show_ota, show_mqtt, show_api):
|
|
|
|
options = []
|
2020-07-24 10:09:43 +02:00
|
|
|
for port in get_serial_ports():
|
|
|
|
options.append((f"{port.path} ({port.description})", port.path))
|
2021-03-07 20:03:16 +01:00
|
|
|
if (show_ota and "ota" in CORE.config) or (show_api and "api" in CORE.config):
|
2019-12-07 18:28:55 +01:00
|
|
|
options.append((f"Over The Air ({CORE.address})", CORE.address))
|
2021-03-07 20:03:16 +01:00
|
|
|
if default == "OTA":
|
2018-12-24 14:15:24 +01:00
|
|
|
return CORE.address
|
2021-03-07 20:03:16 +01:00
|
|
|
if show_mqtt and "mqtt" in CORE.config:
|
2021-09-19 19:22:28 +02:00
|
|
|
options.append((f"MQTT ({CORE.config['mqtt'][CONF_BROKER]})", "MQTT"))
|
2021-03-07 20:03:16 +01:00
|
|
|
if default == "OTA":
|
|
|
|
return "MQTT"
|
2018-12-24 14:15:24 +01:00
|
|
|
if default is not None:
|
|
|
|
return default
|
|
|
|
if check_default is not None and check_default in [opt[1] for opt in options]:
|
|
|
|
return check_default
|
|
|
|
return choose_prompt(options)
|
|
|
|
|
|
|
|
|
|
|
|
def get_port_type(port):
|
2021-03-07 20:03:16 +01:00
|
|
|
if port.startswith("/") or port.startswith("COM"):
|
|
|
|
return "SERIAL"
|
|
|
|
if port == "MQTT":
|
|
|
|
return "MQTT"
|
|
|
|
return "NETWORK"
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
def run_miniterm(config, port):
|
2018-05-21 16:40:22 +02:00
|
|
|
import serial
|
2019-04-08 22:19:21 +02:00
|
|
|
from esphome import platformio_api
|
|
|
|
|
2018-10-04 19:00:51 +02:00
|
|
|
if CONF_LOGGER not in config:
|
|
|
|
_LOGGER.info("Logger is not enabled. Not starting UART logs.")
|
2022-10-20 05:50:39 +02:00
|
|
|
return 1
|
2021-03-07 20:03:16 +01:00
|
|
|
baud_rate = config["logger"][CONF_BAUD_RATE]
|
2018-10-04 19:00:51 +02:00
|
|
|
if baud_rate == 0:
|
|
|
|
_LOGGER.info("UART logging is disabled (baud_rate=0). Not starting UART logs.")
|
2022-10-20 05:50:39 +02:00
|
|
|
return 1
|
2018-05-21 16:40:22 +02:00
|
|
|
_LOGGER.info("Starting log output from %s with baud rate %s", port, baud_rate)
|
|
|
|
|
2018-11-03 14:06:14 +01:00
|
|
|
backtrace_state = False
|
2021-08-10 21:55:36 +02:00
|
|
|
ser = serial.Serial()
|
|
|
|
ser.baudrate = baud_rate
|
|
|
|
ser.port = port
|
|
|
|
|
|
|
|
# We can't set to False by default since it leads to toggling and hence
|
|
|
|
# ESP32 resets on some platforms.
|
|
|
|
if config["logger"][CONF_DEASSERT_RTS_DTR]:
|
|
|
|
ser.dtr = False
|
|
|
|
ser.rts = False
|
|
|
|
|
2022-10-20 05:50:39 +02:00
|
|
|
tries = 0
|
|
|
|
while tries < 5:
|
|
|
|
try:
|
|
|
|
with ser:
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
raw = ser.readline()
|
|
|
|
except serial.SerialException:
|
|
|
|
_LOGGER.error("Serial port closed!")
|
|
|
|
return 0
|
|
|
|
line = (
|
|
|
|
raw.replace(b"\r", b"")
|
|
|
|
.replace(b"\n", b"")
|
|
|
|
.decode("utf8", "backslashreplace")
|
|
|
|
)
|
|
|
|
time_str = datetime.now().time().strftime("[%H:%M:%S]")
|
|
|
|
message = time_str + line
|
|
|
|
safe_print(message)
|
|
|
|
|
|
|
|
backtrace_state = platformio_api.process_stacktrace(
|
|
|
|
config, line, backtrace_state=backtrace_state
|
|
|
|
)
|
|
|
|
except serial.SerialException:
|
|
|
|
tries += 1
|
|
|
|
time.sleep(1)
|
|
|
|
if tries >= 5:
|
|
|
|
_LOGGER.error("Could not connect to serial port %s", port)
|
|
|
|
return 1
|
2018-11-03 14:06:14 +01:00
|
|
|
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2019-05-12 23:04:36 +02:00
|
|
|
def wrap_to_code(name, comp):
|
|
|
|
coro = coroutine(comp.to_code)
|
|
|
|
|
|
|
|
@functools.wraps(comp.to_code)
|
2021-05-17 07:14:15 +02:00
|
|
|
async def wrapped(conf):
|
2019-12-07 18:28:55 +01:00
|
|
|
cg.add(cg.LineComment(f"{name}:"))
|
2019-05-12 23:04:36 +02:00
|
|
|
if comp.config_schema is not None:
|
2019-05-28 16:00:00 +02:00
|
|
|
conf_str = yaml_util.dump(conf)
|
2021-03-07 20:03:16 +01:00
|
|
|
conf_str = conf_str.replace("//", "")
|
2021-12-13 19:15:22 +01:00
|
|
|
# remove tailing \ to avoid multi-line comment warning
|
|
|
|
conf_str = conf_str.replace("\\\n", "\n")
|
2019-05-28 16:00:00 +02:00
|
|
|
cg.add(cg.LineComment(indent(conf_str)))
|
2021-05-17 07:14:15 +02:00
|
|
|
await coro(conf)
|
2019-05-12 23:04:36 +02:00
|
|
|
|
2021-05-17 07:14:15 +02:00
|
|
|
if hasattr(coro, "priority"):
|
|
|
|
wrapped.priority = coro.priority
|
2019-05-12 23:04:36 +02:00
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
2018-04-07 01:23:03 +02:00
|
|
|
def write_cpp(config):
|
2020-07-14 17:59:03 +02:00
|
|
|
generate_cpp_contents(config)
|
|
|
|
return write_cpp_file()
|
|
|
|
|
|
|
|
|
|
|
|
def generate_cpp_contents(config):
|
2018-04-07 01:23:03 +02:00
|
|
|
_LOGGER.info("Generating C++ source...")
|
2018-04-18 18:43:13 +02:00
|
|
|
|
2019-05-12 23:04:36 +02:00
|
|
|
for name, component, conf in iter_components(CORE.config):
|
2019-04-17 12:06:00 +02:00
|
|
|
if component.to_code is not None:
|
2019-05-12 23:04:36 +02:00
|
|
|
coro = wrap_to_code(name, component)
|
|
|
|
CORE.add_job(coro, conf)
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
CORE.flush_tasks()
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2020-07-14 17:59:03 +02:00
|
|
|
|
|
|
|
def write_cpp_file():
|
2018-12-05 21:22:06 +01:00
|
|
|
writer.write_platformio_project()
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
code_s = indent(CORE.cpp_main_section)
|
2018-12-05 21:22:06 +01:00
|
|
|
writer.write_cpp(code_s)
|
2018-04-07 01:23:03 +02:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2018-06-02 22:22:20 +02:00
|
|
|
def compile_program(args, config):
|
2019-04-08 22:19:21 +02:00
|
|
|
from esphome import platformio_api
|
|
|
|
|
2018-04-07 01:23:03 +02:00
|
|
|
_LOGGER.info("Compiling app...")
|
2021-10-17 08:54:09 +02:00
|
|
|
rc = platformio_api.run_compile(config, CORE.verbose)
|
|
|
|
if rc != 0:
|
|
|
|
return rc
|
|
|
|
idedata = platformio_api.get_idedata(config)
|
|
|
|
return 0 if idedata is not None else 1
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
def upload_using_esptool(config, port):
|
2021-10-04 16:59:15 +02:00
|
|
|
from esphome import platformio_api
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
first_baudrate = config[CONF_ESPHOME][CONF_PLATFORMIO_OPTIONS].get(
|
|
|
|
"upload_speed", 460800
|
|
|
|
)
|
2019-11-14 12:42:50 +01:00
|
|
|
|
|
|
|
def run_esptool(baud_rate):
|
2021-10-04 16:59:15 +02:00
|
|
|
idedata = platformio_api.get_idedata(config)
|
|
|
|
|
|
|
|
firmware_offset = "0x10000" if CORE.is_esp32 else "0x0"
|
|
|
|
flash_images = [
|
|
|
|
platformio_api.FlashImage(
|
2021-11-15 20:06:55 +01:00
|
|
|
path=idedata.firmware_bin_path, offset=firmware_offset
|
2021-10-04 16:59:15 +02:00
|
|
|
),
|
|
|
|
*idedata.extra_flash_images,
|
|
|
|
]
|
|
|
|
|
|
|
|
mcu = "esp8266"
|
|
|
|
if CORE.is_esp32:
|
|
|
|
from esphome.components.esp32 import get_esp32_variant
|
|
|
|
|
|
|
|
mcu = get_esp32_variant().lower()
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
cmd = [
|
|
|
|
"esptool.py",
|
|
|
|
"--before",
|
|
|
|
"default_reset",
|
|
|
|
"--after",
|
|
|
|
"hard_reset",
|
|
|
|
"--baud",
|
|
|
|
str(baud_rate),
|
|
|
|
"--port",
|
|
|
|
port,
|
2021-10-04 16:59:15 +02:00
|
|
|
"--chip",
|
|
|
|
mcu,
|
2021-03-07 20:03:16 +01:00
|
|
|
"write_flash",
|
2021-10-04 16:59:15 +02:00
|
|
|
"-z",
|
2021-10-23 13:25:46 +02:00
|
|
|
"--flash_size",
|
|
|
|
"detect",
|
2021-03-07 20:03:16 +01:00
|
|
|
]
|
2021-10-04 16:59:15 +02:00
|
|
|
for img in flash_images:
|
|
|
|
cmd += [img.offset, img.path]
|
2021-03-07 20:03:16 +01:00
|
|
|
|
|
|
|
if os.environ.get("ESPHOME_USE_SUBPROCESS") is None:
|
2019-11-14 12:42:50 +01:00
|
|
|
import esptool
|
2021-03-07 20:03:16 +01:00
|
|
|
|
2019-11-14 12:42:50 +01:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
return run_external_command(esptool._main, *cmd)
|
|
|
|
|
|
|
|
return run_external_process(*cmd)
|
|
|
|
|
|
|
|
rc = run_esptool(first_baudrate)
|
|
|
|
if rc == 0 or first_baudrate == 115200:
|
|
|
|
return rc
|
|
|
|
# Try with 115200 baud rate, with some serial chips the faster baud rates do not work well
|
2021-03-07 20:03:16 +01:00
|
|
|
_LOGGER.info(
|
|
|
|
"Upload with baud rate %s failed. Trying again with baud rate 115200.",
|
|
|
|
first_baudrate,
|
|
|
|
)
|
2019-11-14 12:42:50 +01:00
|
|
|
return run_esptool(115200)
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
|
2018-12-24 14:15:24 +01:00
|
|
|
def upload_program(config, args, host):
|
2021-03-07 20:03:16 +01:00
|
|
|
if get_port_type(host) == "SERIAL":
|
2022-10-20 05:50:39 +02:00
|
|
|
if CORE.target_platform in (PLATFORM_ESP32, PLATFORM_ESP8266):
|
|
|
|
return upload_using_esptool(config, host)
|
|
|
|
|
|
|
|
if CORE.target_platform in (PLATFORM_RP2040):
|
|
|
|
from esphome import platformio_api
|
|
|
|
|
|
|
|
upload_args = ["-t", "upload"]
|
|
|
|
if args.device is not None:
|
|
|
|
upload_args += ["--upload-port", args.device]
|
|
|
|
return platformio_api.run_platformio_cli_run(
|
|
|
|
config, CORE.verbose, *upload_args
|
|
|
|
)
|
|
|
|
|
|
|
|
return 1 # Unknown target platform
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2019-02-13 16:54:02 +01:00
|
|
|
from esphome import espota2
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2019-11-12 15:28:23 +01:00
|
|
|
if CONF_OTA not in config:
|
2021-03-07 20:03:16 +01:00
|
|
|
raise EsphomeError(
|
|
|
|
"Cannot upload Over the Air as the config does not include the ota: "
|
|
|
|
"component"
|
|
|
|
)
|
2019-11-12 15:28:23 +01:00
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
ota_conf = config[CONF_OTA]
|
|
|
|
remote_port = ota_conf[CONF_PORT]
|
2021-09-20 11:47:51 +02:00
|
|
|
password = ota_conf.get(CONF_PASSWORD, "")
|
2019-06-07 14:26:28 +02:00
|
|
|
return espota2.run_ota(host, remote_port, password, CORE.firmware_bin)
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
def show_logs(config, args, port):
|
2021-03-07 20:03:16 +01:00
|
|
|
if "logger" not in config:
|
2019-02-13 16:54:02 +01:00
|
|
|
raise EsphomeError("Logger is not configured!")
|
2021-03-07 20:03:16 +01:00
|
|
|
if get_port_type(port) == "SERIAL":
|
2022-10-20 05:50:39 +02:00
|
|
|
return run_miniterm(config, port)
|
2021-03-07 20:03:16 +01:00
|
|
|
if get_port_type(port) == "NETWORK" and "api" in config:
|
2021-09-14 09:53:37 +02:00
|
|
|
from esphome.components.api.client import run_logs
|
2019-04-08 21:57:25 +02:00
|
|
|
|
2018-12-24 14:15:24 +01:00
|
|
|
return run_logs(config, port)
|
2021-03-07 20:03:16 +01:00
|
|
|
if get_port_type(port) == "MQTT" and "mqtt" in config:
|
2019-04-08 21:57:25 +02:00
|
|
|
from esphome import mqtt
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
return mqtt.show_logs(
|
|
|
|
config, args.topic, args.username, args.password, args.client_id
|
|
|
|
)
|
2018-12-24 14:15:24 +01:00
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
raise EsphomeError("No remote or local logging method configured (api/mqtt/logger)")
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def clean_mqtt(config, args):
|
2019-04-08 21:57:25 +02:00
|
|
|
from esphome import mqtt
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
return mqtt.clear_topic(
|
|
|
|
config, args.topic, args.username, args.password, args.client_id
|
|
|
|
)
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
def command_wizard(args):
|
2019-04-08 22:19:21 +02:00
|
|
|
from esphome import wizard
|
|
|
|
|
2021-06-08 01:14:12 +02:00
|
|
|
return wizard.wizard(args.configuration)
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
def command_config(args, config):
|
2018-12-05 21:22:06 +01:00
|
|
|
_LOGGER.info("Configuration is valid!")
|
2019-10-19 14:04:14 +02:00
|
|
|
if not CORE.verbose:
|
2018-06-02 22:22:20 +02:00
|
|
|
config = strip_default_ids(config)
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print(yaml_util.dump(config))
|
2018-05-21 16:40:22 +02:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
def command_vscode(args):
|
|
|
|
from esphome import vscode
|
|
|
|
|
2021-03-07 23:59:32 +01:00
|
|
|
logging.disable(logging.INFO)
|
|
|
|
logging.disable(logging.WARNING)
|
2019-05-11 11:41:09 +02:00
|
|
|
vscode.read_config(args)
|
2019-04-22 21:56:30 +02:00
|
|
|
|
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
def command_compile(args, config):
|
|
|
|
exit_code = write_cpp(config)
|
|
|
|
if exit_code != 0:
|
|
|
|
return exit_code
|
2018-09-19 14:51:39 +02:00
|
|
|
if args.only_generate:
|
2019-12-07 18:28:55 +01:00
|
|
|
_LOGGER.info("Successfully generated source code.")
|
2018-09-23 18:58:41 +02:00
|
|
|
return 0
|
2018-06-02 22:22:20 +02:00
|
|
|
exit_code = compile_program(args, config)
|
2018-05-21 16:40:22 +02:00
|
|
|
if exit_code != 0:
|
|
|
|
return exit_code
|
2019-12-07 18:28:55 +01:00
|
|
|
_LOGGER.info("Successfully compiled program.")
|
2018-05-21 16:40:22 +02:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
def command_upload(args, config):
|
2021-03-07 20:03:16 +01:00
|
|
|
port = choose_upload_log_host(
|
2021-06-08 01:14:12 +02:00
|
|
|
default=args.device,
|
2021-03-07 20:03:16 +01:00
|
|
|
check_default=None,
|
|
|
|
show_ota=True,
|
|
|
|
show_mqtt=False,
|
|
|
|
show_api=False,
|
|
|
|
)
|
2018-05-21 16:40:22 +02:00
|
|
|
exit_code = upload_program(config, args, port)
|
|
|
|
if exit_code != 0:
|
|
|
|
return exit_code
|
2019-12-07 18:28:55 +01:00
|
|
|
_LOGGER.info("Successfully uploaded program.")
|
2018-05-21 16:40:22 +02:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
def command_logs(args, config):
|
2021-03-07 20:03:16 +01:00
|
|
|
port = choose_upload_log_host(
|
2021-06-08 01:14:12 +02:00
|
|
|
default=args.device,
|
2021-03-07 20:03:16 +01:00
|
|
|
check_default=None,
|
|
|
|
show_ota=False,
|
|
|
|
show_mqtt=True,
|
|
|
|
show_api=True,
|
|
|
|
)
|
2018-12-05 21:22:06 +01:00
|
|
|
return show_logs(config, args, port)
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
def command_run(args, config):
|
|
|
|
exit_code = write_cpp(config)
|
|
|
|
if exit_code != 0:
|
|
|
|
return exit_code
|
2018-06-02 22:22:20 +02:00
|
|
|
exit_code = compile_program(args, config)
|
2018-05-21 16:40:22 +02:00
|
|
|
if exit_code != 0:
|
|
|
|
return exit_code
|
2019-12-07 18:28:55 +01:00
|
|
|
_LOGGER.info("Successfully compiled program.")
|
2021-03-07 20:03:16 +01:00
|
|
|
port = choose_upload_log_host(
|
2021-06-08 01:14:12 +02:00
|
|
|
default=args.device,
|
2021-03-07 20:03:16 +01:00
|
|
|
check_default=None,
|
|
|
|
show_ota=True,
|
|
|
|
show_mqtt=False,
|
|
|
|
show_api=True,
|
|
|
|
)
|
2018-05-21 16:40:22 +02:00
|
|
|
exit_code = upload_program(config, args, port)
|
|
|
|
if exit_code != 0:
|
|
|
|
return exit_code
|
2019-12-07 18:28:55 +01:00
|
|
|
_LOGGER.info("Successfully uploaded program.")
|
2018-05-21 16:40:22 +02:00
|
|
|
if args.no_logs:
|
|
|
|
return 0
|
2021-03-07 20:03:16 +01:00
|
|
|
port = choose_upload_log_host(
|
2021-06-08 01:14:12 +02:00
|
|
|
default=args.device,
|
2021-03-07 20:03:16 +01:00
|
|
|
check_default=port,
|
|
|
|
show_ota=False,
|
|
|
|
show_mqtt=True,
|
|
|
|
show_api=True,
|
|
|
|
)
|
2018-12-05 21:22:06 +01:00
|
|
|
return show_logs(config, args, port)
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
def command_clean_mqtt(args, config):
|
|
|
|
return clean_mqtt(config, args)
|
|
|
|
|
|
|
|
|
|
|
|
def command_mqtt_fingerprint(args, config):
|
2019-04-08 21:57:25 +02:00
|
|
|
from esphome import mqtt
|
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
return mqtt.get_fingerprint(config)
|
|
|
|
|
|
|
|
|
|
|
|
def command_version(args):
|
2019-12-07 18:28:55 +01:00
|
|
|
safe_print(f"Version: {const.__version__}")
|
2018-05-21 16:40:22 +02:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2018-10-14 18:52:21 +02:00
|
|
|
def command_clean(args, config):
|
|
|
|
try:
|
2018-12-05 21:22:06 +01:00
|
|
|
writer.clean_build()
|
2018-10-14 18:52:21 +02:00
|
|
|
except OSError as err:
|
|
|
|
_LOGGER.error("Error deleting build files: %s", err)
|
|
|
|
return 1
|
|
|
|
_LOGGER.info("Done!")
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2018-05-27 14:15:24 +02:00
|
|
|
def command_dashboard(args):
|
2019-02-13 16:54:02 +01:00
|
|
|
from esphome.dashboard import dashboard
|
2018-05-21 16:40:22 +02:00
|
|
|
|
2018-05-27 14:15:24 +02:00
|
|
|
return dashboard.start_web_server(args)
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
|
2019-06-07 14:26:28 +02:00
|
|
|
def command_update_all(args):
|
|
|
|
import click
|
|
|
|
|
|
|
|
success = {}
|
2021-08-11 07:21:57 +02:00
|
|
|
files = list_yaml_files(args.configuration)
|
2019-06-07 14:26:28 +02:00
|
|
|
twidth = 60
|
|
|
|
|
|
|
|
def print_bar(middle_text):
|
2019-12-07 18:28:55 +01:00
|
|
|
middle_text = f" {middle_text} "
|
2019-06-07 14:26:28 +02:00
|
|
|
width = len(click.unstyle(middle_text))
|
2019-10-30 16:16:14 +01:00
|
|
|
half_line = "=" * ((twidth - width) // 2)
|
2019-12-07 18:28:55 +01:00
|
|
|
click.echo(f"{half_line}{middle_text}{half_line}")
|
2019-06-07 14:26:28 +02:00
|
|
|
|
|
|
|
for f in files:
|
2021-09-19 19:22:28 +02:00
|
|
|
print(f"Updating {color(Fore.CYAN, f)}")
|
2021-03-07 20:03:16 +01:00
|
|
|
print("-" * twidth)
|
2019-06-07 14:26:28 +02:00
|
|
|
print()
|
2021-03-07 20:03:16 +01:00
|
|
|
rc = run_external_process(
|
2021-08-10 03:27:21 +02:00
|
|
|
"esphome", "--dashboard", "run", f, "--no-logs", "--device", "OTA"
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
2019-06-07 14:26:28 +02:00
|
|
|
if rc == 0:
|
2021-09-19 19:22:28 +02:00
|
|
|
print_bar(f"[{color(Fore.BOLD_GREEN, 'SUCCESS')}] {f}")
|
2019-06-07 14:26:28 +02:00
|
|
|
success[f] = True
|
|
|
|
else:
|
2021-09-19 19:22:28 +02:00
|
|
|
print_bar(f"[{color(Fore.BOLD_RED, 'ERROR')}] {f}")
|
2019-06-07 14:26:28 +02:00
|
|
|
success[f] = False
|
|
|
|
|
|
|
|
print()
|
|
|
|
print()
|
|
|
|
print()
|
|
|
|
|
2021-09-19 19:22:28 +02:00
|
|
|
print_bar(f"[{color(Fore.BOLD_WHITE, 'SUMMARY')}]")
|
2019-06-07 14:26:28 +02:00
|
|
|
failed = 0
|
|
|
|
for f in files:
|
|
|
|
if success[f]:
|
2021-09-19 19:22:28 +02:00
|
|
|
print(f" - {f}: {color(Fore.GREEN, 'SUCCESS')}")
|
2019-06-07 14:26:28 +02:00
|
|
|
else:
|
2021-09-19 19:22:28 +02:00
|
|
|
print(f" - {f}: {color(Fore.BOLD_RED, 'FAILED')}")
|
2019-06-07 14:26:28 +02:00
|
|
|
failed += 1
|
|
|
|
return failed
|
|
|
|
|
|
|
|
|
2021-10-17 08:54:09 +02:00
|
|
|
def command_idedata(args, config):
|
|
|
|
from esphome import platformio_api
|
|
|
|
import json
|
|
|
|
|
|
|
|
logging.disable(logging.INFO)
|
|
|
|
logging.disable(logging.WARNING)
|
|
|
|
|
|
|
|
idedata = platformio_api.get_idedata(config)
|
|
|
|
if idedata is None:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
print(json.dumps(idedata.raw, indent=2) + "\n")
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2022-04-22 07:08:01 +02:00
|
|
|
def command_rename(args, config):
|
|
|
|
for c in args.name:
|
|
|
|
if c not in ALLOWED_NAME_CHARS:
|
|
|
|
print(
|
|
|
|
color(
|
|
|
|
Fore.BOLD_RED,
|
|
|
|
f"'{c}' is an invalid character for names. Valid characters are: "
|
|
|
|
f"{ALLOWED_NAME_CHARS} (lowercase, no spaces)",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return 1
|
2022-05-09 09:16:46 +02:00
|
|
|
# Load existing yaml file
|
2022-04-22 07:08:01 +02:00
|
|
|
with open(CORE.config_path, mode="r+", encoding="utf-8") as raw_file:
|
|
|
|
raw_contents = raw_file.read()
|
2022-05-09 09:16:46 +02:00
|
|
|
|
|
|
|
yaml = yaml_util.load_yaml(CORE.config_path)
|
|
|
|
if CONF_ESPHOME not in yaml or CONF_NAME not in yaml[CONF_ESPHOME]:
|
|
|
|
print(
|
|
|
|
color(Fore.BOLD_RED, "Complex YAML files cannot be automatically renamed.")
|
|
|
|
)
|
|
|
|
return 1
|
|
|
|
old_name = yaml[CONF_ESPHOME][CONF_NAME]
|
|
|
|
match = re.match(r"^\$\{?([a-zA-Z0-9_]+)\}?$", old_name)
|
|
|
|
if match is None:
|
|
|
|
new_raw = re.sub(
|
|
|
|
rf"name:\s+[\"']?{old_name}[\"']?",
|
|
|
|
f'name: "{args.name}"',
|
|
|
|
raw_contents,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
old_name = yaml[CONF_SUBSTITUTIONS][match.group(1)]
|
|
|
|
if (
|
|
|
|
len(
|
|
|
|
re.findall(
|
|
|
|
rf"^\s+{match.group(1)}:\s+[\"']?{old_name}[\"']?",
|
|
|
|
raw_contents,
|
|
|
|
flags=re.MULTILINE,
|
2022-04-22 07:08:01 +02:00
|
|
|
)
|
|
|
|
)
|
2022-05-09 09:16:46 +02:00
|
|
|
> 1
|
|
|
|
):
|
|
|
|
print(color(Fore.BOLD_RED, "Too many matches in YAML to safely rename"))
|
2022-04-22 07:08:01 +02:00
|
|
|
return 1
|
|
|
|
|
2022-05-09 09:16:46 +02:00
|
|
|
new_raw = re.sub(
|
|
|
|
rf"^(\s+{match.group(1)}):\s+[\"']?{old_name}[\"']?",
|
|
|
|
f'\\1: "{args.name}"',
|
|
|
|
raw_contents,
|
|
|
|
flags=re.MULTILINE,
|
|
|
|
)
|
2022-04-22 07:08:01 +02:00
|
|
|
|
2022-05-09 09:16:46 +02:00
|
|
|
new_path = os.path.join(CORE.config_dir, args.name + ".yaml")
|
|
|
|
print(
|
|
|
|
f"Updating {color(Fore.CYAN, CORE.config_path)} to {color(Fore.CYAN, new_path)}"
|
|
|
|
)
|
|
|
|
print()
|
2022-04-22 07:08:01 +02:00
|
|
|
|
2022-05-09 09:16:46 +02:00
|
|
|
with open(new_path, mode="w", encoding="utf-8") as new_file:
|
|
|
|
new_file.write(new_raw)
|
2022-04-22 07:08:01 +02:00
|
|
|
|
2022-05-09 09:16:46 +02:00
|
|
|
rc = run_external_process("esphome", "config", new_path)
|
|
|
|
if rc != 0:
|
|
|
|
print(color(Fore.BOLD_RED, "Rename failed. Reverting changes."))
|
|
|
|
os.remove(new_path)
|
|
|
|
return 1
|
2022-04-22 07:08:01 +02:00
|
|
|
|
2022-05-09 09:16:46 +02:00
|
|
|
cli_args = [
|
|
|
|
"run",
|
|
|
|
new_path,
|
|
|
|
"--no-logs",
|
|
|
|
"--device",
|
|
|
|
CORE.address,
|
|
|
|
]
|
2022-04-22 07:08:01 +02:00
|
|
|
|
2022-05-09 09:16:46 +02:00
|
|
|
if args.dashboard:
|
|
|
|
cli_args.insert(0, "--dashboard")
|
2022-04-22 07:08:01 +02:00
|
|
|
|
2022-05-09 09:16:46 +02:00
|
|
|
try:
|
|
|
|
rc = run_external_process("esphome", *cli_args)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
rc = 1
|
|
|
|
if rc != 0:
|
|
|
|
os.remove(new_path)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
os.remove(CORE.config_path)
|
|
|
|
|
|
|
|
print(color(Fore.BOLD_GREEN, "SUCCESS"))
|
|
|
|
print()
|
|
|
|
return 0
|
2022-04-22 07:08:01 +02:00
|
|
|
|
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
PRE_CONFIG_ACTIONS = {
|
2021-03-07 20:03:16 +01:00
|
|
|
"wizard": command_wizard,
|
|
|
|
"version": command_version,
|
|
|
|
"dashboard": command_dashboard,
|
|
|
|
"vscode": command_vscode,
|
|
|
|
"update-all": command_update_all,
|
2018-05-21 16:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
POST_CONFIG_ACTIONS = {
|
2021-03-07 20:03:16 +01:00
|
|
|
"config": command_config,
|
|
|
|
"compile": command_compile,
|
|
|
|
"upload": command_upload,
|
|
|
|
"logs": command_logs,
|
|
|
|
"run": command_run,
|
|
|
|
"clean-mqtt": command_clean_mqtt,
|
|
|
|
"mqtt-fingerprint": command_mqtt_fingerprint,
|
|
|
|
"clean": command_clean,
|
2021-10-17 08:54:09 +02:00
|
|
|
"idedata": command_idedata,
|
2022-04-22 07:08:01 +02:00
|
|
|
"rename": command_rename,
|
2018-05-21 16:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args(argv):
|
2021-06-08 01:14:12 +02:00
|
|
|
options_parser = argparse.ArgumentParser(add_help=False)
|
|
|
|
options_parser.add_argument(
|
|
|
|
"-v", "--verbose", help="Enable verbose ESPHome logs.", action="store_true"
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
2021-06-08 01:14:12 +02:00
|
|
|
options_parser.add_argument(
|
|
|
|
"-q", "--quiet", help="Disable all ESPHome logs.", action="store_true"
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
2021-06-08 01:14:12 +02:00
|
|
|
options_parser.add_argument(
|
|
|
|
"--dashboard", help=argparse.SUPPRESS, action="store_true"
|
|
|
|
)
|
|
|
|
options_parser.add_argument(
|
2021-03-07 20:03:16 +01:00
|
|
|
"-s",
|
|
|
|
"--substitution",
|
|
|
|
nargs=2,
|
|
|
|
action="append",
|
|
|
|
help="Add a substitution",
|
|
|
|
metavar=("key", "value"),
|
|
|
|
)
|
2021-06-08 01:14:12 +02:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description=f"ESPHome v{const.__version__}", parents=[options_parser]
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
2021-06-08 01:14:12 +02:00
|
|
|
|
|
|
|
mqtt_options = argparse.ArgumentParser(add_help=False)
|
|
|
|
mqtt_options.add_argument("--topic", help="Manually set the MQTT topic.")
|
|
|
|
mqtt_options.add_argument("--username", help="Manually set the MQTT username.")
|
|
|
|
mqtt_options.add_argument("--password", help="Manually set the MQTT password.")
|
|
|
|
mqtt_options.add_argument("--client-id", help="Manually set the MQTT client id.")
|
2021-03-07 20:03:16 +01:00
|
|
|
|
2021-06-08 01:14:12 +02:00
|
|
|
subparsers = parser.add_subparsers(
|
|
|
|
help="Command to run:", dest="command", metavar="command"
|
|
|
|
)
|
2018-04-07 01:23:03 +02:00
|
|
|
subparsers.required = True
|
2021-06-08 01:14:12 +02:00
|
|
|
|
|
|
|
parser_config = subparsers.add_parser(
|
|
|
|
"config", help="Validate the configuration and spit it out."
|
|
|
|
)
|
|
|
|
parser_config.add_argument(
|
|
|
|
"configuration", help="Your YAML configuration file(s).", nargs="+"
|
|
|
|
)
|
2021-03-07 20:03:16 +01:00
|
|
|
|
|
|
|
parser_compile = subparsers.add_parser(
|
|
|
|
"compile", help="Read the configuration and compile a program."
|
|
|
|
)
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_compile.add_argument(
|
|
|
|
"configuration", help="Your YAML configuration file(s).", nargs="+"
|
|
|
|
)
|
2021-03-07 20:03:16 +01:00
|
|
|
parser_compile.add_argument(
|
|
|
|
"--only-generate",
|
|
|
|
help="Only generate source code, do not compile.",
|
|
|
|
action="store_true",
|
|
|
|
)
|
|
|
|
|
|
|
|
parser_upload = subparsers.add_parser(
|
2021-06-08 01:14:12 +02:00
|
|
|
"upload", help="Validate the configuration and upload the latest binary."
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
|
|
|
parser_upload.add_argument(
|
2021-06-08 01:14:12 +02:00
|
|
|
"configuration", help="Your YAML configuration file(s).", nargs="+"
|
|
|
|
)
|
|
|
|
parser_upload.add_argument(
|
|
|
|
"--device",
|
|
|
|
help="Manually specify the serial port/address to use, for example /dev/ttyUSB0.",
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
parser_logs = subparsers.add_parser(
|
2021-06-08 01:14:12 +02:00
|
|
|
"logs",
|
|
|
|
help="Validate the configuration and show all logs.",
|
|
|
|
parents=[mqtt_options],
|
|
|
|
)
|
|
|
|
parser_logs.add_argument(
|
|
|
|
"configuration", help="Your YAML configuration file.", nargs=1
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
|
|
|
parser_logs.add_argument(
|
2021-06-08 01:14:12 +02:00
|
|
|
"--device",
|
|
|
|
help="Manually specify the serial port/address to use, for example /dev/ttyUSB0.",
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
parser_run = subparsers.add_parser(
|
|
|
|
"run",
|
2021-06-08 01:14:12 +02:00
|
|
|
help="Validate the configuration, create a binary, upload it, and start logs.",
|
|
|
|
parents=[mqtt_options],
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
|
|
|
parser_run.add_argument(
|
2021-06-08 01:14:12 +02:00
|
|
|
"configuration", help="Your YAML configuration file(s).", nargs="+"
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
|
|
|
parser_run.add_argument(
|
2021-06-08 01:14:12 +02:00
|
|
|
"--device",
|
|
|
|
help="Manually specify the serial port/address to use, for example /dev/ttyUSB0.",
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
|
|
|
parser_run.add_argument(
|
2021-06-08 01:14:12 +02:00
|
|
|
"--no-logs", help="Disable starting logs.", action="store_true"
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
parser_clean = subparsers.add_parser(
|
2021-06-08 01:14:12 +02:00
|
|
|
"clean-mqtt",
|
|
|
|
help="Helper to clear retained messages from an MQTT topic.",
|
|
|
|
parents=[mqtt_options],
|
|
|
|
)
|
|
|
|
parser_clean.add_argument(
|
|
|
|
"configuration", help="Your YAML configuration file(s).", nargs="+"
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
|
|
|
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_wizard = subparsers.add_parser(
|
2021-03-07 20:03:16 +01:00
|
|
|
"wizard",
|
2021-06-08 01:14:12 +02:00
|
|
|
help="A helpful setup wizard that will guide you through setting up ESPHome.",
|
|
|
|
)
|
2021-11-15 20:06:55 +01:00
|
|
|
parser_wizard.add_argument("configuration", help="Your YAML configuration file.")
|
2021-03-07 20:03:16 +01:00
|
|
|
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_fingerprint = subparsers.add_parser(
|
2021-03-07 20:03:16 +01:00
|
|
|
"mqtt-fingerprint", help="Get the SSL fingerprint from a MQTT broker."
|
|
|
|
)
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_fingerprint.add_argument(
|
|
|
|
"configuration", help="Your YAML configuration file(s).", nargs="+"
|
|
|
|
)
|
2021-03-07 20:03:16 +01:00
|
|
|
|
2021-06-08 01:14:12 +02:00
|
|
|
subparsers.add_parser("version", help="Print the ESPHome version and exit.")
|
2021-03-07 20:03:16 +01:00
|
|
|
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_clean = subparsers.add_parser(
|
|
|
|
"clean", help="Delete all temporary build files."
|
|
|
|
)
|
|
|
|
parser_clean.add_argument(
|
|
|
|
"configuration", help="Your YAML configuration file(s).", nargs="+"
|
|
|
|
)
|
2021-03-07 20:03:16 +01:00
|
|
|
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_dashboard = subparsers.add_parser(
|
2021-03-07 20:03:16 +01:00
|
|
|
"dashboard", help="Create a simple web server for a dashboard."
|
|
|
|
)
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_dashboard.add_argument(
|
2021-11-15 20:06:55 +01:00
|
|
|
"configuration", help="Your YAML configuration file directory."
|
2021-06-08 01:14:12 +02:00
|
|
|
)
|
|
|
|
parser_dashboard.add_argument(
|
2021-03-07 20:03:16 +01:00
|
|
|
"--port",
|
|
|
|
help="The HTTP port to open connections on. Defaults to 6052.",
|
|
|
|
type=int,
|
|
|
|
default=6052,
|
|
|
|
)
|
2021-11-24 19:59:32 +01:00
|
|
|
parser_dashboard.add_argument(
|
|
|
|
"--address",
|
|
|
|
help="The address to bind to.",
|
|
|
|
type=str,
|
|
|
|
default="0.0.0.0",
|
|
|
|
)
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_dashboard.add_argument(
|
2021-03-07 20:03:16 +01:00
|
|
|
"--username",
|
2021-06-08 01:14:12 +02:00
|
|
|
help="The optional username to require for authentication.",
|
2021-03-07 20:03:16 +01:00
|
|
|
type=str,
|
|
|
|
default="",
|
|
|
|
)
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_dashboard.add_argument(
|
2021-03-07 20:03:16 +01:00
|
|
|
"--password",
|
2021-06-08 01:14:12 +02:00
|
|
|
help="The optional password to require for authentication.",
|
2021-03-07 20:03:16 +01:00
|
|
|
type=str,
|
|
|
|
default="",
|
|
|
|
)
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_dashboard.add_argument(
|
2021-03-07 20:03:16 +01:00
|
|
|
"--open-ui", help="Open the dashboard UI in a browser.", action="store_true"
|
|
|
|
)
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_dashboard.add_argument(
|
2022-02-09 11:46:20 +01:00
|
|
|
"--ha-addon", help=argparse.SUPPRESS, action="store_true"
|
2021-06-08 01:14:12 +02:00
|
|
|
)
|
|
|
|
parser_dashboard.add_argument(
|
2021-03-07 20:03:16 +01:00
|
|
|
"--socket", help="Make the dashboard serve under a unix socket", type=str
|
|
|
|
)
|
|
|
|
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_vscode = subparsers.add_parser("vscode")
|
2021-08-11 07:21:57 +02:00
|
|
|
parser_vscode.add_argument("configuration", help="Your YAML configuration file.")
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_vscode.add_argument("--ace", action="store_true")
|
2021-03-07 20:03:16 +01:00
|
|
|
|
2021-06-08 01:14:12 +02:00
|
|
|
parser_update = subparsers.add_parser("update-all")
|
|
|
|
parser_update.add_argument(
|
2021-08-11 07:21:57 +02:00
|
|
|
"configuration", help="Your YAML configuration file directories.", nargs="+"
|
2021-06-08 01:14:12 +02:00
|
|
|
)
|
2019-06-07 14:26:28 +02:00
|
|
|
|
2021-10-17 08:54:09 +02:00
|
|
|
parser_idedata = subparsers.add_parser("idedata")
|
|
|
|
parser_idedata.add_argument(
|
|
|
|
"configuration", help="Your YAML configuration file(s).", nargs=1
|
|
|
|
)
|
|
|
|
|
2022-05-09 00:18:24 +02:00
|
|
|
parser_rename = subparsers.add_parser(
|
|
|
|
"rename",
|
|
|
|
help="Rename a device in YAML, compile the binary and upload it.",
|
|
|
|
)
|
2022-04-22 07:08:01 +02:00
|
|
|
parser_rename.add_argument(
|
|
|
|
"configuration", help="Your YAML configuration file.", nargs=1
|
|
|
|
)
|
|
|
|
parser_rename.add_argument("name", help="The new name for the device.", type=str)
|
|
|
|
|
2021-09-13 09:37:11 +02:00
|
|
|
# Keep backward compatibility with the old command line format of
|
|
|
|
# esphome <config> <command>.
|
|
|
|
#
|
|
|
|
# Unfortunately this can't be done by adding another configuration argument to the
|
|
|
|
# main config parser, as argparse is greedy when parsing arguments, so in regular
|
|
|
|
# usage it'll eat the command as the configuration argument and error out out
|
|
|
|
# because it can't parse the configuration as a command.
|
|
|
|
#
|
|
|
|
# Instead, if parsing using the current format fails, construct an ad-hoc parser
|
|
|
|
# that doesn't actually process the arguments, but parses them enough to let us
|
|
|
|
# figure out if the old format is used. In that case, swap the command and
|
|
|
|
# configuration in the arguments and retry with the normal parser (and raise
|
|
|
|
# a deprecation warning).
|
|
|
|
arguments = argv[1:]
|
|
|
|
|
|
|
|
# On Python 3.9+ we can simply set exit_on_error=False in the constructor
|
|
|
|
def _raise(x):
|
|
|
|
raise argparse.ArgumentError(None, x)
|
|
|
|
|
|
|
|
# First, try new-style parsing, but don't exit in case of failure
|
|
|
|
try:
|
|
|
|
# duplicate parser so that we can use the original one to raise errors later on
|
|
|
|
current_parser = argparse.ArgumentParser(add_help=False, parents=[parser])
|
|
|
|
current_parser.set_defaults(deprecated_argv_suggestion=None)
|
|
|
|
current_parser.error = _raise
|
|
|
|
return current_parser.parse_args(arguments)
|
|
|
|
except argparse.ArgumentError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Second, try compat parsing and rearrange the command-line if it succeeds
|
|
|
|
# Disable argparse's built-in help option and add it manually to prevent this
|
|
|
|
# parser from printing the help messagefor the old format when invoked with -h.
|
|
|
|
compat_parser = argparse.ArgumentParser(parents=[options_parser], add_help=False)
|
|
|
|
compat_parser.add_argument("-h", "--help", action="store_true")
|
|
|
|
compat_parser.add_argument("configuration", nargs="*")
|
|
|
|
compat_parser.add_argument(
|
|
|
|
"command",
|
|
|
|
choices=[
|
|
|
|
"config",
|
|
|
|
"compile",
|
|
|
|
"upload",
|
|
|
|
"logs",
|
|
|
|
"run",
|
|
|
|
"clean-mqtt",
|
|
|
|
"wizard",
|
|
|
|
"mqtt-fingerprint",
|
|
|
|
"version",
|
|
|
|
"clean",
|
|
|
|
"dashboard",
|
|
|
|
"vscode",
|
|
|
|
"update-all",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
compat_parser.error = _raise
|
|
|
|
result, unparsed = compat_parser.parse_known_args(argv[1:])
|
|
|
|
last_option = len(arguments) - len(unparsed) - 1 - len(result.configuration)
|
|
|
|
unparsed = [
|
|
|
|
"--device" if arg in ("--upload-port", "--serial-port") else arg
|
|
|
|
for arg in unparsed
|
|
|
|
]
|
|
|
|
arguments = (
|
|
|
|
arguments[0:last_option]
|
|
|
|
+ [result.command]
|
|
|
|
+ result.configuration
|
|
|
|
+ unparsed
|
|
|
|
)
|
|
|
|
deprecated_argv_suggestion = arguments
|
|
|
|
except argparse.ArgumentError:
|
|
|
|
# old-style parsing failed, don't suggest any argument
|
|
|
|
deprecated_argv_suggestion = None
|
|
|
|
|
|
|
|
# Finally, run the new-style parser again with the possibly swapped arguments,
|
|
|
|
# and let it error out if the command is unparsable.
|
|
|
|
parser.set_defaults(deprecated_argv_suggestion=deprecated_argv_suggestion)
|
|
|
|
return parser.parse_args(arguments)
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
|
2019-02-13 16:54:02 +01:00
|
|
|
def run_esphome(argv):
|
2018-05-21 16:40:22 +02:00
|
|
|
args = parse_args(argv)
|
2018-12-18 19:31:43 +01:00
|
|
|
CORE.dashboard = args.dashboard
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2021-10-17 21:01:51 +02:00
|
|
|
setup_log(
|
|
|
|
args.verbose,
|
|
|
|
args.quiet,
|
|
|
|
# Show timestamp for dashboard access logs
|
|
|
|
args.command == "dashboard",
|
|
|
|
)
|
2021-06-15 10:20:24 +02:00
|
|
|
if args.deprecated_argv_suggestion is not None and args.command != "vscode":
|
2021-06-08 01:14:12 +02:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Calling ESPHome with the configuration before the command is deprecated "
|
|
|
|
"and will be removed in the future. "
|
|
|
|
)
|
|
|
|
_LOGGER.warning("Please instead use:")
|
2021-09-13 09:37:11 +02:00
|
|
|
_LOGGER.warning(" esphome %s", " ".join(args.deprecated_argv_suggestion))
|
2019-05-31 10:43:11 +02:00
|
|
|
|
2022-02-10 09:55:11 +01:00
|
|
|
if sys.version_info < (3, 8, 0):
|
2021-03-07 20:03:16 +01:00
|
|
|
_LOGGER.error(
|
2022-02-10 09:55:11 +01:00
|
|
|
"You're running ESPHome with Python <3.8. ESPHome is no longer compatible "
|
|
|
|
"with this Python version. Please reinstall ESPHome with Python 3.8+"
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
2019-12-07 18:28:55 +01:00
|
|
|
return 1
|
2019-10-21 23:32:12 +02:00
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
if args.command in PRE_CONFIG_ACTIONS:
|
|
|
|
try:
|
|
|
|
return PRE_CONFIG_ACTIONS[args.command](args)
|
2019-02-13 16:54:02 +01:00
|
|
|
except EsphomeError as e:
|
2021-05-17 07:14:15 +02:00
|
|
|
_LOGGER.error(e, exc_info=args.verbose)
|
2018-05-21 16:40:22 +02:00
|
|
|
return 1
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2019-07-01 11:09:06 +02:00
|
|
|
for conf_path in args.configuration:
|
2021-11-15 20:06:55 +01:00
|
|
|
if any(os.path.basename(conf_path) == x for x in SECRETS_FILES):
|
|
|
|
_LOGGER.warning("Skipping secrets file %s", conf_path)
|
|
|
|
continue
|
|
|
|
|
2019-07-01 11:09:06 +02:00
|
|
|
CORE.config_path = conf_path
|
|
|
|
CORE.dashboard = args.dashboard
|
2018-04-18 18:43:13 +02:00
|
|
|
|
2020-06-21 20:33:01 +02:00
|
|
|
config = read_config(dict(args.substitution) if args.substitution else {})
|
2019-07-01 11:09:06 +02:00
|
|
|
if config is None:
|
2021-10-17 08:54:09 +02:00
|
|
|
return 2
|
2019-07-01 11:09:06 +02:00
|
|
|
CORE.config = config
|
|
|
|
|
|
|
|
if args.command not in POST_CONFIG_ACTIONS:
|
2019-12-07 18:28:55 +01:00
|
|
|
safe_print(f"Unknown command {args.command}")
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2018-05-20 12:41:52 +02:00
|
|
|
try:
|
2019-07-01 11:09:06 +02:00
|
|
|
rc = POST_CONFIG_ACTIONS[args.command](args, config)
|
2019-02-13 16:54:02 +01:00
|
|
|
except EsphomeError as e:
|
2021-05-17 07:14:15 +02:00
|
|
|
_LOGGER.error(e, exc_info=args.verbose)
|
2018-05-20 12:41:52 +02:00
|
|
|
return 1
|
2019-07-01 11:09:06 +02:00
|
|
|
if rc != 0:
|
|
|
|
return rc
|
|
|
|
|
|
|
|
CORE.reset()
|
|
|
|
return 0
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
def main():
|
|
|
|
try:
|
2019-02-13 16:54:02 +01:00
|
|
|
return run_esphome(sys.argv)
|
|
|
|
except EsphomeError as e:
|
2018-05-21 16:40:22 +02:00
|
|
|
_LOGGER.error(e)
|
|
|
|
return 1
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
2018-04-07 01:23:03 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|