untranslated_entities.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """Find entities with names that match existing translation keys."""
  2. import json
  3. import sys
  4. from homeassistant.util import slugify
  5. from custom_components.tuya_local.helpers.device_config import (
  6. TuyaDeviceConfig,
  7. available_configs,
  8. )
  9. def error_location(entity):
  10. return f"::error file=custom_components/tuya_local/devices/{entity._device.config},line={entity._config.__line__}:"
  11. def main() -> int:
  12. with open("custom_components/tuya_local/translations/en.json", "r") as f:
  13. english = json.load(f)["entity"]
  14. detected = 0
  15. for config in available_configs():
  16. device = TuyaDeviceConfig(config)
  17. for entity in device.all_entities():
  18. key = entity.translation_key
  19. where = error_location(entity)
  20. if key and (
  21. entity.entity not in english or key not in english[entity.entity]
  22. ):
  23. print(f"{where}: translation_key {entity.entity}.{key} does not exist")
  24. detected += 1
  25. continue
  26. if entity.name is None:
  27. continue
  28. slug = slugify(entity.name)
  29. cls = entity.device_class
  30. if cls is not None and key is None and cls == slug:
  31. print(f"{where}: Entity name {entity.name} hides class translation.")
  32. detected += 1
  33. continue
  34. if entity.entity not in english:
  35. continue
  36. if entity.translation_key:
  37. if slug == entity.translation_key:
  38. print(f"{where}: Entity name {entity.name} hides translation.")
  39. detected += 1
  40. continue
  41. translations = english[entity.entity]
  42. if slug in translations:
  43. print(f"{where}: Entity can use translation_key: {slug}")
  44. detected += 1
  45. return detected
  46. if __name__ == "__main__":
  47. sys.exit(main())