"""Generate self-contained remote packages from the maintained board profiles.""" import argparse from pathlib import Path import re ROOT = Path(__file__).resolve().parents[1] REPO = 'https://github.com/MaxGramser/homeassistant_espscreen' REFS = {'cyd': 'main', 'guition': 'main'} def generate(board, text=None): source = ROOT / ('home-like-2432s028.yaml' if board == 'cyd' else 'guition-4848s040.yaml') s = source.read_text() if text is None else text # The owner's wizard YAML supplies wifi/API/OTA credentials. No shared keys. s = re.sub(r'^ encryption:\n key: !secret api_encryption_key\n', '', s, flags=re.M) s = re.sub(r'^ota:\n.*?(?=^\w)', '', s, flags=re.M | re.S) s = re.sub(r'^wifi:\n.*?(?=^\w)', '', s, flags=re.M | re.S) # Fonts are fetched over HTTPS; the headers come with the external component. components = '[xpt2046, smart_display]' if board == 'cyd' else '[smart_display]' external = f'''external_components: - source: type: git url: {REPO}.git ref: {REFS[board]} path: components refresh: 0s components: {components} ''' s = re.sub(r'^external_components:\n.*?(?=^\w)', lambda m: external, s, flags=re.M | re.S) s = re.sub(r'file: ["\']fonts/([^"\']+)["\']', lambda m: 'file: "https://raw.githubusercontent.com/MaxGramser/homeassistant_espscreen/main/fonts/' + m[1] + '"', s) # A screen gets its values from ESP Screen Manager while it runs, entity changes included: # a fixed Home Assistant subscription in a board profile is a leftover. for section in ['sensor', 'text_sensor']: match = re.search(r'^' + section + r':\n.*?(?=^[a-zA-Z_]+:|\Z)', s, re.M | re.S) if match and re.search(r'^ - platform: homeassistant$', match[0], re.M): raise ValueError(f'{source.name}: a {section} subscribes to Home Assistant; runtime tiles bring their own values') if '!secret' in s: raise ValueError('Remote package still depends on local secrets') return '# GENERATED by tools/generate_packages.py; edit the source board profile.\n' + s def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--check', action='store_true') args = parser.parse_args() for board in REFS: source = ROOT / ('home-like-2432s028.yaml' if board == 'cyd' else 'guition-4848s040.yaml') if not source.exists(): continue path = ROOT / 'packages' / f'{board}.yaml' expected = generate(board) if args.check: if not path.exists() or path.read_text() != expected: raise SystemExit(f'Outdated: {path}') else: path.parent.mkdir(exist_ok=True) path.write_text(expected) print(f'{board}: package ' + ('verified' if args.check else 'generated')) if __name__ == '__main__': main()