common_funcs.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Common functions used in the utilities."""
  2. from custom_components.tuya_local.helpers.device_config import (
  3. get_config,
  4. )
  5. class FakeDevice:
  6. def __init__(self, dps):
  7. self._dps = dps
  8. def get_property(self, dpid):
  9. return self._dps.get(dpid)
  10. @property
  11. def name(self):
  12. return "cmdline"
  13. def load_config(filename):
  14. """Load the config for the device."""
  15. if filename.endswith(".yaml"):
  16. filename = filename[:-5]
  17. if "/" in filename:
  18. filename = filename.split("/")[-1]
  19. return get_config(filename)
  20. def representation(dp):
  21. """Return a represenative value for the dp."""
  22. if dp.type is bool:
  23. return True
  24. if dp.type is int:
  25. range_spec = dp._config.get("range")
  26. if isinstance(range_spec, dict) and "min" in range_spec:
  27. return range_spec["min"]
  28. return 0
  29. if dp.type is str:
  30. return ""
  31. if dp.type is float:
  32. return 0.0
  33. def make_sample_dps(config):
  34. """Return a dictionary of sample DPS values."""
  35. all_dps = config._get_all_dps()
  36. return {dp.id: representation(dp) for dp in all_dps}