untranslated_entities.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 main() -> int:
  10. with open("custom_components/tuya_local/translations/en.json", "r") as f:
  11. english = json.load(f)
  12. detected = 0
  13. for config in available_configs():
  14. device = TuyaDeviceConfig(config)
  15. for entity in device.all_entities():
  16. if (
  17. entity.translation_key
  18. or entity.name is None
  19. or entity.entity not in english["entity"]
  20. ):
  21. continue
  22. translations = english["entity"][entity.entity]
  23. slug = slugify(entity.name)
  24. if slug in translations:
  25. print(
  26. f"::error file=custom_components/tuya_local/devices/{config},line={entity._config.__line__}:: Entity can use translation_key: {slug}"
  27. )
  28. detected += 1
  29. return detected
  30. if __name__ == "__main__":
  31. sys.exit(main())