forked from esphome/esphome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync-device_class.py
executable file
·67 lines (52 loc) · 1.8 KB
/
sync-device_class.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
import re
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.components.button import ButtonDeviceClass
from homeassistant.components.cover import CoverDeviceClass
from homeassistant.components.number import NumberDeviceClass
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.components.switch import SwitchDeviceClass
BLOCKLIST = (
# requires special support on HA side
"enum",
)
DOMAINS = {
"binary_sensor": BinarySensorDeviceClass,
"button": ButtonDeviceClass,
"cover": CoverDeviceClass,
"number": NumberDeviceClass,
"sensor": SensorDeviceClass,
"switch": SwitchDeviceClass,
}
def sub(path, pattern, repl):
with open(path) as handle:
content = handle.read()
content = re.sub(pattern, repl, content, flags=re.MULTILINE)
with open(path, "w") as handle:
handle.write(content)
def main():
classes = {"EMPTY": ""}
allowed = {}
for domain, enum in DOMAINS.items():
available = {
cls.value.upper(): cls.value for cls in enum if cls.value not in BLOCKLIST
}
classes.update(available)
allowed[domain] = list(available.keys()) + ["EMPTY"]
# replace constant defines in const.py
out = ""
for cls in sorted(classes):
out += f'DEVICE_CLASS_{cls.upper()} = "{classes[cls]}"\n'
sub("esphome/const.py", '(DEVICE_CLASS_\\w+ = "\\w*"\r?\n)+', out)
for domain in sorted(allowed):
# replace imports
out = ""
for item in sorted(allowed[domain]):
out += f" DEVICE_CLASS_{item.upper()},\n"
sub(
f"esphome/components/{domain}/__init__.py",
"( DEVICE_CLASS_\\w+,\r?\n)+",
out,
)
if __name__ == "__main__":
main()