lock.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 friendly name for this entity."""
  33. return self._config.name(self._device.name)
  34. @property
  35. def unique_id(self):
  36. """Return the device unique ID."""
  37. return self._config.unique_id(self._device.unique_id)
  38. @property
  39. def device_info(self):
  40. """Return the device information."""
  41. return self._device.device_info
  42. @property
  43. def icon(self):
  44. """Return the icon to use in the frontend for this device."""
  45. icon = self._config.icon(self._device)
  46. if icon:
  47. return icon
  48. else:
  49. return super().icon
  50. @property
  51. def state(self):
  52. """Return the current state."""
  53. lock = self._lock_dps.get_value(self._device)
  54. if lock is None:
  55. return STATE_UNAVAILABLE
  56. else:
  57. return STATE_LOCKED if lock else STATE_UNLOCKED
  58. @property
  59. def is_locked(self):
  60. """Return the a boolean representing whether the lock is locked."""
  61. return self.state == STATE_LOCKED
  62. @property
  63. def device_state_attributes(self):
  64. """Get additional attributes that the integration itself does not support."""
  65. attr = {}
  66. for a in self._attr_dps:
  67. attr[a.name] = a.get_value(self._device)
  68. return attr
  69. async def async_lock(self, **kwargs):
  70. """Lock the lock."""
  71. await self._lock_dps.async_set_value(self._device, True)
  72. async def async_unlock(self, **kwargs):
  73. """Unlock the lock."""
  74. await self._lock_dps.async_set_value(self._device, False)
  75. async def async_update(self):
  76. await self._device.async_refresh()