mirror of
https://github.com/esphome/esphome.git
synced 2025-02-17 00:33:14 +01:00
Add support for command-line substitutions (#1014)
* Add support for command-line substitutions * Fix flake8 * pylint fixes
This commit is contained in:
parent
f9a8629157
commit
ecb1c77f8b
4 changed files with 19 additions and 13 deletions
|
@ -428,6 +428,8 @@ def parse_args(argv):
|
||||||
parser.add_argument('-q', '--quiet', help="Disable all esphome logs.",
|
parser.add_argument('-q', '--quiet', help="Disable all esphome logs.",
|
||||||
action='store_true')
|
action='store_true')
|
||||||
parser.add_argument('--dashboard', help=argparse.SUPPRESS, action='store_true')
|
parser.add_argument('--dashboard', help=argparse.SUPPRESS, action='store_true')
|
||||||
|
parser.add_argument('-s', '--substitution', nargs=2, action='append',
|
||||||
|
help='Add a substitution', metavar=('key', 'value'))
|
||||||
parser.add_argument('configuration', help='Your YAML configuration file.', nargs='*')
|
parser.add_argument('configuration', help='Your YAML configuration file.', nargs='*')
|
||||||
|
|
||||||
subparsers = parser.add_subparsers(help='Commands', dest='command')
|
subparsers = parser.add_subparsers(help='Commands', dest='command')
|
||||||
|
@ -532,7 +534,7 @@ def run_esphome(argv):
|
||||||
CORE.config_path = conf_path
|
CORE.config_path = conf_path
|
||||||
CORE.dashboard = args.dashboard
|
CORE.dashboard = args.dashboard
|
||||||
|
|
||||||
config = read_config()
|
config = read_config(dict(args.substitution) if args.substitution else {})
|
||||||
if config is None:
|
if config is None:
|
||||||
return 1
|
return 1
|
||||||
CORE.config = config
|
CORE.config = config
|
||||||
|
|
|
@ -101,11 +101,15 @@ def _substitute_item(substitutions, item, path):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def do_substitution_pass(config):
|
def do_substitution_pass(config, command_line_substitutions):
|
||||||
if CONF_SUBSTITUTIONS not in config:
|
if CONF_SUBSTITUTIONS not in config and not command_line_substitutions:
|
||||||
return
|
return
|
||||||
|
|
||||||
substitutions = config[CONF_SUBSTITUTIONS]
|
substitutions = config[CONF_SUBSTITUTIONS]
|
||||||
|
if substitutions is None:
|
||||||
|
substitutions = command_line_substitutions
|
||||||
|
elif command_line_substitutions:
|
||||||
|
substitutions = {**substitutions, **command_line_substitutions}
|
||||||
with cv.prepend_path('substitutions'):
|
with cv.prepend_path('substitutions'):
|
||||||
if not isinstance(substitutions, dict):
|
if not isinstance(substitutions, dict):
|
||||||
raise cv.Invalid("Substitutions must be a key to value mapping, got {}"
|
raise cv.Invalid("Substitutions must be a key to value mapping, got {}"
|
||||||
|
|
|
@ -387,15 +387,15 @@ def recursive_check_replaceme(value):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def validate_config(config):
|
def validate_config(config, command_line_substitutions):
|
||||||
result = Config()
|
result = Config()
|
||||||
|
|
||||||
# 1. Load substitutions
|
# 1. Load substitutions
|
||||||
if CONF_SUBSTITUTIONS in config:
|
if CONF_SUBSTITUTIONS in config:
|
||||||
result[CONF_SUBSTITUTIONS] = config[CONF_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:
|
||||||
substitutions.do_substitution_pass(config)
|
substitutions.do_substitution_pass(config, command_line_substitutions)
|
||||||
except vol.Invalid as err:
|
except vol.Invalid as err:
|
||||||
result.add_error(err)
|
result.add_error(err)
|
||||||
return result
|
return result
|
||||||
|
@ -656,7 +656,7 @@ class InvalidYAMLError(EsphomeError):
|
||||||
self.base_exc = base_exc
|
self.base_exc = base_exc
|
||||||
|
|
||||||
|
|
||||||
def _load_config():
|
def _load_config(command_line_substitutions):
|
||||||
try:
|
try:
|
||||||
config = yaml_util.load_yaml(CORE.config_path)
|
config = yaml_util.load_yaml(CORE.config_path)
|
||||||
except EsphomeError as e:
|
except EsphomeError as e:
|
||||||
|
@ -664,7 +664,7 @@ def _load_config():
|
||||||
CORE.raw_config = config
|
CORE.raw_config = config
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = validate_config(config)
|
result = validate_config(config, command_line_substitutions)
|
||||||
except EsphomeError:
|
except EsphomeError:
|
||||||
raise
|
raise
|
||||||
except Exception:
|
except Exception:
|
||||||
|
@ -674,9 +674,9 @@ def _load_config():
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def load_config():
|
def load_config(command_line_substitutions):
|
||||||
try:
|
try:
|
||||||
return _load_config()
|
return _load_config(command_line_substitutions)
|
||||||
except vol.Invalid as err:
|
except vol.Invalid as err:
|
||||||
raise EsphomeError(f"Error while parsing config: {err}")
|
raise EsphomeError(f"Error while parsing config: {err}")
|
||||||
|
|
||||||
|
@ -813,10 +813,10 @@ def strip_default_ids(config):
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
def read_config():
|
def read_config(command_line_substitutions):
|
||||||
_LOGGER.info("Reading configuration %s...", CORE.config_path)
|
_LOGGER.info("Reading configuration %s...", CORE.config_path)
|
||||||
try:
|
try:
|
||||||
res = load_config()
|
res = load_config(command_line_substitutions)
|
||||||
except EsphomeError as err:
|
except EsphomeError as err:
|
||||||
_LOGGER.error("Error while reading config: %s", err)
|
_LOGGER.error("Error while reading config: %s", err)
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -66,7 +66,7 @@ def read_config(args):
|
||||||
CORE.config_path = data['file']
|
CORE.config_path = data['file']
|
||||||
vs = VSCodeResult()
|
vs = VSCodeResult()
|
||||||
try:
|
try:
|
||||||
res = load_config()
|
res = load_config(dict(args.substitution) if args.substitution else {})
|
||||||
except Exception as err: # pylint: disable=broad-except
|
except Exception as err: # pylint: disable=broad-except
|
||||||
vs.add_yaml_error(str(err))
|
vs.add_yaml_error(str(err))
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Reference in a new issue