2019-04-24 17:09:50 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import yaml
|
|
|
|
from pathlib import Path
|
|
|
|
from enum import Enum
|
|
|
|
from shutil import copyfile
|
2020-07-25 19:28:44 +02:00
|
|
|
import sys
|
2019-04-24 17:09:50 +02:00
|
|
|
|
2021-09-15 20:45:09 +02:00
|
|
|
|
2019-04-24 17:09:50 +02:00
|
|
|
class Channel(Enum):
|
2021-09-15 20:45:09 +02:00
|
|
|
stable = "stable"
|
|
|
|
beta = "beta"
|
|
|
|
dev = "dev"
|
2019-04-24 17:09:50 +02:00
|
|
|
|
|
|
|
|
2020-07-25 19:28:44 +02:00
|
|
|
def main(args):
|
2021-09-15 20:45:09 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Generate ESPHome Home Assistant config.json"
|
|
|
|
)
|
|
|
|
parser.add_argument("channels", nargs="+", type=Channel, choices=list(Channel))
|
2020-07-25 19:28:44 +02:00
|
|
|
args = parser.parse_args(args)
|
2019-04-24 17:09:50 +02:00
|
|
|
|
2020-07-25 19:28:44 +02:00
|
|
|
root = Path(__file__).parent.parent
|
2021-09-15 20:45:09 +02:00
|
|
|
templ = root / "template"
|
2019-04-24 17:09:50 +02:00
|
|
|
|
2021-09-15 20:45:09 +02:00
|
|
|
with open(templ / "addon_config.yaml", "r") as f:
|
2020-07-25 19:28:44 +02:00
|
|
|
config = yaml.safe_load(f)
|
2019-04-24 17:09:50 +02:00
|
|
|
|
2021-09-15 20:45:09 +02:00
|
|
|
copyf = config["copy_files"]
|
2019-04-24 17:09:50 +02:00
|
|
|
|
2020-07-25 19:28:44 +02:00
|
|
|
for channel in args.channels:
|
2021-09-15 20:45:09 +02:00
|
|
|
conf = config[f"esphome-{channel.value}"]
|
2021-09-20 10:19:35 +02:00
|
|
|
base_image = conf.pop("base_image", None)
|
2021-09-15 20:45:09 +02:00
|
|
|
dir_ = root / conf.pop("directory")
|
2022-03-24 05:23:36 +01:00
|
|
|
path = dir_ / "config.yaml"
|
2021-09-15 20:45:09 +02:00
|
|
|
with open(path, "w") as f:
|
2023-01-18 20:58:57 +01:00
|
|
|
yaml.dump(conf, f, indent=2, sort_keys=False, explicit_start=True)
|
2020-07-25 19:28:44 +02:00
|
|
|
|
|
|
|
for file_, conf_ in copyf.items():
|
2021-09-19 12:36:26 +02:00
|
|
|
if Path.exists(templ / channel.value / file_):
|
|
|
|
copyfile(templ / channel.value / file_, dir_ / file_)
|
|
|
|
else:
|
|
|
|
copyfile(templ / file_, dir_ / file_)
|
2020-07-25 19:28:44 +02:00
|
|
|
|
2021-09-15 20:45:09 +02:00
|
|
|
path = dir_ / "FILES ARE GENERATED DO NOT EDIT"
|
|
|
|
with open(path, "w") as f:
|
2021-02-14 20:45:10 +01:00
|
|
|
f.write("Any edits should be made to the files in the 'template' directory")
|
|
|
|
|
2020-07-25 19:28:44 +02:00
|
|
|
if channel == Channel.dev:
|
2022-03-24 05:23:36 +01:00
|
|
|
path = dir_ / "build.yaml"
|
2020-07-25 19:28:44 +02:00
|
|
|
build_conf = {
|
2021-09-15 20:45:09 +02:00
|
|
|
"build_from": {
|
2023-01-20 01:58:26 +01:00
|
|
|
arch: base_image for arch in conf["arch"]
|
2021-09-15 20:45:09 +02:00
|
|
|
}
|
2020-07-25 19:28:44 +02:00
|
|
|
}
|
2021-09-15 20:45:09 +02:00
|
|
|
with open(path, "w") as f:
|
2022-03-24 05:23:36 +01:00
|
|
|
yaml.dump(build_conf, f, indent=2, sort_keys=True, explicit_start=True)
|
2020-07-25 19:28:44 +02:00
|
|
|
|
2019-04-24 17:09:50 +02:00
|
|
|
|
2021-09-15 20:45:09 +02:00
|
|
|
if __name__ == "__main__":
|
2020-07-25 19:28:44 +02:00
|
|
|
main(sys.argv[1:])
|