humidifier.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """
  2. Setup for different kinds of Tuya humidifier devices
  3. """
  4. import logging
  5. from . import DOMAIN
  6. from .const import (
  7. CONF_HUMIDIFIER,
  8. CONF_DEVICE_ID,
  9. CONF_TYPE,
  10. CONF_TYPE_AUTO,
  11. )
  12. from .generic.humidifier import TuyaLocalHumidifier
  13. from .helpers.device_config import config_for_legacy_use
  14. _LOGGER = logging.getLogger(__name__)
  15. async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
  16. """Set up the Tuya device according to its type."""
  17. data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
  18. device = data["device"]
  19. if discovery_info[CONF_TYPE] == CONF_TYPE_AUTO:
  20. raise ValueError(
  21. f"Device type for {device.name} not resolved before humidifier init"
  22. )
  23. cfg = config_for_legacy_use(discovery_info[CONF_TYPE])
  24. ecfg = cfg.primary_entity
  25. if ecfg.entity != "humidifier":
  26. for ecfg in cfg.secondary_entities():
  27. if ecfg.entity == "humidifier":
  28. break
  29. if ecfg.entity != "humidifier":
  30. raise ValueError(
  31. f"{device.name} does not support use as a humidifier device."
  32. )
  33. if ecfg.deprecated:
  34. _LOGGER.warning(ecfg.deprecation_message)
  35. data[CONF_HUMIDIFIER] = TuyaLocalHumidifier(device, ecfg)
  36. async_add_entities([data[CONF_HUMIDIFIER]])
  37. _LOGGER.debug(f"Adding humidifier device for {discovery_info[CONF_TYPE]}")
  38. async def async_setup_entry(hass, config_entry, async_add_entities):
  39. config = {**config_entry.data, **config_entry.options}
  40. discovery_info = {
  41. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  42. CONF_TYPE: config[CONF_TYPE],
  43. }
  44. await async_setup_platform(hass, {}, async_add_entities, discovery_info)