lock.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """
  2. Platform to control Tuya lock devices.
  3. Initial implementation is based on the secondary child-lock feature of Goldair
  4. climate devices.
  5. """
  6. from homeassistant.components.lock import LockEntity, STATE_LOCKED, STATE_UNLOCKED
  7. from ..device import TuyaLocalDevice
  8. from ..helpers.device_config import TuyaEntityConfig
  9. from ..helpers.mixin import TuyaLocalEntity
  10. class TuyaLocalLock(TuyaLocalEntity, LockEntity):
  11. """Representation of a Tuya Wi-Fi connected lock."""
  12. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  13. """
  14. Initialise the lock.
  15. Args:
  16. device (TuyaLocalDevice): The device API instance.
  17. config (TuyaEntityConfig): The configuration for this entity.
  18. """
  19. dps_map = self._init_begin(device, config)
  20. self._lock_dps = dps_map.pop("lock")
  21. self._init_end(dps_map)
  22. @property
  23. def state(self):
  24. """Return the current state."""
  25. lock = self._lock_dps.get_value(self._device)
  26. if lock is None:
  27. return None
  28. else:
  29. return STATE_LOCKED if lock else STATE_UNLOCKED
  30. @property
  31. def is_locked(self):
  32. """Return the a boolean representing whether the lock is locked."""
  33. return self.state == STATE_LOCKED
  34. async def async_lock(self, **kwargs):
  35. """Lock the lock."""
  36. await self._lock_dps.async_set_value(self._device, True)
  37. async def async_unlock(self, **kwargs):
  38. """Unlock the lock."""
  39. await self._lock_dps.async_set_value(self._device, False)