untranslated_entities.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 entity.name is None or entity.entity not in english["entity"]:
  17. continue
  18. slug = slugify(entity.name)
  19. if entity.translation_key and slug != entity.translation_key:
  20. continue
  21. translations = english["entity"][entity.entity]
  22. if slug in translations:
  23. print(
  24. f"::error file=custom_components/tuya_local/devices/{config},line={entity._config.__line__}:: Entity can use translation_key: {slug}"
  25. )
  26. detected += 1
  27. return detected
  28. if __name__ == "__main__":
  29. sys.exit(main())