match_against.py 1.6 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 (
  5. TuyaDeviceConfig,
  6. _typematch,
  7. )
  8. class FakeDevice:
  9. def __init__(self, dps):
  10. self._dps = dps
  11. def get_property(self, id):
  12. return self._dps.get(id)
  13. def main() -> int:
  14. dps = json.loads(" ".join(sys.argv[2:]))
  15. device = FakeDevice(dps)
  16. config = TuyaDeviceConfig(sys.argv[1])
  17. print(f"{config.primary_entity.config_id}:")
  18. for dp in config.primary_entity.dps():
  19. if dp.id not in dps.keys():
  20. print(f" {dp.name} missing from data")
  21. if not dp.optional:
  22. print(f" dp {dp.id} is REQUIRED!!!!")
  23. elif not _typematch(dp.type, dps.get(dp.id)):
  24. print(
  25. f" {dp.name} type MISMATCH, expected {dp.type.__name__}, got {dps.get(dp.id)}!!!"
  26. )
  27. else:
  28. print(f" {dp.name}: {dp.get_value(device)} from {dp.values(device)}")
  29. for entity in config.secondary_entities():
  30. print(f"{entity.config_id}:")
  31. for dp in entity.dps():
  32. if dp.id not in dps.keys():
  33. print(f" {dp.name} missing from data")
  34. if not dp.optional:
  35. print(f" dp {dp.id} is REQUIRED!!!!")
  36. elif not _typematch(dp.type, dps.get(dp.id)):
  37. print(
  38. f" {dp.name} type MISMATCH, expected {dp.type.__name__}, got {dps.get(dp.id)}!!!"
  39. )
  40. else:
  41. print(f" {dp.name}: {dp.get_value(device)}")
  42. if __name__ == "__main__":
  43. sys.exit(main())