match_against.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. values = dp.values(device)
  29. if values:
  30. values = f" from {values}"
  31. else:
  32. values = ""
  33. print(f" {dp.name}: {dp.get_value(device)}{values}")
  34. for entity in config.secondary_entities():
  35. print(f"{entity.config_id}:")
  36. for dp in entity.dps():
  37. if dp.id not in dps.keys():
  38. print(f" {dp.name} missing from data")
  39. if not dp.optional:
  40. print(f">> dp {dp.id} is REQUIRED!!!!")
  41. elif not _typematch(dp.type, dps.get(dp.id)):
  42. print(
  43. f">> {dp.name} type MISMATCH, expected {dp.type.__name__}, got {dps.get(dp.id)}!!!"
  44. )
  45. else:
  46. values = dp.values(device)
  47. if values:
  48. values = f" from {values}"
  49. else:
  50. values = ""
  51. print(f" {dp.name}: {dp.get_value(device)}{values}")
  52. if __name__ == "__main__":
  53. sys.exit(main())