climate.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 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 Tuya device according to its type."""
  16. data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
  17. device = data["device"]
  18. climates = []
  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 == "climate" and discovery_info.get(ecfg.config_id, False):
  24. legacy_class = ecfg.legacy_class
  25. if legacy_class is None:
  26. data[ecfg.config_id] = TuyaLocalClimate(device, ecfg)
  27. else:
  28. data[ecfg.config_id] = legacy_class(device)
  29. climates.append(data[ecfg.config_id])
  30. if ecfg.deprecated:
  31. _LOGGER.warning(ecfg.deprecation_message)
  32. _LOGGER.debug(f"Adding climate for {ecfg.name}")
  33. for ecfg in cfg.secondary_entities():
  34. if ecfg.entity == "climate" and discovery_info.get(ecfg.config_id, False):
  35. legacy_class = ecfg.legacy_class
  36. if legacy_class is None:
  37. data[ecfg.config_id] = TuyaLocalClimate(device, ecfg)
  38. else:
  39. data[ecfg.config_id] = legacy_class(device)
  40. climates.append(data[ecfg.config_id])
  41. if ecfg.deprecated:
  42. _LOGGER.warning(ecfg.deprecation_message)
  43. _LOGGER.debug(f"Adding climate for {ecfg.name}")
  44. if not climates:
  45. raise ValueError(f"{device.name} does not support use as a climate device.")
  46. async_add_entities(climates)
  47. async def async_setup_entry(hass, config_entry, async_add_entities):
  48. config = {**config_entry.data, **config_entry.options}
  49. await async_setup_platform(hass, {}, async_add_entities, config)