match_against.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """Find matching devices for the supplied dp list"""
  2. import json
  3. import sys
  4. from custom_components.tuya_local.helpers.device_config import (
  5. TuyaDeviceConfig,
  6. _typematch,
  7. )
  8. from common_funcs import FakeDevice, load_config
  9. def main() -> int:
  10. dps = json.loads(" ".join(sys.argv[2:]))
  11. device = FakeDevice(dps)
  12. config = load_config(sys.argv[1])
  13. if config is None:
  14. print(f"No config could be loaded for {sys.argv[1]}")
  15. return 1
  16. print(f"{config.primary_entity.config_id}:")
  17. for dp in config.primary_entity.dps():
  18. if dp.id not in dps.keys():
  19. print(f" {dp.name} missing from data")
  20. if not dp.optional:
  21. print(f">> dp {dp.id} is REQUIRED!!!!")
  22. elif not _typematch(dp.type, dps.get(dp.id)):
  23. print(
  24. f">> {dp.name} type MISMATCH, expected {dp.type.__name__}, got {dps.get(dp.id)}!!!"
  25. )
  26. else:
  27. values = dp.values(device)
  28. if values:
  29. values = f" from {values}"
  30. else:
  31. values = ""
  32. print(f" {dp.name}: {dp.get_value(device)}{values}")
  33. for entity in config.secondary_entities():
  34. print(f"{entity.config_id}:")
  35. for dp in entity.dps():
  36. if dp.id not in dps.keys():
  37. print(f" {dp.name} missing from data")
  38. if not dp.optional:
  39. print(f">> dp {dp.id} is REQUIRED!!!!")
  40. elif not _typematch(dp.type, dps.get(dp.id)):
  41. print(
  42. f">> {dp.name} type MISMATCH, expected {dp.type.__name__}, got {dps.get(dp.id)}!!!"
  43. )
  44. else:
  45. values = dp.values(device)
  46. if values:
  47. values = f" from {values}"
  48. else:
  49. values = ""
  50. print(f" {dp.name}: {dp.get_value(device)}{values}")
  51. if __name__ == "__main__":
  52. sys.exit(main())