Don't remove location information for packages (#1133)

This commit is contained in:
Otto Winter 2020-07-15 15:27:24 +02:00 committed by GitHub
parent dcadcdf056
commit 4752096520
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 41 deletions

View File

@ -1,51 +1,44 @@
from deepmerge import conservative_merger as package_merger
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.const import CONF_PACKAGES from esphome.const import CONF_PACKAGES
VALID_PACKAGE_NAME_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
def _merge_package(full_old, full_new):
def _merge_package(config, package_name, package_config): def merge(old, new):
config = config.copy() # pylint: disable=no-else-return
package_merger.merge(config, package_config) if isinstance(new, dict):
return config if not isinstance(old, dict):
return new
res = old.copy()
for k, v in new.items():
res[k] = merge(old[k], v) if k in old else v
return res
elif isinstance(new, list):
if not isinstance(old, list):
return new
return old + new
return new
def _is_valid_package_name(value: str) -> bool: return merge(full_old, full_new)
if not value:
return False
if value[0].isdigit():
return False
try:
cv.valid_name(value)
except cv.Invalid:
return False
return True
def do_packages_pass(config: dict): def do_packages_pass(config: dict):
if CONF_PACKAGES not in config: if CONF_PACKAGES not in config:
return return config
packages = config[CONF_PACKAGES] packages = config[CONF_PACKAGES]
temp_config = config.copy()
with cv.prepend_path(CONF_PACKAGES): with cv.prepend_path(CONF_PACKAGES):
if packages is not None and not isinstance(packages, dict): if not isinstance(packages, dict):
raise cv.Invalid("Packages must be a key to value mapping, got {} instead" raise cv.Invalid("Packages must be a key to value mapping, got {} instead"
"".format(type(packages))) "".format(type(packages)))
for package_name, package_config in packages.items(): for package_name, package_config in packages.items():
with cv.prepend_path(package_name): with cv.prepend_path(package_name):
if not isinstance(package_config, dict): recursive_package = package_config
raise cv.Invalid("Package definition must be a dictionary containing valid " if isinstance(package_config, dict):
"esphome configuration to be merged with the main " recursive_package = do_packages_pass(package_config)
"config, got {} instead" config = _merge_package(config, recursive_package)
.format(type(package_config)))
if not _is_valid_package_name(package_name): del config[CONF_PACKAGES]
raise cv.Invalid("Package name is invalid. Valid name should consist of " return config
"letters, numbers and underscores. It shouldn't also "
"start with number")
temp_config = _merge_package(temp_config, package_name, package_config)
del temp_config[CONF_PACKAGES]
config.clear()
config.update(temp_config)

View File

@ -3,11 +3,10 @@ import re
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import core from esphome import core
from esphome.const import CONF_SUBSTITUTIONS
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_SUBSTITUTIONS = 'substitutions'
VALID_SUBSTITUTIONS_CHARACTERS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \ VALID_SUBSTITUTIONS_CHARACTERS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \
'0123456789_' '0123456789_'

View File

@ -11,10 +11,8 @@ from contextlib import contextmanager
import voluptuous as vol import voluptuous as vol
from esphome import core, core_config, yaml_util from esphome import core, core_config, yaml_util
from esphome.components import substitutions from esphome.const import CONF_ESPHOME, CONF_PLATFORM, ESP_PLATFORMS, CONF_PACKAGES, \
from esphome.components.packages import do_packages_pass CONF_SUBSTITUTIONS
from esphome.components.substitutions import CONF_SUBSTITUTIONS
from esphome.const import CONF_ESPHOME, CONF_PLATFORM, ESP_PLATFORMS, CONF_PACKAGES
from esphome.core import CORE, EsphomeError # noqa from esphome.core import CORE, EsphomeError # noqa
from esphome.helpers import color, indent from esphome.helpers import color, indent
from esphome.util import safe_print, OrderedDict from esphome.util import safe_print, OrderedDict
@ -393,9 +391,10 @@ def validate_config(config, command_line_substitutions):
# 0. Load packages # 0. Load packages
if CONF_PACKAGES in config: if CONF_PACKAGES in config:
from esphome.components.packages import do_packages_pass
result.add_output_path([CONF_PACKAGES], CONF_PACKAGES) result.add_output_path([CONF_PACKAGES], CONF_PACKAGES)
try: try:
do_packages_pass(config) config = do_packages_pass(config)
except vol.Invalid as err: except vol.Invalid as err:
result.update(config) result.update(config)
result.add_error(err) result.add_error(err)
@ -403,6 +402,7 @@ def validate_config(config, command_line_substitutions):
# 1. Load substitutions # 1. Load substitutions
if CONF_SUBSTITUTIONS in config: if CONF_SUBSTITUTIONS in config:
from esphome.components import substitutions
result[CONF_SUBSTITUTIONS] = {**config[CONF_SUBSTITUTIONS], **command_line_substitutions} result[CONF_SUBSTITUTIONS] = {**config[CONF_SUBSTITUTIONS], **command_line_substitutions}
result.add_output_path([CONF_SUBSTITUTIONS], CONF_SUBSTITUTIONS) result.add_output_path([CONF_SUBSTITUTIONS], CONF_SUBSTITUTIONS)
try: try:

View File

@ -467,6 +467,7 @@ CONF_STEP_PIN = 'step_pin'
CONF_STOP = 'stop' CONF_STOP = 'stop'
CONF_STOP_ACTION = 'stop_action' CONF_STOP_ACTION = 'stop_action'
CONF_SUBNET = 'subnet' CONF_SUBNET = 'subnet'
CONF_SUBSTITUTIONS = 'substitutions'
CONF_SUPPORTS_COOL = 'supports_cool' CONF_SUPPORTS_COOL = 'supports_cool'
CONF_SUPPORTS_HEAT = 'supports_heat' CONF_SUPPORTS_HEAT = 'supports_heat'
CONF_SWING_BOTH_ACTION = 'swing_both_action' CONF_SWING_BOTH_ACTION = 'swing_both_action'

View File

@ -11,4 +11,3 @@ ifaddr==0.1.7
platformio==4.3.4 platformio==4.3.4
esptool==2.8 esptool==2.8
click==7.1.2 click==7.1.2
deepmerge==0.1.0