mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Fix issues with YAML include tag.
Calling load_yaml_config_file(config_path) causes issues when trying to load from multiple files in a directory since config_path is modified in the closure. This also separates the parse code block into its own function so that there is no restriction that the object be a dictionary for included files. This means that scalars, lists, etc. can also be stored in separate files -- for example, the scene component expects a list.
This commit is contained in:
parent
e0d697d0bc
commit
fc560d179e
@ -103,6 +103,19 @@ def load_yaml_config_file(config_path):
|
||||
""" Parse a YAML configuration file. """
|
||||
import yaml
|
||||
|
||||
def parse(fname):
|
||||
""" Actually parse the file. """
|
||||
try:
|
||||
with open(fname) as conf_file:
|
||||
# If configuration file is empty YAML returns None
|
||||
# We convert that to an empty dict
|
||||
conf_dict = yaml.load(conf_file) or {}
|
||||
except yaml.YAMLError:
|
||||
_LOGGER.exception('Error reading YAML configuration file %s',
|
||||
fname)
|
||||
raise HomeAssistantError()
|
||||
return conf_dict
|
||||
|
||||
def yaml_include(loader, node):
|
||||
"""
|
||||
Loads another YAML file and embeds it using the !include tag.
|
||||
@ -110,20 +123,12 @@ def load_yaml_config_file(config_path):
|
||||
Example:
|
||||
device_tracker: !include device_tracker.yaml
|
||||
"""
|
||||
fname = os.path.join(os.path.dirname(config_path), node.value)
|
||||
return load_yaml_config_file(fname)
|
||||
fname = os.path.join(os.path.dirname(loader.name), node.value)
|
||||
return parse(fname)
|
||||
|
||||
yaml.add_constructor('!include', yaml_include)
|
||||
|
||||
try:
|
||||
with open(config_path) as conf_file:
|
||||
# If configuration file is empty YAML returns None
|
||||
# We convert that to an empty dict
|
||||
conf_dict = yaml.load(conf_file) or {}
|
||||
|
||||
except yaml.YAMLError:
|
||||
_LOGGER.exception('Error reading YAML configuration file')
|
||||
raise HomeAssistantError()
|
||||
conf_dict = parse(config_path)
|
||||
|
||||
if not isinstance(conf_dict, dict):
|
||||
_LOGGER.error(
|
||||
|
Loading…
x
Reference in New Issue
Block a user