2018-09-29 14:50:04 +02:00
|
|
|
import logging
|
2018-09-23 18:58:41 +02:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
import esphome.codegen as cg
|
2019-02-13 16:54:02 +01:00
|
|
|
import esphome.config_validation as cv
|
2021-09-20 11:47:51 +02:00
|
|
|
from esphome import automation
|
2021-03-07 20:03:16 +01:00
|
|
|
from esphome.const import (
|
|
|
|
CONF_ARDUINO_VERSION,
|
|
|
|
CONF_BOARD,
|
|
|
|
CONF_BOARD_FLASH_MODE,
|
|
|
|
CONF_BUILD_PATH,
|
|
|
|
CONF_COMMENT,
|
|
|
|
CONF_ESPHOME,
|
2021-09-20 11:47:51 +02:00
|
|
|
CONF_FRAMEWORK,
|
2021-03-07 20:03:16 +01:00
|
|
|
CONF_INCLUDES,
|
|
|
|
CONF_LIBRARIES,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_ON_BOOT,
|
|
|
|
CONF_ON_LOOP,
|
|
|
|
CONF_ON_SHUTDOWN,
|
|
|
|
CONF_PLATFORM,
|
|
|
|
CONF_PLATFORMIO_OPTIONS,
|
|
|
|
CONF_PRIORITY,
|
2021-06-09 03:04:00 +02:00
|
|
|
CONF_PROJECT,
|
2021-03-07 20:03:16 +01:00
|
|
|
CONF_TRIGGER_ID,
|
2021-09-20 11:47:51 +02:00
|
|
|
CONF_TYPE,
|
2021-06-09 03:04:00 +02:00
|
|
|
CONF_VERSION,
|
2021-09-20 11:47:51 +02:00
|
|
|
KEY_CORE,
|
|
|
|
TARGET_PLATFORMS,
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
2019-04-22 21:56:30 +02:00
|
|
|
from esphome.core import CORE, coroutine_with_priority
|
2019-05-29 19:30:35 +02:00
|
|
|
from esphome.helpers import copy_file_if_changed, walk_files
|
2018-09-23 18:58:41 +02:00
|
|
|
|
2018-09-29 14:50:04 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
BUILD_FLASH_MODES = ["qio", "qout", "dio", "dout"]
|
|
|
|
StartupTrigger = cg.esphome_ns.class_(
|
|
|
|
"StartupTrigger", cg.Component, automation.Trigger.template()
|
|
|
|
)
|
|
|
|
ShutdownTrigger = cg.esphome_ns.class_(
|
|
|
|
"ShutdownTrigger", cg.Component, automation.Trigger.template()
|
|
|
|
)
|
|
|
|
LoopTrigger = cg.esphome_ns.class_(
|
|
|
|
"LoopTrigger", cg.Component, automation.Trigger.template()
|
|
|
|
)
|
2018-09-23 18:58:41 +02:00
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
VERSION_REGEX = re.compile(r"^[0-9]+\.[0-9]+\.[0-9]+(?:[ab]\d+)?$")
|
2018-09-23 18:58:41 +02:00
|
|
|
|
2021-03-17 21:22:48 +01:00
|
|
|
CONF_NAME_ADD_MAC_SUFFIX = "name_add_mac_suffix"
|
|
|
|
|
2018-09-23 18:58:41 +02:00
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
VALID_INCLUDE_EXTS = {".h", ".hpp", ".tcc", ".ino", ".cpp", ".c"}
|
2019-05-29 19:30:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
def valid_include(value):
|
|
|
|
try:
|
|
|
|
return cv.directory(value)
|
|
|
|
except cv.Invalid:
|
|
|
|
pass
|
|
|
|
value = cv.file_(value)
|
|
|
|
_, ext = os.path.splitext(value)
|
|
|
|
if ext not in VALID_INCLUDE_EXTS:
|
2021-03-07 20:03:16 +01:00
|
|
|
raise cv.Invalid(
|
2021-09-19 19:22:28 +02:00
|
|
|
f"Include has invalid file extension {ext} - valid extensions are {', '.join(VALID_INCLUDE_EXTS)}"
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
2019-05-29 19:30:35 +02:00
|
|
|
return value
|
|
|
|
|
|
|
|
|
2021-06-09 03:04:00 +02:00
|
|
|
def valid_project_name(value: str):
|
|
|
|
if value.count(".") != 1:
|
|
|
|
raise cv.Invalid("project name needs to have a namespace")
|
|
|
|
|
|
|
|
value = value.replace(" ", "_")
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
CONF_ESP8266_RESTORE_FROM_FLASH = "esp8266_restore_from_flash"
|
2021-03-07 20:03:16 +01:00
|
|
|
CONFIG_SCHEMA = cv.Schema(
|
|
|
|
{
|
2021-08-10 14:14:42 +02:00
|
|
|
cv.Required(CONF_NAME): cv.hostname,
|
2021-03-07 20:03:16 +01:00
|
|
|
cv.Optional(CONF_COMMENT): cv.string,
|
2021-09-20 11:47:51 +02:00
|
|
|
cv.Required(CONF_BUILD_PATH): cv.string,
|
2021-03-07 20:03:16 +01:00
|
|
|
cv.Optional(CONF_PLATFORMIO_OPTIONS, default={}): cv.Schema(
|
|
|
|
{
|
|
|
|
cv.string_strict: cv.Any([cv.string], cv.string),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
cv.Optional(CONF_ON_BOOT): automation.validate_automation(
|
|
|
|
{
|
|
|
|
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(StartupTrigger),
|
|
|
|
cv.Optional(CONF_PRIORITY, default=600.0): cv.float_,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
cv.Optional(CONF_ON_SHUTDOWN): automation.validate_automation(
|
|
|
|
{
|
|
|
|
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ShutdownTrigger),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
cv.Optional(CONF_ON_LOOP): automation.validate_automation(
|
|
|
|
{
|
|
|
|
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(LoopTrigger),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
cv.Optional(CONF_INCLUDES, default=[]): cv.ensure_list(valid_include),
|
|
|
|
cv.Optional(CONF_LIBRARIES, default=[]): cv.ensure_list(cv.string_strict),
|
2021-03-17 21:22:48 +01:00
|
|
|
cv.Optional(CONF_NAME_ADD_MAC_SUFFIX, default=False): cv.boolean,
|
2021-06-09 03:04:00 +02:00
|
|
|
cv.Optional(CONF_PROJECT): cv.Schema(
|
|
|
|
{
|
|
|
|
cv.Required(CONF_NAME): cv.All(cv.string_strict, valid_project_name),
|
|
|
|
cv.Required(CONF_VERSION): cv.string_strict,
|
|
|
|
}
|
|
|
|
),
|
2021-03-07 20:03:16 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
PRELOAD_CONFIG_SCHEMA = cv.Schema(
|
|
|
|
{
|
|
|
|
cv.Required(CONF_NAME): cv.valid_name,
|
2021-09-20 11:47:51 +02:00
|
|
|
cv.Optional(CONF_BUILD_PATH): cv.string,
|
|
|
|
# Compat options, these were moved to target-platform specific sections
|
|
|
|
# but we'll keep these around for a long time because every config would
|
|
|
|
# be impacted
|
|
|
|
cv.Optional(CONF_PLATFORM): cv.one_of(*TARGET_PLATFORMS, lower=True),
|
|
|
|
cv.Optional(CONF_BOARD): cv.string_strict,
|
|
|
|
cv.Optional(CONF_ESP8266_RESTORE_FROM_FLASH): cv.valid,
|
|
|
|
cv.Optional(CONF_BOARD_FLASH_MODE): cv.valid,
|
|
|
|
cv.Optional(CONF_ARDUINO_VERSION): cv.valid,
|
2021-03-07 20:03:16 +01:00
|
|
|
},
|
|
|
|
extra=cv.ALLOW_EXTRA,
|
|
|
|
)
|
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
def preload_core_config(config, result):
|
|
|
|
with cv.prepend_path(CONF_ESPHOME):
|
|
|
|
conf = PRELOAD_CONFIG_SCHEMA(config[CONF_ESPHOME])
|
|
|
|
|
|
|
|
CORE.name = conf[CONF_NAME]
|
|
|
|
CORE.data[KEY_CORE] = {}
|
|
|
|
|
|
|
|
if CONF_BUILD_PATH not in conf:
|
|
|
|
conf[CONF_BUILD_PATH] = CORE.name
|
|
|
|
CORE.build_path = CORE.relative_config_path(conf[CONF_BUILD_PATH])
|
2018-09-23 18:58:41 +02:00
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
has_oldstyle = CONF_PLATFORM in conf
|
|
|
|
newstyle_found = [key for key in TARGET_PLATFORMS if key in config]
|
|
|
|
oldstyle_opts = [
|
|
|
|
CONF_ESP8266_RESTORE_FROM_FLASH,
|
|
|
|
CONF_BOARD_FLASH_MODE,
|
|
|
|
CONF_ARDUINO_VERSION,
|
|
|
|
CONF_BOARD,
|
|
|
|
]
|
|
|
|
|
|
|
|
if not has_oldstyle and not newstyle_found:
|
|
|
|
raise cv.Invalid("Platform missing for core options!", [CONF_ESPHOME])
|
|
|
|
if has_oldstyle and newstyle_found:
|
|
|
|
raise cv.Invalid(
|
|
|
|
f"Please remove the `platform` key from the [esphome] block. You're already using the new style with the [{conf[CONF_PLATFORM]}] block",
|
|
|
|
[CONF_ESPHOME, CONF_PLATFORM],
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
2021-09-20 11:47:51 +02:00
|
|
|
if len(newstyle_found) > 1:
|
|
|
|
raise cv.Invalid(
|
|
|
|
f"Found multiple target platform blocks: {', '.join(newstyle_found)}. Only one is allowed.",
|
|
|
|
[newstyle_found[0]],
|
|
|
|
)
|
|
|
|
if newstyle_found:
|
|
|
|
# Convert to newstyle
|
|
|
|
for key in oldstyle_opts:
|
|
|
|
if key in conf:
|
|
|
|
raise cv.Invalid(
|
|
|
|
f"Please move {key} to the [{newstyle_found[0]}] block.",
|
|
|
|
[CONF_ESPHOME, key],
|
|
|
|
)
|
|
|
|
|
|
|
|
if has_oldstyle:
|
|
|
|
plat = conf.pop(CONF_PLATFORM)
|
|
|
|
plat_conf = {}
|
|
|
|
if CONF_ESP8266_RESTORE_FROM_FLASH in conf:
|
|
|
|
plat_conf["restore_from_flash"] = conf.pop(CONF_ESP8266_RESTORE_FROM_FLASH)
|
|
|
|
if CONF_BOARD_FLASH_MODE in conf:
|
|
|
|
plat_conf[CONF_BOARD_FLASH_MODE] = conf.pop(CONF_BOARD_FLASH_MODE)
|
|
|
|
if CONF_ARDUINO_VERSION in conf:
|
|
|
|
plat_conf[CONF_FRAMEWORK] = {
|
|
|
|
CONF_TYPE: "arduino",
|
|
|
|
CONF_VERSION: conf.pop(CONF_ARDUINO_VERSION),
|
|
|
|
}
|
|
|
|
if CONF_BOARD in conf:
|
|
|
|
plat_conf[CONF_BOARD] = conf.pop(CONF_BOARD)
|
|
|
|
# Insert generated target platform config to main config
|
|
|
|
config[plat] = plat_conf
|
|
|
|
config[CONF_ESPHOME] = conf
|
2018-09-29 14:50:04 +02:00
|
|
|
|
|
|
|
|
2019-05-29 19:30:35 +02:00
|
|
|
def include_file(path, basename):
|
|
|
|
parts = basename.split(os.path.sep)
|
|
|
|
dst = CORE.relative_src_path(*parts)
|
|
|
|
copy_file_if_changed(path, dst)
|
|
|
|
|
|
|
|
_, ext = os.path.splitext(path)
|
2021-03-07 20:03:16 +01:00
|
|
|
if ext in [".h", ".hpp", ".tcc"]:
|
2019-05-29 19:30:35 +02:00
|
|
|
# Header, add include statement
|
2019-12-07 18:28:55 +01:00
|
|
|
cg.add_global(cg.RawStatement(f'#include "{basename}"'))
|
2019-05-29 19:30:35 +02:00
|
|
|
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
@coroutine_with_priority(-1000.0)
|
2021-05-17 07:14:15 +02:00
|
|
|
async def add_includes(includes):
|
2019-04-17 12:06:00 +02:00
|
|
|
# Add includes at the very end, so that the included files can access global variables
|
|
|
|
for include in includes:
|
2019-04-22 21:56:30 +02:00
|
|
|
path = CORE.relative_config_path(include)
|
2019-05-29 19:30:35 +02:00
|
|
|
if os.path.isdir(path):
|
|
|
|
# Directory, copy tree
|
|
|
|
for p in walk_files(path):
|
|
|
|
basename = os.path.relpath(p, os.path.dirname(path))
|
|
|
|
include_file(p, basename)
|
|
|
|
else:
|
|
|
|
# Copy file
|
|
|
|
basename = os.path.basename(path)
|
|
|
|
include_file(path, basename)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
|
2020-07-27 18:22:47 +02:00
|
|
|
@coroutine_with_priority(-1000.0)
|
2021-09-20 11:47:51 +02:00
|
|
|
async def _add_platformio_options(pio_options):
|
|
|
|
# Add includes at the very end, so that they override everything
|
|
|
|
for key, val in pio_options.items():
|
|
|
|
cg.add_platformio_option(key, val)
|
2020-07-27 18:22:47 +02:00
|
|
|
|
|
|
|
|
2021-01-08 20:11:42 +01:00
|
|
|
@coroutine_with_priority(30.0)
|
2021-05-17 07:14:15 +02:00
|
|
|
async def _add_automations(config):
|
2018-09-23 18:58:41 +02:00
|
|
|
for conf in config.get(CONF_ON_BOOT, []):
|
2019-04-17 12:06:00 +02:00
|
|
|
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf.get(CONF_PRIORITY))
|
2021-05-17 07:14:15 +02:00
|
|
|
await cg.register_component(trigger, conf)
|
|
|
|
await automation.build_automation(trigger, [], conf)
|
2018-09-23 18:58:41 +02:00
|
|
|
|
|
|
|
for conf in config.get(CONF_ON_SHUTDOWN, []):
|
2019-04-17 12:06:00 +02:00
|
|
|
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
|
2021-05-17 07:14:15 +02:00
|
|
|
await cg.register_component(trigger, conf)
|
|
|
|
await automation.build_automation(trigger, [], conf)
|
2018-09-23 18:58:41 +02:00
|
|
|
|
|
|
|
for conf in config.get(CONF_ON_LOOP, []):
|
2019-04-17 12:06:00 +02:00
|
|
|
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
|
2021-05-17 07:14:15 +02:00
|
|
|
await cg.register_component(trigger, conf)
|
|
|
|
await automation.build_automation(trigger, [], conf)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2021-01-08 20:11:42 +01:00
|
|
|
|
|
|
|
@coroutine_with_priority(100.0)
|
2021-05-17 07:14:15 +02:00
|
|
|
async def to_code(config):
|
2021-03-07 20:03:16 +01:00
|
|
|
cg.add_global(cg.global_ns.namespace("esphome").using)
|
|
|
|
cg.add(
|
2021-03-17 21:22:48 +01:00
|
|
|
cg.App.pre_setup(
|
|
|
|
config[CONF_NAME],
|
|
|
|
cg.RawExpression('__DATE__ ", " __TIME__'),
|
|
|
|
config[CONF_NAME_ADD_MAC_SUFFIX],
|
|
|
|
)
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
2021-01-08 20:11:42 +01:00
|
|
|
|
|
|
|
CORE.add_job(_add_automations, config)
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
cg.add_build_flag("-fno-exceptions")
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
# Libraries
|
|
|
|
for lib in config[CONF_LIBRARIES]:
|
2021-03-07 20:03:16 +01:00
|
|
|
if "@" in lib:
|
|
|
|
name, vers = lib.split("@", 1)
|
2019-04-17 12:06:00 +02:00
|
|
|
cg.add_library(name, vers)
|
2021-07-26 10:50:45 +02:00
|
|
|
elif "://" in lib:
|
|
|
|
# Repository...
|
|
|
|
if "=" in lib:
|
|
|
|
name, repo = lib.split("=", 1)
|
|
|
|
cg.add_library(name, None, repo)
|
|
|
|
else:
|
|
|
|
cg.add_library(None, None, lib)
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
else:
|
|
|
|
cg.add_library(lib, None)
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
cg.add_build_flag("-Wno-unused-variable")
|
|
|
|
cg.add_build_flag("-Wno-unused-but-set-variable")
|
|
|
|
cg.add_build_flag("-Wno-sign-compare")
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
if config[CONF_INCLUDES]:
|
|
|
|
CORE.add_job(add_includes, config[CONF_INCLUDES])
|
2021-06-09 03:04:00 +02:00
|
|
|
|
|
|
|
if CONF_PROJECT in config:
|
|
|
|
cg.add_define("ESPHOME_PROJECT_NAME", config[CONF_PROJECT][CONF_NAME])
|
|
|
|
cg.add_define("ESPHOME_PROJECT_VERSION", config[CONF_PROJECT][CONF_VERSION])
|
2021-09-20 11:47:51 +02:00
|
|
|
|
|
|
|
if config[CONF_PLATFORMIO_OPTIONS]:
|
|
|
|
CORE.add_job(_add_platformio_options, config[CONF_PLATFORMIO_OPTIONS])
|