match_against.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 _typematch
  5. from .common_funcs import FakeDevice, load_config, make_sample_dps
  6. def main() -> int:
  7. rest = " ".join(sys.argv[2:])
  8. config = load_config(rest)
  9. if config is None:
  10. dps = json.loads(rest)
  11. else:
  12. dps = make_sample_dps(config)
  13. config = load_config(sys.argv[1])
  14. if config is None:
  15. print(f"No config could be loaded for {sys.argv[1]}")
  16. return 1
  17. device = FakeDevice(dps)
  18. unseen = set(dps.keys())
  19. for entity in config.all_entities():
  20. print(f"{entity.config_id}:")
  21. for dp in entity.dps():
  22. unseen.discard(dp.id)
  23. if dp.id not in dps.keys():
  24. print(f" {dp.name} missing from data")
  25. if not dp.optional:
  26. print(f">> dp {dp.id} is REQUIRED!!!!")
  27. elif not _typematch(dp.type, dps.get(dp.id)):
  28. print(
  29. f">> {dp.name} type MISMATCH, expected {dp.type.__name__}, got {dps.get(dp.id)}!!!"
  30. )
  31. else:
  32. values = dp.values(device)
  33. if values:
  34. values = f" from {values}"
  35. else:
  36. values = ""
  37. print(f" {dp.name}: {dp.get_value(device)}{values}")
  38. for dp in unseen:
  39. print(f" Extra dp {dp} in data")
  40. if __name__ == "__main__":
  41. sys.exit(main())