Просмотр исходного кода

ci (translations): detect more translation issues

Detects the following issues:
  - using translation keys that don't exist
  - name shadowing class
  - name shadowing translation key
  - name that could be a translation key
Jason Rumney 1 день назад
Родитель
Сommit
daa23ffc2f
1 измененных файлов с 29 добавлено и 4 удалено
  1. 29 4
      util/untranslated_entities.py

+ 29 - 4
util/untranslated_entities.py

@@ -11,23 +11,48 @@ from custom_components.tuya_local.helpers.device_config import (
 )
 
 
+def error_location(entity):
+    return f"::error file=custom_components/tuya_local/devices/{entity._device.config},line={entity._config.__line__}:"
+
 def main() -> int:
     with open("custom_components/tuya_local/translations/en.json", "r") as f:
-        english = json.load(f)
+        english = json.load(f)["entity"]
     detected = 0
     for config in available_configs():
         device = TuyaDeviceConfig(config)
         for entity in device.all_entities():
-            if entity.name is None or entity.entity not in english["entity"]:
+            key = entity.translation_key
+            where = location(entity)
+            if key and (
+                    entity.entity not in english
+                    or key not in english[entity.entity]
+            ):
+                print(f"{where}: translation_key {key} does not exist")
+                detected +=1
+                continue
+
+            if entity.name is None:
                 continue
             slug = slugify(entity.name)
+            cls = entity.class
+
+            if cls is not None and key is None and cls == slug:
+                print(f"{where}: Entity name {entity.name} hides class translation.")
+                detected +=1
+                continue
+
+            if entity.entity not in english["entity"]:
+                continue
 
-            if entity.translation_key and slug != entity.translation_key:
+            if entity.translation_key:
+                if slug == entity.translation_key:
+                    print(f"{where}: Entity name {entity.name} hides translation.")
+                    detected +=1
                 continue
             translations = english["entity"][entity.entity]
             if slug in translations:
                 print(
-                    f"::error file=custom_components/tuya_local/devices/{config},line={entity._config.__line__}:: Entity can use translation_key: {slug}"
+                    f"{where}: Entity can use translation_key: {slug}"
                 )
                 detected += 1
     return detected