lock.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. class TuyaLocalLock(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. self._device = device
  20. self._config = config
  21. self._attr_dps = []
  22. for d in config.dps():
  23. if d.name == "lock":
  24. self._lock_dps = d
  25. else:
  26. self._attr_dps.append(d)
  27. @property
  28. def should_poll(self):
  29. return True
  30. @property
  31. def name(self):
  32. """Return the name for this entity."""
  33. return self._device.name
  34. @property
  35. def friendly_name(self):
  36. """Return the friendly name for this entity."""
  37. return self._config.name
  38. @property
  39. def unique_id(self):
  40. """Return the device unique ID."""
  41. return self._device.unique_id
  42. @property
  43. def device_info(self):
  44. """Return the device information."""
  45. return self._device.device_info
  46. @property
  47. def state(self):
  48. """Return the current state."""
  49. lock = self._lock_dps.get_value(self._device)
  50. if lock is None:
  51. return STATE_UNAVAILABLE
  52. else:
  53. return STATE_LOCKED if lock else STATE_UNLOCKED
  54. @property
  55. def is_locked(self):
  56. """Return the a boolean representing whether the lock is locked."""
  57. return self.state == STATE_LOCKED
  58. @property
  59. def device_state_attributes(self):
  60. """Get additional attributes that the integration itself does not support."""
  61. attr = {}
  62. for a in self._attr_dps:
  63. attr[a.name] = a.get_value(self._device)
  64. return attr
  65. async def async_lock(self, **kwargs):
  66. """Lock the lock."""
  67. await self._lock_dps.async_set_value(self._device, True)
  68. async def async_unlock(self, **kwargs):
  69. """Unlock the lock."""
  70. await self._lock_dps.async_set_value(self._device, False)
  71. async def async_update(self):
  72. await self._device.async_refresh()