Skip to content

Commit a2d64cb

Browse files
committed
Add enum values to HA strings json
1 parent 355fb2a commit a2d64cb

2 files changed

Lines changed: 1060 additions & 134 deletions

File tree

.github/scripts/merge_topics.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,30 @@ def main():
88
print(f"topics_path={topics_path}")
99
print(f"en_path={en_path}")
1010
with open(topics_path, encoding='utf-8') as f:
11-
topics = json.load(f)
11+
topics_data = json.load(f)
1212

1313
with open(en_path, encoding='utf-8') as f:
1414
en = json.load(f)
1515

16+
# Build enum lookup: enum_name -> {lowercase_id: human_readable_name}
17+
enum_lookup = {}
18+
for enum_def in topics_data.get('enums', []):
19+
enum_name = enum_def.get('name')
20+
enum_values = {}
21+
for ev in enum_def.get('EnumValues', []):
22+
# Use lowercase id as key, name as value
23+
enum_values[ev.get('id', '').lower()] = ev.get('name', '')
24+
enum_lookup[enum_name] = enum_values
25+
1626
# Update topics: add or update entries in en.json under entity.sensor for each topic id
1727
entity = {}
1828
count = 0
19-
for topic in topics.get('topics', []):
29+
for topic in topics_data.get('topics', []):
2030
translation_key = topic.get('short_id').replace('{', '').replace('}', '') # same as in common.py
2131
topic_name = topic.get('generic_name')
2232
message_type = topic.get('message_type')
2333
is_adjustable_suffix = topic.get('is_adjustable_suffix')
34+
enum_name = topic.get('enum')
2435

2536
# Extract the part after the dot and make it lower case
2637
if '.' in message_type:
@@ -32,30 +43,42 @@ def main():
3243
if entity_type == "service":
3344
continue
3445

46+
# Build entity entry with name and optional state for enums
47+
entity_entry = {"name": topic_name}
48+
if enum_name and enum_name in enum_lookup:
49+
entity_entry["state"] = enum_lookup[enum_name]
50+
3551
# Add to original entity type
3652
if entity_type not in entity:
3753
entity[entity_type] = {}
38-
entity[entity_type][translation_key] = {"name": topic_name}
54+
entity[entity_type][translation_key] = entity_entry
3955
count += 1
4056

4157
# If is_adjustable_suffix is set, also add to sensor entity type
4258
if is_adjustable_suffix is not None and entity_type != 'sensor':
4359
if 'sensor' not in entity:
4460
entity['sensor'] = {}
45-
entity['sensor'][translation_key] = {"name": topic_name}
61+
entity['sensor'][translation_key] = entity_entry
4662
# to support READ_ONLY we need everything in sensor and in binary_sensor
4763
if entity_type == 'switch':
4864
if 'binary_sensor' not in entity:
4965
entity['binary_sensor'] = {}
50-
entity['binary_sensor'][translation_key] = {"name": topic_name}
66+
entity['binary_sensor'][translation_key] = entity_entry
5167
if entity_type in ['number', 'select']:
5268
if 'sensor' not in entity:
5369
entity['sensor'] = {}
54-
entity['sensor'][translation_key] = {"name": topic_name}
70+
entity['sensor'][translation_key] = entity_entry
5571
# Sort the entity dictionary and its nested dictionaries
5672
sorted_entity = {}
5773
for entity_type in sorted(entity.keys()):
58-
sorted_entity[entity_type] = dict(sorted(entity[entity_type].items()))
74+
sorted_entity[entity_type] = {}
75+
for translation_key in sorted(entity[entity_type].keys()):
76+
entry = entity[entity_type][translation_key]
77+
sorted_entry = {"name": entry["name"]}
78+
if "state" in entry:
79+
# Sort the state dictionary alphabetically by key
80+
sorted_entry["state"] = dict(sorted(entry["state"].items()))
81+
sorted_entity[entity_type][translation_key] = sorted_entry
5982

6083
en['entity'] = sorted_entity
6184

0 commit comments

Comments
 (0)