lock.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_TYPE,
  9. )
  10. from .generic.lock import TuyaLocalLock
  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 lock device according to its type."""
  15. _LOGGER.debug(f"Domain data: {hass.data[DOMAIN]}")
  16. data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
  17. device = data["device"]
  18. locks = []
  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 == "lock" and (
  24. discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
  25. ):
  26. data[ecfg.config_id] = TuyaLocalLock(device, ecfg)
  27. locks.append(data[ecfg.config_id])
  28. if ecfg.deprecated:
  29. _LOGGER.warning(ecfg.deprecation_message)
  30. _LOGGER.debug(f"Adding lock for {ecfg.name}")
  31. for ecfg in cfg.secondary_entities():
  32. if ecfg.entity == "lock" and (
  33. discovery_info.get(ecfg.config_id, False) or not ecfg.deprecated
  34. ):
  35. data[ecfg.config_id] = TuyaLocalLock(device, ecfg)
  36. locks.append(data[ecfg.config_id])
  37. if ecfg.deprecated:
  38. _LOGGER.warning(ecfg.deprecation_message)
  39. _LOGGER.debug(f"Adding lock for {ecfg.name}")
  40. if not locks:
  41. raise ValueError(f"{device.name} does not support use as a lock device.")
  42. async_add_entities(locks)
  43. async def async_setup_entry(hass, config_entry, async_add_entities):
  44. config = {**config_entry.data, **config_entry.options}
  45. await async_setup_platform(hass, {}, async_add_entities, config)