lock.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """
  2. Setup for different kinds of Tuya lock devices
  3. """
  4. import logging
  5. from . import DOMAIN
  6. from .const import (
  7. CONF_DEVICE_ID,
  8. CONF_LOCK,
  9. CONF_TYPE,
  10. )
  11. from .generic.lock import TuyaLocalLock
  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 lock device according to its type."""
  16. _LOGGER.debug(f"Domain data: {hass.data[DOMAIN]}")
  17. data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
  18. device = data["device"]
  19. cfg = config_for_legacy_use(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 != "lock":
  24. for ecfg in cfg.secondary_entities():
  25. if ecfg.entity == "lock":
  26. break
  27. if ecfg.entity != "lock":
  28. raise ValueError(f"{device.name} does not support use as a lock device.")
  29. if ecfg.deprecated:
  30. _LOGGER.warning(ecfg.deprecation_message)
  31. data[CONF_LOCK] = TuyaLocalLock(device, ecfg)
  32. async_add_entities([data[CONF_LOCK]])
  33. _LOGGER.debug(f"Adding lock for {discovery_info[CONF_TYPE]}")
  34. async def async_setup_entry(hass, config_entry, async_add_entities):
  35. config = {**config_entry.data, **config_entry.options}
  36. discovery_info = {
  37. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  38. CONF_TYPE: config[CONF_TYPE],
  39. }
  40. await async_setup_platform(hass, {}, async_add_entities, discovery_info)