2019-04-22 21:56:30 +02:00
|
|
|
import json
|
2019-05-11 11:41:09 +02:00
|
|
|
import os
|
2019-04-22 21:56:30 +02:00
|
|
|
|
2023-02-06 20:08:40 -05:00
|
|
|
from esphome.const import CONF_ID
|
2019-10-24 21:53:42 +02:00
|
|
|
from esphome.core import CORE
|
|
|
|
from esphome.helpers import read_file
|
2019-04-22 21:56:30 +02:00
|
|
|
|
|
|
|
|
2023-02-06 20:08:40 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-10-05 20:09:27 +13:00
|
|
|
def read_config_file(path: str) -> str:
|
2021-03-07 16:03:16 -03:00
|
|
|
if CORE.vscode and (
|
|
|
|
not CORE.ace or os.path.abspath(path) == os.path.abspath(CORE.config_path)
|
|
|
|
):
|
|
|
|
print(
|
|
|
|
json.dumps(
|
|
|
|
{
|
|
|
|
"type": "read_file",
|
|
|
|
"path": path,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2019-12-07 18:28:55 +01:00
|
|
|
data = json.loads(input())
|
2021-03-07 16:03:16 -03:00
|
|
|
assert data["type"] == "file_response"
|
|
|
|
return data["content"]
|
2019-04-22 21:56:30 +02:00
|
|
|
|
2019-10-24 21:53:42 +02:00
|
|
|
return read_file(path)
|
2022-01-25 09:46:42 +11:00
|
|
|
|
|
|
|
|
|
|
|
def merge_config(full_old, full_new):
|
|
|
|
def merge(old, new):
|
|
|
|
if isinstance(new, dict):
|
|
|
|
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
|
2022-11-24 11:09:19 +13:00
|
|
|
if isinstance(new, list):
|
2022-01-25 09:46:42 +11:00
|
|
|
if not isinstance(old, list):
|
|
|
|
return new
|
2023-02-06 20:08:40 -05:00
|
|
|
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
|
2022-11-24 11:09:19 +13:00
|
|
|
if new is None:
|
2022-01-25 19:24:59 +11:00
|
|
|
return old
|
2022-01-25 09:46:42 +11:00
|
|
|
|
|
|
|
return new
|
|
|
|
|
|
|
|
return merge(full_old, full_new)
|