hon/scripts/sensor_docs.py

88 lines
2.9 KiB
Python
Raw Normal View History

2023-04-23 03:06:31 +05:30
#!/usr/bin/env python
2023-04-18 02:48:27 +05:30
import re
2023-04-23 03:06:31 +05:30
import sys
2023-04-18 02:48:27 +05:30
from pathlib import Path
2023-04-23 03:06:31 +05:30
if __name__ == "__main__":
sys.path.insert(0, str(Path(__file__).parent.parent))
2023-04-17 03:05:43 +05:30
from custom_components.hon.binary_sensor import BINARY_SENSORS
from custom_components.hon.button import BUTTONS
from custom_components.hon.number import NUMBERS
from custom_components.hon.select import SELECTS
from custom_components.hon.sensor import SENSORS
2023-04-18 02:48:27 +05:30
from custom_components.hon.switch import SWITCHES, HonSwitchEntityDescription
2023-04-17 03:05:43 +05:30
APPLIANCES = {
"AC": "Air conditioner",
"AP": "Air purifier",
"AS": "Air scanner",
"DW": "Dish washer",
"HO": "Hood",
"IH": "Hob",
"MW": "Microwave",
"OV": "Oven",
"REF": "Fridge",
"RVC": "Robot vacuum cleaner",
"TD": "Tumble dryer",
"WC": "Wine Cellar",
"WD": "Washer dryer",
"WH": "Water Heater",
"WM": "Washing machine",
}
ENTITY_CATEGORY_SORT = ["control", "config", "sensor"]
entities = {
"binary_sensor": BINARY_SENSORS,
"button": BUTTONS,
"number": NUMBERS,
"select": SELECTS,
"sensor": SENSORS,
"switch": SWITCHES,
}
result = {}
for entity_type, appliances in entities.items():
for appliance, data in appliances.items():
for entity in data:
2023-04-18 02:48:27 +05:30
if (
isinstance(entity, HonSwitchEntityDescription)
and entity.entity_category != "config"
):
key = f"{entity.turn_on_key}` / `{entity.turn_off_key}"
else:
key = entity.key
2023-04-23 05:31:14 +05:30
translation = bool(entity.translation_key)
attributes = (key, entity.name, entity.icon, entity_type, translation)
2023-04-17 03:05:43 +05:30
category = "control" if entity_type in ["switch", "button"] else "sensor"
result.setdefault(appliance, {}).setdefault(
entity.entity_category or category, []
).append(attributes)
2023-04-18 02:48:27 +05:30
text = ""
2023-04-17 03:05:43 +05:30
for appliance, categories in sorted(result.items()):
2023-04-18 02:48:27 +05:30
text += f"\n### {APPLIANCES[appliance]}\n"
2023-04-17 03:05:43 +05:30
categories = {k: categories[k] for k in ENTITY_CATEGORY_SORT if k in categories}
for category, data in categories.items():
2023-04-18 02:48:27 +05:30
text += f"#### {str(category).capitalize()}s\n"
2023-04-23 05:31:14 +05:30
text += "| Name | Icon | Entity | Key | Auto-Translation |\n"
text += "| --- | --- | --- | --- | --- |\n"
for key, name, icon, entity_type, translation in sorted(
data, key=lambda d: d[1]
):
icon = f"`{icon.replace('mdi:', '')}`" if icon else ""
translation = "" if translation else ""
text += f"| {name} | {icon} | `{entity_type}` | `{key}` | {translation} |\n"
2023-04-18 02:48:27 +05:30
with open(Path(__file__).parent.parent / "README.md", "r") as file:
readme = file.read()
readme = re.sub(
"(## Appliance Features\n)(?:.|\\s)+?([^#]## |\\Z)",
f"\\1{text}\\2",
readme,
re.DOTALL,
)
with open(Path(__file__).parent.parent / "README.md", "w") as file:
file.write(readme)