select.py 2.0 KB

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