|
| 1 | +"""Generate enum files from the Overkiz API reference data.""" |
| 2 | + |
| 3 | +# ruff: noqa: T201 |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import asyncio |
| 8 | +import os |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +from pyoverkiz.auth.credentials import UsernamePasswordCredentials |
| 12 | +from pyoverkiz.client import OverkizClient |
| 13 | +from pyoverkiz.enums import Server |
| 14 | + |
| 15 | +# Hardcoded protocols that may not be available on all servers |
| 16 | +# Format: (name, prefix) |
| 17 | +ADDITIONAL_PROTOCOLS = [ |
| 18 | + ("HLRR_WIFI", "hlrrwifi"), |
| 19 | + ("MODBUSLINK", "modbuslink"), |
| 20 | + ("RTN", "rtn"), |
| 21 | +] |
| 22 | + |
| 23 | + |
| 24 | +async def generate_protocol_enum() -> None: |
| 25 | + """Generate the Protocol enum from the Overkiz API.""" |
| 26 | + username = os.environ["OVERKIZ_USERNAME"] |
| 27 | + password = os.environ["OVERKIZ_PASSWORD"] |
| 28 | + |
| 29 | + async with OverkizClient( |
| 30 | + server=Server.SOMFY_EUROPE, |
| 31 | + credentials=UsernamePasswordCredentials(username, password), |
| 32 | + ) as client: |
| 33 | + await client.login() |
| 34 | + |
| 35 | + protocol_types = await client.get_reference_protocol_types() |
| 36 | + |
| 37 | + # Build list of protocol entries (name, prefix, id, label) |
| 38 | + protocols: list[tuple[str, str, int | None, str | None]] = [ |
| 39 | + (p.name, p.prefix, p.id, p.label) for p in protocol_types |
| 40 | + ] |
| 41 | + |
| 42 | + # Add hardcoded protocols that may not be on all servers (avoid duplicates) |
| 43 | + fetched_prefixes = {p.prefix for p in protocol_types} |
| 44 | + for name, prefix in ADDITIONAL_PROTOCOLS: |
| 45 | + if prefix not in fetched_prefixes: |
| 46 | + protocols.append((name, prefix, None, None)) |
| 47 | + |
| 48 | + # Sort by name for consistent output |
| 49 | + protocols.sort(key=lambda p: p[0]) |
| 50 | + |
| 51 | + # Generate the enum file content |
| 52 | + lines = [ |
| 53 | + '"""Protocol enums describe device URL schemes used by Overkiz.', |
| 54 | + "", |
| 55 | + "THIS FILE IS AUTO-GENERATED. DO NOT EDIT MANUALLY.", |
| 56 | + "Run `uv run utils/generate_enums.py` to regenerate.", |
| 57 | + '"""', |
| 58 | + "", |
| 59 | + "from enum import StrEnum, unique", |
| 60 | + "", |
| 61 | + "from pyoverkiz.enums.base import UnknownEnumMixin", |
| 62 | + "", |
| 63 | + "", |
| 64 | + "@unique", |
| 65 | + "class Protocol(UnknownEnumMixin, StrEnum):", |
| 66 | + ' """Protocol used by Overkiz.', |
| 67 | + "", |
| 68 | + " Values have been retrieved from /reference/protocolTypes", |
| 69 | + ' """', |
| 70 | + "", |
| 71 | + ' UNKNOWN = "unknown"', |
| 72 | + "", |
| 73 | + ] |
| 74 | + |
| 75 | + # Add each protocol as an enum value with label comment |
| 76 | + for name, prefix, protocol_id, label in protocols: |
| 77 | + if protocol_id is not None: |
| 78 | + lines.append(f' {name} = "{prefix}" # {protocol_id}: {label}') |
| 79 | + else: |
| 80 | + lines.append(f' {name} = "{prefix}"') |
| 81 | + |
| 82 | + lines.append("") # End with newline |
| 83 | + |
| 84 | + # Write to the protocol.py file |
| 85 | + output_path = ( |
| 86 | + Path(__file__).parent.parent / "pyoverkiz" / "enums" / "protocol.py" |
| 87 | + ) |
| 88 | + output_path.write_text("\n".join(lines)) |
| 89 | + |
| 90 | + fetched_count = len(protocol_types) |
| 91 | + additional_count = len( |
| 92 | + [p for p in ADDITIONAL_PROTOCOLS if p[1] not in fetched_prefixes] |
| 93 | + ) |
| 94 | + |
| 95 | + print(f"✓ Generated {output_path}") |
| 96 | + print(f"✓ Added {fetched_count} protocols from API") |
| 97 | + print(f"✓ Added {additional_count} additional hardcoded protocols") |
| 98 | + print(f"✓ Total: {len(protocols)} protocols") |
| 99 | + |
| 100 | + |
| 101 | +asyncio.run(generate_protocol_enum()) |
0 commit comments