select.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. super().__init__()
  28. dps_map = self._init_begin(device, config)
  29. self._option_dps = dps_map.pop("option")
  30. if self._option_dps is None:
  31. raise AttributeError(f"{config.config_id} is missing an option dps")
  32. if not self._option_dps.values(device):
  33. raise AttributeError(
  34. f"{config.config_id} does not have a mapping to a list of options"
  35. )
  36. self._init_end(dps_map)
  37. @property
  38. def options(self):
  39. "Return the list of possible options."
  40. return self._option_dps.values(self._device)
  41. @property
  42. def current_option(self):
  43. "Return the currently selected option"
  44. return self._option_dps.get_value(self._device)
  45. async def async_select_option(self, option):
  46. "Set the option"
  47. await self._option_dps.async_set_value(self._device, option)