match_against.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. elif not _typematch(dp.type, dps.get(dp.id)):
  22. print(
  23. f" {dp.name} type mismatch, expected {dp.type.__name__}, got {dps.get(dp.id)}"
  24. )
  25. else:
  26. print(f" {dp.name}: {dp.get_value(device)} from {dp.values(device)}")
  27. for entity in config.secondary_entities():
  28. print(f"{entity.config_id}:")
  29. for dp in entity.dps():
  30. if dp.id not in dps.keys():
  31. print(f" {dp.name} missing from data")
  32. elif not _typematch(dp.type, dps.get(dp.id)):
  33. print(
  34. f" {dp.name} type mismatch, expected {dp.type.__name__}, got {dps.get(dp.id)}"
  35. )
  36. else:
  37. print(f" {dp.name}: {dp.get_value(device)}")
  38. if __name__ == "__main__":
  39. sys.exit(main())