|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import yaml |
| 5 | +from pathlib import Path |
| 6 | +from enum import Enum |
| 7 | +from shutil import copyfile |
| 8 | +import sys |
| 9 | +import os |
| 10 | + |
| 11 | + |
| 12 | +class Channel(Enum): |
| 13 | + stable = "stable" |
| 14 | + beta = "beta" |
| 15 | + edge = "edge" |
| 16 | + |
| 17 | + |
| 18 | +def main(args): |
| 19 | + parser = argparse.ArgumentParser( |
| 20 | + description="Generate ESPHome Home Assistant config.json" |
| 21 | + ) |
| 22 | + parser.add_argument("channels", nargs="+", type=Channel, choices=list(Channel)) |
| 23 | + args = parser.parse_args(args) |
| 24 | + |
| 25 | + root = Path(__file__).parent.parent |
| 26 | + templ = root / "template" |
| 27 | + |
| 28 | + with open(templ / "addon_config.yaml", "r") as f: |
| 29 | + config = yaml.safe_load(f) |
| 30 | + |
| 31 | + copyf = config["copy_files"] |
| 32 | + |
| 33 | + for channel in args.channels: |
| 34 | + conf = config[f"typedaemon-{channel.value}"] |
| 35 | + base_image = conf.pop("base_image", None) |
| 36 | + dir_ = root / conf.pop("directory") |
| 37 | + path = dir_ / "config.yaml" |
| 38 | + |
| 39 | + os.makedirs(dir_, exist_ok=True) |
| 40 | + |
| 41 | + with open(path, "w") as f: |
| 42 | + yaml.dump(conf, f, indent=2, sort_keys=False, explicit_start=True) |
| 43 | + |
| 44 | + for file_ in copyf: |
| 45 | + os.makedirs(dir_ / Path(file_).parent, exist_ok=True) |
| 46 | + if Path.exists(templ / channel.value / file_): |
| 47 | + copyfile(templ / channel.value / file_, dir_ / file_) |
| 48 | + else: |
| 49 | + copyfile(templ / file_, dir_ / file_) |
| 50 | + |
| 51 | + path = dir_ / "FILES ARE GENERATED DO NOT EDIT" |
| 52 | + with open(path, "w") as f: |
| 53 | + f.write("Any edits should be made to the files in the 'template' directory") |
| 54 | + |
| 55 | + if channel == Channel.edge: |
| 56 | + path = dir_ / "build.yaml" |
| 57 | + build_conf = { |
| 58 | + "build_from": { |
| 59 | + arch: base_image for arch in conf["arch"] |
| 60 | + } |
| 61 | + } |
| 62 | + with open(path, "w") as f: |
| 63 | + yaml.dump(build_conf, f, indent=2, sort_keys=True, explicit_start=True) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + main(sys.argv[1:]) |
0 commit comments