best_match.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 possible_matches
  5. class FakeDevice:
  6. def __init__(self, dps):
  7. self._dps = dps
  8. def get_property(self, id):
  9. return self._dps.get(id)
  10. @property
  11. def name(self):
  12. return "cmdline"
  13. def main() -> int:
  14. dps = json.loads(" ".join(sys.argv[1:]))
  15. device = FakeDevice(dps)
  16. best = 0
  17. best_matches = set()
  18. for m in possible_matches(dps):
  19. if m.match_quality(dps) > best:
  20. best_matches.clear()
  21. best = m.match_quality(dps)
  22. if m.match_quality(dps) == best:
  23. best_matches.add(m)
  24. for m in best_matches:
  25. dps_seen = set(dps.keys())
  26. print(f"{m.config_type} matched {m.match_quality(dps)}%")
  27. print(f" {m.primary_entity.config_id}:")
  28. for dp in m.primary_entity.dps():
  29. dps_seen.discard(dp.id)
  30. print(f" {dp.name}: {dp.get_value(device)}")
  31. for entity in m.secondary_entities():
  32. print(f" {entity.config_id}:")
  33. for dp in entity.dps():
  34. dps_seen.discard(dp.id)
  35. print(f" {dp.name}: {dp.get_value(device)}")
  36. for dp in dps_seen:
  37. print(f" Missing {dp}: {dps[dp]}")
  38. if __name__ == "__main__":
  39. sys.exit(main())