climate.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. Setup for different kinds of Tuya climate devices
  3. """
  4. import logging
  5. from . import DOMAIN
  6. from .const import (
  7. CONF_CLIMATE,
  8. CONF_DEVICE_ID,
  9. CONF_TYPE,
  10. )
  11. from .generic.climate import TuyaLocalClimate
  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 != "climate":
  23. for ecfg in cfg.secondary_entities():
  24. if ecfg.entity == "climate":
  25. break
  26. if ecfg.entity != "climate":
  27. raise ValueError(f"{device.name} does not support use as a climate device.")
  28. if ecfg.deprecated:
  29. _LOGGER.warning(ecfg.deprecation_message)
  30. legacy_class = ecfg.legacy_class
  31. # Transition: generic climate entity exists, but is not complete. More
  32. # complex climate devices still need a device specific class.
  33. # If legacy_class exists, use it, otherwise use the generic climate class.
  34. if legacy_class is not None:
  35. data[CONF_CLIMATE] = legacy_class(device)
  36. else:
  37. data[CONF_CLIMATE] = TuyaLocalClimate(device, ecfg)
  38. async_add_entities([data[CONF_CLIMATE]])
  39. _LOGGER.debug(f"Adding climate device for {discovery_info[CONF_TYPE]}")
  40. async def async_setup_entry(hass, config_entry, async_add_entities):
  41. config = {**config_entry.data, **config_entry.options}
  42. discovery_info = {
  43. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  44. CONF_TYPE: config[CONF_TYPE],
  45. }
  46. await async_setup_platform(hass, {}, async_add_entities, discovery_info)