select.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. Setup for different kinds of Tuya selects
  3. """
  4. from homeassistant.components.select import SelectEntity
  5. from .device import TuyaLocalDevice
  6. from .helpers.config import async_tuya_setup_platform
  7. from .helpers.device_config import TuyaEntityConfig
  8. from .helpers.mixin import TuyaLocalEntity
  9. async def async_setup_entry(hass, config_entry, async_add_entities):
  10. config = {**config_entry.data, **config_entry.options}
  11. await async_tuya_setup_platform(
  12. hass,
  13. async_add_entities,
  14. config,
  15. "select",
  16. TuyaLocalSelect,
  17. )
  18. class TuyaLocalSelect(TuyaLocalEntity, SelectEntity):
  19. """Representation of a Tuya Select"""
  20. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  21. """
  22. Initialise the select.
  23. Args:
  24. device (TuyaLocalDevice): the device API instance
  25. config (TuyaEntityConfig): the configuration for this entity
  26. """
  27. dps_map = self._init_begin(device, config)
  28. self._option_dps = dps_map.pop("option")
  29. if self._option_dps is None:
  30. raise AttributeError(f"{config.name} is missing an option dps")
  31. if not self._option_dps.values(device):
  32. raise AttributeError(
  33. f"{config.name} does not have a mapping to a list of options"
  34. )
  35. self._init_end(dps_map)
  36. @property
  37. def options(self):
  38. "Return the list of possible options."
  39. return self._option_dps.values(self._device)
  40. @property
  41. def current_option(self):
  42. "Return the currently selected option"
  43. return self._option_dps.get_value(self._device)
  44. async def async_select_option(self, option):
  45. "Set the option"
  46. await self._option_dps.async_set_value(self._device, option)