best_match.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """Find matching devices for the supplied dp list"""
  2. import json
  3. import sys
  4. from common_funcs import FakeDevice
  5. from custom_components.tuya_local.helpers.device_config import possible_matches
  6. def main() -> int:
  7. dps = json.loads(" ".join(sys.argv[1:]))
  8. device = FakeDevice(dps)
  9. best = 0
  10. best_matches = set()
  11. for m in possible_matches(dps):
  12. if m.match_quality(dps) > best:
  13. best_matches.clear()
  14. best = m.match_quality(dps)
  15. if m.match_quality(dps) == best:
  16. best_matches.add(m)
  17. for m in best_matches:
  18. dps_seen = set(dps.keys())
  19. print(f"{m.config_type} matched {m.match_quality(dps)}%")
  20. for entity in m.all_entities():
  21. print(f" {entity.config_id}:")
  22. for dp in entity.dps():
  23. dps_seen.discard(dp.id)
  24. print(f" {dp.name}: {dp.get_value(device)}")
  25. if dp.values(device):
  26. print(f" values: {dp.values(device)}")
  27. for dp in dps_seen:
  28. print(f" Missing {dp}: {dps[dp]}")
  29. if __name__ == "__main__":
  30. sys.exit(main())