lock.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """
  2. Setup for different kinds of Tuya climate devices
  3. """
  4. import logging
  5. from . import DOMAIN
  6. from .const import (
  7. CONF_CHILD_LOCK,
  8. CONF_DEVICE_ID,
  9. CONF_TYPE,
  10. CONF_TYPE_AUTO,
  11. )
  12. from .generic.lock import TuyaLocalLock
  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 lock device according to its type."""
  17. _LOGGER.debug(f"Domain data: {hass.data[DOMAIN]}")
  18. data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
  19. device = data["device"]
  20. if discovery_info[CONF_TYPE] == CONF_TYPE_AUTO:
  21. discovery_info[CONF_TYPE] = await device.async_inferred_type()
  22. if discovery_info[CONF_TYPE] is None:
  23. raise ValueError(f"Unable to detect type for device {device.name}")
  24. cfg = config_for_legacy_use(discovery_info[CONF_TYPE])
  25. ecfg = cfg.primary_entity
  26. if ecfg.entity != "lock":
  27. for ecfg in cfg.secondary_entities():
  28. if ecfg.entity == "lock":
  29. break
  30. if ecfg.entity != "lock":
  31. raise ValueError(f"{device.name} does not support use as a lock device.")
  32. data[CONF_CHILD_LOCK] = TuyaLocalLock(device, ecfg)
  33. async_add_entities([data[CONF_CHILD_LOCK]])
  34. _LOGGER.debug(f"Adding lock 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)