select.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """
  2. Setup for different kinds of Tuya selects
  3. """
  4. import logging
  5. from . import DOMAIN
  6. from .const import (
  7. CONF_DEVICE_ID,
  8. CONF_TYPE,
  9. )
  10. from .generic.select import TuyaLocalSelect
  11. from .helpers.device_config import get_config
  12. _LOGGER = logging.getLogger(__name__)
  13. async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
  14. """Set up the select entity according to it's type."""
  15. data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
  16. device = data["device"]
  17. selects = []
  18. cfg = get_config(discovery_info[CONF_TYPE])
  19. if cfg is None:
  20. raise ValueError(f"No device config found for {discovery_info}")
  21. ecfg = cfg.primary_entity
  22. if ecfg.entity == "select" and (
  23. discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
  24. ):
  25. data[ecfg.config_id] = TuyaLocalSelect(device, ecfg)
  26. selects.append(data[ecfg.config_id])
  27. if ecfg.deprecated:
  28. _LOGGER.warning(ecfg.deprecation_message)
  29. _LOGGER.debug(f"Adding select for {discovery_info[ecfg.config_id]}")
  30. for ecfg in cfg.secondary_entities():
  31. if ecfg.entity == "select" and (
  32. discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
  33. ):
  34. data[ecfg.config_id] = TuyaLocalSelect(device, ecfg)
  35. selects.append(data[ecfg.config_id])
  36. if ecfg.deprecated:
  37. _LOGGER.warning(ecfg.deprecation_message)
  38. _LOGGER.debug(f"Adding select for {discovery_info[ecfg.config_id]}")
  39. if not selects:
  40. raise ValueError(f"{device.name} does not support use as a select device.")
  41. async_add_entities(selects)
  42. async def async_setup_entry(hass, config_entry, async_add_entities):
  43. config = {**config_entry.data, **config_entry.options}
  44. await async_setup_platform(hass, {}, async_add_entities, config)