light.py 1.7 KB

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