lock.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 homeassistant.const import STATE_UNAVAILABLE
  8. from ..device import TuyaLocalDevice
  9. from ..helpers.device_config import TuyaEntityConfig
  10. from ..helpers.mixin import TuyaLocalEntity
  11. class TuyaLocalLock(TuyaLocalEntity, LockEntity):
  12. """Representation of a Tuya Wi-Fi connected lock."""
  13. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  14. """
  15. Initialise the lock.
  16. Args:
  17. device (TuyaLocalDevice): The device API instance.
  18. config (TuyaEntityConfig): The configuration for this entity.
  19. """
  20. dps_map = self._init_begin(device, config)
  21. self._lock_dps = dps_map.pop("lock")
  22. self._init_end(dps_map)
  23. @property
  24. def state(self):
  25. """Return the current state."""
  26. lock = self._lock_dps.get_value(self._device)
  27. if lock is None:
  28. return STATE_UNAVAILABLE
  29. else:
  30. return STATE_LOCKED if lock else STATE_UNLOCKED
  31. @property
  32. def is_locked(self):
  33. """Return the a boolean representing whether the lock is locked."""
  34. return self.state == STATE_LOCKED
  35. async def async_lock(self, **kwargs):
  36. """Lock the lock."""
  37. await self._lock_dps.async_set_value(self._device, True)
  38. async def async_unlock(self, **kwargs):
  39. """Unlock the lock."""
  40. await self._lock_dps.async_set_value(self._device, False)