updates
This commit is contained in:
@@ -263,6 +263,40 @@ def recorder_config(all_devices):
|
||||
}
|
||||
|
||||
|
||||
def merge_configs(base_config, override_config):
|
||||
"""
|
||||
Merge two configuration dictionaries by combining list-type entries.
|
||||
|
||||
For keys that exist in both configs and have list values in override_config,
|
||||
the lists are concatenated. If base_config has a dict value for that key,
|
||||
it's first converted to a single-element list before extending.
|
||||
|
||||
Keys with list values are moved from override_config to base_config.
|
||||
Non-list entries in override_config remain unchanged.
|
||||
|
||||
Args:
|
||||
base_config: The base configuration dict to merge into (modified in place)
|
||||
override_config: The override configuration dict (list entries are removed)
|
||||
|
||||
Returns:
|
||||
tuple: (base_config, override_config) after merging
|
||||
"""
|
||||
keys_to_merge = []
|
||||
for key in override_config:
|
||||
if isinstance(override_config[key], list):
|
||||
keys_to_merge.append(key)
|
||||
for key in keys_to_merge:
|
||||
override_value = override_config[key]
|
||||
if key not in base_config:
|
||||
base_config[key] = []
|
||||
# Handle case where base_config[key] is a dict (convert to list first)
|
||||
if isinstance(base_config[key], dict):
|
||||
base_config[key] = [base_config[key]]
|
||||
base_config[key].extend(override_value)
|
||||
del override_config[key]
|
||||
return base_config, override_config
|
||||
|
||||
|
||||
def create_config(target_directory, development=False):
|
||||
groups_yaml_path = os.path.join(script_path, 'groups.yaml')
|
||||
manual_config_path = os.path.join(script_path, 'manual_config.yaml')
|
||||
@@ -282,12 +316,20 @@ def create_config(target_directory, development=False):
|
||||
|
||||
with open(os.path.join(target_directory, 'configuration.yaml'), 'w', encoding="utf-8") as output:
|
||||
output.write("# Dont' edit manually! this is generated!\n\n")
|
||||
for key in ['sensor', 'switch', 'light', 'cover', 'binary_sensor']:
|
||||
if key in manual_config_dict:
|
||||
if key not in all_devices:
|
||||
all_devices[key] = []
|
||||
all_devices[key].extend(manual_config_dict[key])
|
||||
del manual_config_dict[key]
|
||||
# Merge list-type configurations from manual_config into all_devices
|
||||
keys_to_merge = []
|
||||
for key in manual_config_dict:
|
||||
if isinstance(manual_config_dict[key], list):
|
||||
keys_to_merge.append(key)
|
||||
for key in keys_to_merge:
|
||||
manual_value = manual_config_dict[key]
|
||||
if key not in all_devices:
|
||||
all_devices[key] = []
|
||||
# Handle case where all_devices[key] is a dict (convert to list first)
|
||||
if isinstance(all_devices[key], dict):
|
||||
all_devices[key] = [all_devices[key]]
|
||||
all_devices[key].extend(manual_value)
|
||||
del manual_config_dict[key]
|
||||
yaml.dump(manual_config_dict, output)
|
||||
yaml.dump(all_devices, output)
|
||||
yaml.dump(logbook_config(all_devices), output)
|
||||
|
||||
Reference in New Issue
Block a user