4
0

match_against.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. @property
  14. def name(self):
  15. return "cmdline"
  16. def main() -> int:
  17. dps = json.loads(" ".join(sys.argv[2:]))
  18. device = FakeDevice(dps)
  19. config = TuyaDeviceConfig(sys.argv[1])
  20. print(f"{config.primary_entity.config_id}:")
  21. for dp in config.primary_entity.dps():
  22. if dp.id not in dps.keys():
  23. print(f" {dp.name} missing from data")
  24. if not dp.optional:
  25. print(f">> dp {dp.id} is REQUIRED!!!!")
  26. elif not _typematch(dp.type, dps.get(dp.id)):
  27. print(
  28. f">> {dp.name} type MISMATCH, expected {dp.type.__name__}, got {dps.get(dp.id)}!!!"
  29. )
  30. else:
  31. values = dp.values(device)
  32. if values:
  33. values = f" from {values}"
  34. else:
  35. values = ""
  36. print(f" {dp.name}: {dp.get_value(device)}{values}")
  37. for entity in config.secondary_entities():
  38. print(f"{entity.config_id}:")
  39. for dp in entity.dps():
  40. if dp.id not in dps.keys():
  41. print(f" {dp.name} missing from data")
  42. if not dp.optional:
  43. print(f">> dp {dp.id} is REQUIRED!!!!")
  44. elif not _typematch(dp.type, dps.get(dp.id)):
  45. print(
  46. f">> {dp.name} type MISMATCH, expected {dp.type.__name__}, got {dps.get(dp.id)}!!!"
  47. )
  48. else:
  49. values = dp.values(device)
  50. if values:
  51. values = f" from {values}"
  52. else:
  53. values = ""
  54. print(f" {dp.name}: {dp.get_value(device)}{values}")
  55. if __name__ == "__main__":
  56. sys.exit(main())