lock.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. dps_map = {c.name: c for c in config.dps()}
  23. self._lock_dps = dps_map.pop("lock")
  24. for d in dps_map.values():
  25. if not d.hidden:
  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 icon(self):
  48. """Return the icon to use in the frontend for this device."""
  49. icon = self._config.icon(self._device)
  50. if icon:
  51. return icon
  52. else:
  53. return super().icon
  54. @property
  55. def state(self):
  56. """Return the current state."""
  57. lock = self._lock_dps.get_value(self._device)
  58. if lock is None:
  59. return STATE_UNAVAILABLE
  60. else:
  61. return STATE_LOCKED if lock else STATE_UNLOCKED
  62. @property
  63. def is_locked(self):
  64. """Return the a boolean representing whether the lock is locked."""
  65. return self.state == STATE_LOCKED
  66. @property
  67. def device_state_attributes(self):
  68. """Get additional attributes that the integration itself does not support."""
  69. attr = {}
  70. for a in self._attr_dps:
  71. attr[a.name] = a.get_value(self._device)
  72. return attr
  73. async def async_lock(self, **kwargs):
  74. """Lock the lock."""
  75. await self._lock_dps.async_set_value(self._device, True)
  76. async def async_unlock(self, **kwargs):
  77. """Unlock the lock."""
  78. await self._lock_dps.async_set_value(self._device, False)
  79. async def async_update(self):
  80. await self._device.async_refresh()