Skip to content

Commit

Permalink
Rewrite print_models script
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Dec 25, 2024
1 parent 43ecc37 commit cd9a2d1
Showing 1 changed file with 48 additions and 71 deletions.
119 changes: 48 additions & 71 deletions print_models.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,73 @@
import re
from custom_components.xiaomi_gateway3.core.devices import DEVICES

from homeassistant.components.sensor import DOMAIN

from custom_components.xiaomi_gateway3.core.converters.devices import DEVICES
from custom_components.xiaomi_gateway3.core.converters.mibeacon import MiBeacon

assert DOMAIN # fix circular import

columns = ["Brand", "Name", "Model", "Entities", "S"]
columns = ["Brand", "Name", "Model", "Entities"]
header = ["---"] * len(columns)

devices = {
"Gateways": [
[
"Xiaomi",
"Multimode Gateway CN",
"[ZNDMWG03LM](https://home.miot-spec.com/s/lumi.gateway.mgl03)",
"alarm, command, data, gateway",
"4",
],
[
"Xiaomi",
"Multimode Gateway EU",
"[ZNDMWG02LM](https://home.miot-spec.com/s/lumi.gateway.mgl03)",
"alarm, command, data, gateway",
"4",
],
]
}
devices = {}
models = set()

for device in DEVICES:
is_ble = True

for i, device in enumerate(DEVICES):
# skip devices with bad support
if device.get("support", 3) < 3:
if device.pop("support", 3) < 3:
continue

for k, v in device.items():
if not isinstance(v, list) or k in ("spec", "lumi.gateway.aqcn03"):
continue
spec = device.pop("spec")

brand, name, model = v if len(v) == 3 else v + [k]
# skip BLE with unknown spec
if "default" not in device:
spec = ", ".join([conv.attr for conv in spec if conv.domain])
else:
spec = "*"

if isinstance(k, str):
if "gateway" in k:
kind = "Gateways"
elif k.startswith("lumi.") or k.startswith("ikea."):
kind = "Xiaomi Zigbee"
else:
kind = "Other Zigbee"
elif MiBeacon in device["spec"]:
kind = "Xiaomi BLE"
else:
kind = "Xiaomi Mesh"
for model, info in device.items():
if not isinstance(info, list):
continue

if kind != "Other Zigbee":
link = f"https://home.miot-spec.com/s/{k}"
if model in models:
print("duplicate:", model)
else:
link = f"https://www.zigbee2mqtt.io/supported-devices/#s={model}"
models.add(model)

items = devices.setdefault(kind, [])
if isinstance(model, str) and model not in info:
info.append(model)

# skip if model already exists
if any(True for i in items if f"[{model}]" in i[2]):
continue
market_brand = info[0] or "~"
market_name = info[1]
market_model = ", ".join(info[2:]) if len(info) > 2 else ""

# skip BLE with unknown spec
if "default" not in device:
spec = ", ".join(
[
conv.attr + "*" if conv.enabled is None else conv.attr
for conv in device["spec"]
if conv.domain
]
)
if isinstance(model, str):
if "gateway" in model:
kind = "Gateways"
elif model.startswith(("lumi.", "ikea.")):
kind = "Xiaomi Zigbee"
elif model.startswith("matter."):
kind = "Matter"
else:
kind = "Other Zigbee"
elif isinstance(model, int):
if is_ble:
kind = "Xiaomi BLE"
else:
kind = "Xiaomi Mesh"
else:
spec = "*"

support = str(device.get("support", ""))
kind = "Unknown"

model = f"[{model}]({link})"
devices.setdefault(kind, []).append(
[market_brand, market_name, market_model, spec]
)

items.append([brand, name, model, spec, support])
if device.get("default") == "ble":
is_ble = False

out = "<!--supported-->\n"
out = f"# Supported devices\n\nTotal devices: {len(models)}\n\n"
for k, v in devices.items():
out += f"### Supported {k}\n\nTotal devices: {len(v)}\n\n"
out += f"## Supported {k}\n\nTotal devices: {len(v)}\n\n"
out += "|" + "|".join(columns) + "|\n"
out += "|" + "|".join(header) + "|\n"
for line in sorted(v):
out += "|" + "|".join(line) + "|\n"
out += "\n"
out += "<!--supported-->"

raw = open("README.md", "r", encoding="utf-8").read()
raw = re.sub(r"<!--supported-->(.+?)<!--supported-->", out, raw, flags=re.DOTALL)
open("README.md", "w", encoding="utf-8").write(raw)
open("DEVICES.md", "w", encoding="utf-8").write(out)

0 comments on commit cd9a2d1

Please sign in to comment.