1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-05 21:02:20 +01:00

Merge components in packages (#3555)

Co-authored-by: Paul Monigatti <pm@paul.pm>
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Quentin Smith
2023-02-06 20:08:40 -05:00
committed by GitHub
parent 393ca64d70
commit 40df3aa55e
7 changed files with 408 additions and 3 deletions

View File

@@ -1,10 +1,27 @@
import json
import os
from esphome.const import CONF_ID
from esphome.core import CORE
from esphome.helpers import read_file
class Extend:
def __init__(self, value):
self.value = value
def __str__(self):
return f"!extend {self.value}"
def __eq__(self, b):
"""
Check if two Extend objects contain the same ID.
Only used in unit tests.
"""
return isinstance(b, Extend) and self.value == b.value
def read_config_file(path: str) -> str:
if CORE.vscode and (
not CORE.ace or os.path.abspath(path) == os.path.abspath(CORE.config_path)
@@ -36,7 +53,25 @@ def merge_config(full_old, full_new):
if isinstance(new, list):
if not isinstance(old, list):
return new
return old + new
res = old.copy()
ids = {
v[CONF_ID]: i
for i, v in enumerate(res)
if CONF_ID in v and isinstance(v[CONF_ID], str)
}
for v in new:
if CONF_ID in v:
new_id = v[CONF_ID]
if isinstance(new_id, Extend):
new_id = new_id.value
if new_id in ids:
v[CONF_ID] = new_id
res[ids[new_id]] = merge(res[ids[new_id]], v)
continue
else:
ids[new_id] = len(res)
res.append(v)
return res
if new is None:
return old