lock.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. ecfg = cfg.primary_entity
  21. if ecfg.entity != "lock":
  22. for ecfg in cfg.secondary_entities():
  23. if ecfg.entity == "lock":
  24. break
  25. if ecfg.entity != "lock":
  26. raise ValueError(f"{device.name} does not support use as a lock device.")
  27. if ecfg.deprecated:
  28. _LOGGER.warning(ecfg.deprecation_message)
  29. data[CONF_LOCK] = TuyaLocalLock(device, ecfg)
  30. async_add_entities([data[CONF_LOCK]])
  31. _LOGGER.debug(f"Adding lock for {discovery_info[CONF_TYPE]}")
  32. async def async_setup_entry(hass, config_entry, async_add_entities):
  33. config = {**config_entry.data, **config_entry.options}
  34. discovery_info = {
  35. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  36. CONF_TYPE: config[CONF_TYPE],
  37. }
  38. await async_setup_platform(hass, {}, async_add_entities, discovery_info)