climate.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """
  2. Setup for different kinds of Tuya climate devices
  3. """
  4. import logging
  5. from . import DOMAIN
  6. from .const import (
  7. CONF_DEVICE_ID,
  8. CONF_TYPE,
  9. )
  10. from .generic.climate import TuyaLocalClimate
  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 Tuya device according to its type."""
  15. data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
  16. device = data["device"]
  17. climates = []
  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 == "climate" and discovery_info.get(ecfg.config_id, False):
  23. legacy_class = ecfg.legacy_class
  24. if legacy_class is None:
  25. data[ecfg.config_id] = TuyaLocalClimate(device, ecfg)
  26. else:
  27. data[ecfg.config_id] = legacy_class(device)
  28. climates.append(data[ecfg.config_id])
  29. if ecfg.deprecated:
  30. _LOGGER.warning(ecfg.deprecation_message)
  31. _LOGGER.debug(f"Adding climate for {ecfg.name}")
  32. for ecfg in cfg.secondary_entities():
  33. if ecfg.entity == "climate" and discovery_info.get(ecfg.config_id, False):
  34. legacy_class = ecfg.legacy_class
  35. if legacy_class is None:
  36. data[ecfg.config_id] = TuyaLocalClimate(device, ecfg)
  37. else:
  38. data[ecfg.config_id] = legacy_class(device)
  39. climates.append(data[ecfg.config_id])
  40. if ecfg.deprecated:
  41. _LOGGER.warning(ecfg.deprecation_message)
  42. _LOGGER.debug(f"Adding climate for {ecfg.name}")
  43. if not climates:
  44. raise ValueError(f"{device.name} does not support use as a climate device.")
  45. async_add_entities(climates)
  46. async def async_setup_entry(hass, config_entry, async_add_entities):
  47. config = {**config_entry.data, **config_entry.options}
  48. await async_setup_platform(hass, {}, async_add_entities, config)