hon/scripts/sensor_docs.py

99 lines
3.1 KiB
Python
Raw Normal View History

2023-04-22 21:36:31 +00:00
#!/usr/bin/env python
2023-04-17 21:18:27 +00:00
import re
2023-04-22 21:36:31 +00:00
import sys
2023-04-17 21:18:27 +00:00
from pathlib import Path
2023-05-10 16:13:05 +00:00
2023-04-22 21:36:31 +00:00
if __name__ == "__main__":
sys.path.insert(0, str(Path(__file__).parent.parent))
2023-04-16 21:35:43 +00:00
from custom_components.hon.binary_sensor import BINARY_SENSORS
from custom_components.hon.button import BUTTONS
2023-06-30 17:40:30 +00:00
from custom_components.hon.light import LIGHTS
2023-05-10 16:13:05 +00:00
from custom_components.hon.climate import CLIMATES
2023-04-16 21:35:43 +00:00
from custom_components.hon.number import NUMBERS
from custom_components.hon.select import SELECTS
from custom_components.hon.sensor import SENSORS
2023-06-09 03:56:52 +00:00
from custom_components.hon.fan import FANS
2023-05-28 05:50:59 +00:00
from custom_components.hon.switch import (
SWITCHES,
HonControlSwitchEntityDescription,
HonSwitchEntityDescription,
)
2023-04-16 21:35:43 +00:00
APPLIANCES = {
2023-06-20 22:59:00 +00:00
"AC": "Air Conditioner",
"AP": "Air Purifier",
"AS": "Air Scanner",
"DW": "Dish Washer",
2023-04-16 21:35:43 +00:00
"HO": "Hood",
2023-06-20 22:59:00 +00:00
"IH": "Induction Hob",
2023-04-16 21:35:43 +00:00
"MW": "Microwave",
"OV": "Oven",
"REF": "Fridge",
2023-06-20 22:59:00 +00:00
"RVC": "Robot Vacuum Cleaner",
"TD": "Tumble Dryer",
2023-04-16 21:35:43 +00:00
"WC": "Wine Cellar",
2023-06-20 22:59:00 +00:00
"WD": "Washer Dryer",
2023-04-16 21:35:43 +00:00
"WH": "Water Heater",
2023-06-20 22:59:00 +00:00
"WM": "Washing Machine",
2023-04-16 21:35:43 +00:00
}
ENTITY_CATEGORY_SORT = ["control", "config", "sensor"]
entities = {
"binary_sensor": BINARY_SENSORS,
"button": BUTTONS,
"number": NUMBERS,
"select": SELECTS,
"sensor": SENSORS,
"switch": SWITCHES,
2023-05-10 16:13:05 +00:00
"climate": CLIMATES,
2023-06-09 03:56:52 +00:00
"fan": FANS,
2023-06-30 17:40:30 +00:00
"light": LIGHTS,
2023-04-16 21:35:43 +00:00
}
result = {}
for entity_type, appliances in entities.items():
for appliance, data in appliances.items():
for entity in data:
2023-05-28 05:50:59 +00:00
if isinstance(entity, HonControlSwitchEntityDescription):
2023-04-17 21:18:27 +00:00
key = f"{entity.turn_on_key}` / `{entity.turn_off_key}"
else:
key = entity.key
2023-04-24 19:38:05 +00:00
attributes = (key, entity.name, entity.icon, entity_type)
2023-05-10 16:13:05 +00:00
category = (
"control"
2023-05-16 22:01:33 +00:00
if entity.key.startswith("settings")
2023-05-28 05:50:59 +00:00
or isinstance(entity, HonSwitchEntityDescription)
or isinstance(entity, HonControlSwitchEntityDescription)
2023-05-16 22:01:33 +00:00
or entity_type in ["button", "climate"]
2023-05-10 16:13:05 +00:00
else "sensor"
)
2023-04-16 21:35:43 +00:00
result.setdefault(appliance, {}).setdefault(
entity.entity_category or category, []
).append(attributes)
2023-04-17 21:18:27 +00:00
text = ""
2023-04-16 21:35:43 +00:00
for appliance, categories in sorted(result.items()):
2023-04-17 21:18:27 +00:00
text += f"\n### {APPLIANCES[appliance]}\n"
2023-04-16 21:35:43 +00:00
categories = {k: categories[k] for k in ENTITY_CATEGORY_SORT if k in categories}
for category, data in categories.items():
2023-04-17 21:18:27 +00:00
text += f"#### {str(category).capitalize()}s\n"
2023-04-24 19:38:05 +00:00
text += "| Name | Icon | Entity | Key |\n"
text += "| --- | --- | --- | --- |\n"
for key, name, icon, entity_type in sorted(data, key=lambda d: d[1]):
2023-04-23 00:01:14 +00:00
icon = f"`{icon.replace('mdi:', '')}`" if icon else ""
2023-04-24 19:38:05 +00:00
text += f"| {name} | {icon} | `{entity_type}` | `{key}` |\n"
2023-04-17 21:18:27 +00:00
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)