1
0
mirror of https://github.com/esphome/esphome.git synced 2025-02-25 14:28:14 +00:00

[core] Remove old style platform configuration (#8118)

This commit is contained in:
Jesse Hills 2025-01-21 19:32:47 +13:00 committed by GitHub
parent db644542ed
commit b454f63b36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 27 additions and 80 deletions

View File

@ -1,21 +1,16 @@
import logging import logging
import multiprocessing import multiprocessing
import os import os
import re
from esphome import automation from esphome import automation
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.const import ( from esphome.const import (
CONF_ARDUINO_VERSION,
CONF_AREA, CONF_AREA,
CONF_BOARD,
CONF_BOARD_FLASH_MODE,
CONF_BUILD_PATH, CONF_BUILD_PATH,
CONF_COMMENT, CONF_COMMENT,
CONF_COMPILE_PROCESS_LIMIT, CONF_COMPILE_PROCESS_LIMIT,
CONF_ESPHOME, CONF_ESPHOME,
CONF_FRAMEWORK,
CONF_FRIENDLY_NAME, CONF_FRIENDLY_NAME,
CONF_INCLUDES, CONF_INCLUDES,
CONF_LIBRARIES, CONF_LIBRARIES,
@ -30,12 +25,9 @@ from esphome.const import (
CONF_PLATFORMIO_OPTIONS, CONF_PLATFORMIO_OPTIONS,
CONF_PRIORITY, CONF_PRIORITY,
CONF_PROJECT, CONF_PROJECT,
CONF_SOURCE,
CONF_TRIGGER_ID, CONF_TRIGGER_ID,
CONF_TYPE,
CONF_VERSION, CONF_VERSION,
KEY_CORE, KEY_CORE,
PLATFORM_ESP8266,
TARGET_PLATFORMS, TARGET_PLATFORMS,
__version__ as ESPHOME_VERSION, __version__ as ESPHOME_VERSION,
) )
@ -44,7 +36,6 @@ from esphome.helpers import copy_file_if_changed, get_str_env, walk_files
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
BUILD_FLASH_MODES = ["qio", "qout", "dio", "dout"]
StartupTrigger = cg.esphome_ns.class_( StartupTrigger = cg.esphome_ns.class_(
"StartupTrigger", cg.Component, automation.Trigger.template() "StartupTrigger", cg.Component, automation.Trigger.template()
) )
@ -58,8 +49,6 @@ ProjectUpdateTrigger = cg.esphome_ns.class_(
"ProjectUpdateTrigger", cg.Component, automation.Trigger.template(cg.std_string) "ProjectUpdateTrigger", cg.Component, automation.Trigger.template(cg.std_string)
) )
VERSION_REGEX = re.compile(r"^[0-9]+\.[0-9]+\.[0-9]+(?:[ab]\d+)?$")
VALID_INCLUDE_EXTS = {".h", ".hpp", ".tcc", ".ino", ".cpp", ".c"} VALID_INCLUDE_EXTS = {".h", ".hpp", ".tcc", ".ino", ".cpp", ".c"}
@ -111,7 +100,6 @@ else:
_compile_process_limit_default = cv.UNDEFINED _compile_process_limit_default = cv.UNDEFINED
CONF_ESP8266_RESTORE_FROM_FLASH = "esp8266_restore_from_flash"
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
cv.Schema( cv.Schema(
{ {
@ -175,14 +163,9 @@ PRELOAD_CONFIG_SCHEMA = cv.Schema(
{ {
cv.Required(CONF_NAME): cv.valid_name, cv.Required(CONF_NAME): cv.valid_name,
cv.Optional(CONF_BUILD_PATH): cv.string, cv.Optional(CONF_BUILD_PATH): cv.string,
# Compat options, these were moved to target-platform specific sections cv.Optional(CONF_PLATFORM): cv.invalid(
# but we'll keep these around for a long time because every config would "Please remove the `platform` key from the [esphome] block and use the correct platform component. This style of configuration has now been removed."
# 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,
cv.Optional(CONF_MIN_VERSION, default=ESPHOME_VERSION): cv.All( cv.Optional(CONF_MIN_VERSION, default=ESPHOME_VERSION): cv.All(
cv.version_number, cv.validate_esphome_version cv.version_number, cv.validate_esphome_version
), ),
@ -204,62 +187,20 @@ def preload_core_config(config, result):
conf[CONF_BUILD_PATH] = os.path.join(build_path, CORE.name) conf[CONF_BUILD_PATH] = os.path.join(build_path, CORE.name)
CORE.build_path = CORE.relative_internal_path(conf[CONF_BUILD_PATH]) CORE.build_path = CORE.relative_internal_path(conf[CONF_BUILD_PATH])
has_oldstyle = CONF_PLATFORM in conf target_platforms = [key for key in TARGET_PLATFORMS if key in config]
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: if not target_platforms:
raise cv.Invalid( raise cv.Invalid(
"Platform missing. You must include one of the available platform keys: " "Platform missing. You must include one of the available platform keys: "
+ ", ".join(TARGET_PLATFORMS), + ", ".join(TARGET_PLATFORMS),
[CONF_ESPHOME], [CONF_ESPHOME],
) )
if has_oldstyle and newstyle_found: if len(target_platforms) > 1:
raise cv.Invalid( 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", f"Found multiple target platform blocks: {', '.join(target_platforms)}. Only one is allowed.",
[CONF_ESPHOME, CONF_PLATFORM], [target_platforms[0]],
) )
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] = {}
if plat != PLATFORM_ESP8266:
plat_conf[CONF_FRAMEWORK][CONF_TYPE] = "arduino"
try:
if conf[CONF_ARDUINO_VERSION] not in ("recommended", "latest", "dev"):
cv.Version.parse(conf[CONF_ARDUINO_VERSION])
plat_conf[CONF_FRAMEWORK][CONF_VERSION] = conf.pop(CONF_ARDUINO_VERSION)
except ValueError:
plat_conf[CONF_FRAMEWORK][CONF_SOURCE] = 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 config[CONF_ESPHOME] = conf

View File

@ -1,7 +1,8 @@
--- ---
esphome: esphome:
name: test name: test
platform: ESP8266
esp8266:
board: d1_mini_lite board: d1_mini_lite
binary_sensor: binary_sensor:

View File

@ -1,7 +1,8 @@
--- ---
esphome: esphome:
name: test name: test
platform: ESP8266
esp8266:
board: d1_mini_lite board: d1_mini_lite
wifi: wifi:

View File

@ -1,7 +1,8 @@
--- ---
esphome: esphome:
name: test name: test
platform: ESP32
esp32:
board: nodemcu-32s board: nodemcu-32s
deep_sleep: deep_sleep:

View File

@ -1,7 +1,8 @@
--- ---
esphome: esphome:
name: test name: test
platform: ESP32
esp32:
board: nodemcu-32s board: nodemcu-32s
deep_sleep: deep_sleep:

View File

@ -1,7 +1,8 @@
--- ---
esphome: esphome:
name: test name: test
platform: ESP8266
esp8266:
board: d1_mini_lite board: d1_mini_lite
sensor: sensor:

View File

@ -1,7 +1,8 @@
--- ---
esphome: esphome:
name: test name: test
platform: ESP8266
esp8266:
board: d1_mini_lite board: d1_mini_lite
text_sensor: text_sensor:

View File

@ -12,7 +12,7 @@ esphome:
# not overwritten by vars in the !include above # not overwritten by vars in the !include above
name: ${name} name: ${name}
name_add_mac_suffix: true name_add_mac_suffix: true
platform: esp8266
board: !include {file: includes/scalar.yaml, vars: {var1: nodemcu}}
libraries: !include {file: includes/list.yaml, vars: {var1: Wire}} libraries: !include {file: includes/list.yaml, vars: {var1: Wire}}
esp8266:
board: !include {file: includes/scalar.yaml, vars: {var1: nodemcu}}

View File

@ -12,7 +12,7 @@ esphome:
# not overwritten by vars in the !include above # not overwritten by vars in the !include above
name: ${name} name: ${name}
name_add_mac_suffix: true name_add_mac_suffix: true
platform: esp8266
board: !include {file: includes/scalar.yaml, vars: {var1: nodemcu}}
libraries: !include {file: includes/list.yaml, vars: {var1: Wire}} libraries: !include {file: includes/list.yaml, vars: {var1: Wire}}
esp8266:
board: !include {file: includes/scalar.yaml, vars: {var1: nodemcu}}

View File

@ -10,7 +10,7 @@ def test_include_with_vars(fixture_path):
substitutions.do_substitution_pass(actual, None) substitutions.do_substitution_pass(actual, None)
assert actual["esphome"]["name"] == "original" assert actual["esphome"]["name"] == "original"
assert actual["esphome"]["libraries"][0] == "Wire" assert actual["esphome"]["libraries"][0] == "Wire"
assert actual["esphome"]["board"] == "nodemcu" assert actual["esp8266"]["board"] == "nodemcu"
assert actual["wifi"]["ssid"] == "my_custom_ssid" assert actual["wifi"]["ssid"] == "my_custom_ssid"