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_CLIMATE,
  8. CONF_DEVICE_ID,
  9. CONF_TYPE,
  10. CONF_TYPE_AUTO,
  11. )
  12. from .generic.climate import TuyaLocalClimate
  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. discovery_info[CONF_TYPE] = await device.async_inferred_type()
  21. if discovery_info[CONF_TYPE] is None:
  22. raise ValueError(f"Unable to detect type for device {device.name}")
  23. cfg = config_for_legacy_use(discovery_info[CONF_TYPE])
  24. ecfg = cfg.primary_entity
  25. if ecfg.entity != "climate":
  26. for ecfg in cfg.secondary_entities():
  27. if ecfg.entity == "climate":
  28. break
  29. if ecfg.entity != "climate":
  30. raise ValueError(f"{device.name} does not support use as a climate device.")
  31. legacy_class = ecfg.legacy_class
  32. # Transition: generic climate entity exists, but is not complete. More
  33. # complex climate devices still need a device specific class.
  34. # If legacy_class exists, use it, otherwise use the generic climate class.
  35. if legacy_class is not None:
  36. data[CONF_CLIMATE] = legacy_class(device)
  37. else:
  38. data[CONF_CLIMATE] = TuyaLocalClimate(device, ecfg)
  39. async_add_entities([data[CONF_CLIMATE]])
  40. _LOGGER.debug(f"Adding climate device for {discovery_info[CONF_TYPE]}")
  41. async def async_setup_entry(hass, config_entry, async_add_entities):
  42. config = {**config_entry.data, **config_entry.options}
  43. discovery_info = {
  44. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  45. CONF_TYPE: config[CONF_TYPE],
  46. }
  47. await async_setup_platform(hass, {}, async_add_entities, discovery_info)