match_against.py 1.9 KB

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