humidifier.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. )
  11. from .generic.humidifier import TuyaLocalHumidifier
  12. from .helpers.device_config import config_for_legacy_use
  13. _LOGGER = logging.getLogger(__name__)
  14. async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
  15. """Set up the Tuya device according to its type."""
  16. data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
  17. device = data["device"]
  18. cfg = config_for_legacy_use(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 != "humidifier":
  23. for ecfg in cfg.secondary_entities():
  24. if ecfg.entity == "humidifier":
  25. break
  26. if ecfg.entity != "humidifier":
  27. raise ValueError(
  28. f"{device.name} does not support use as a humidifier device."
  29. )
  30. if ecfg.deprecated:
  31. _LOGGER.warning(ecfg.deprecation_message)
  32. data[CONF_HUMIDIFIER] = TuyaLocalHumidifier(device, ecfg)
  33. async_add_entities([data[CONF_HUMIDIFIER]])
  34. _LOGGER.debug(f"Adding humidifier device for {discovery_info[CONF_TYPE]}")
  35. async def async_setup_entry(hass, config_entry, async_add_entities):
  36. config = {**config_entry.data, **config_entry.options}
  37. discovery_info = {
  38. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  39. CONF_TYPE: config[CONF_TYPE],
  40. }
  41. await async_setup_platform(hass, {}, async_add_entities, discovery_info)