1 """Nuki.io lock platform."""
3 from __future__
import annotations
5 from abc
import abstractmethod
8 from pynuki
import NukiLock, NukiOpener
9 from pynuki.constants
import MODE_OPENER_CONTINUOUS
10 from pynuki.device
import NukiDevice
11 from requests.exceptions
import RequestException
12 import voluptuous
as vol
20 from .
import NukiEntryData
21 from .const
import ATTR_ENABLE, ATTR_UNLATCH, DOMAIN
as NUKI_DOMAIN, ERROR_STATES
22 from .entity
import NukiEntity
23 from .helpers
import CannotConnect
27 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
29 """Set up the Nuki lock platform."""
30 entry_data: NukiEntryData = hass.data[NUKI_DOMAIN][entry.entry_id]
31 coordinator = entry_data.coordinator
33 entities: list[NukiDeviceEntity] = [
41 platform = entity_platform.async_get_current_platform()
42 platform.async_register_entity_service(
45 vol.Optional(ATTR_UNLATCH, default=
False): cv.boolean,
50 platform.async_register_entity_service(
51 "set_continuous_mode",
53 vol.Required(ATTR_ENABLE): cv.boolean,
55 "set_continuous_mode",
59 class NukiDeviceEntity[_NukiDeviceT: NukiDevice](NukiEntity[_NukiDeviceT], LockEntity):
60 """Representation of a Nuki device."""
62 _attr_has_entity_name =
True
63 _attr_supported_features = LockEntityFeature.OPEN
64 _attr_translation_key =
"nuki_lock"
69 """Return a unique ID."""
70 return self._nuki_device.nuki_id
74 """Return True if entity is available."""
75 return super().available
and self._nuki_device.state
not in ERROR_STATES
78 def lock(self, **kwargs: Any) ->
None:
79 """Lock the device."""
82 def unlock(self, **kwargs: Any) ->
None:
83 """Unlock the device."""
86 def open(self, **kwargs: Any) ->
None:
87 """Open the door latch."""
91 """Representation of a Nuki lock."""
95 """Return true if lock is locked."""
96 return self._nuki_device.is_locked
98 def lock(self, **kwargs: Any) ->
None:
99 """Lock the device."""
101 self._nuki_device.
lock()
102 except RequestException
as err:
103 raise CannotConnect
from err
106 """Unlock the device."""
108 self._nuki_device.
unlock()
109 except RequestException
as err:
110 raise CannotConnect
from err
112 def open(self, **kwargs: Any) ->
None:
113 """Open the door latch."""
115 self._nuki_device.unlatch()
116 except RequestException
as err:
117 raise CannotConnect
from err
122 This will first unlock the door, then wait for 20 seconds (or another
123 amount of time depending on the lock settings) and relock.
127 except RequestException
as err:
128 raise CannotConnect
from err
132 """Representation of a Nuki opener."""
136 """Return true if either ring-to-open or continuous mode is enabled."""
138 self._nuki_device.is_rto_activated
139 or self._nuki_device.mode == MODE_OPENER_CONTINUOUS
142 def lock(self, **kwargs: Any) ->
None:
143 """Disable ring-to-open."""
145 self._nuki_device.deactivate_rto()
146 except RequestException
as err:
147 raise CannotConnect
from err
150 """Enable ring-to-open."""
152 self._nuki_device.activate_rto()
153 except RequestException
as err:
154 raise CannotConnect
from err
156 def open(self, **kwargs: Any) ->
None:
157 """Buzz open the door."""
159 self._nuki_device.electric_strike_actuation()
160 except RequestException
as err:
161 raise CannotConnect
from err
169 This feature will cause the door to automatically open when anyone
170 rings the bell. This is similar to ring-to-open, except that it does
171 not automatically deactivate
175 self._nuki_device.activate_continuous_mode()
177 self._nuki_device.deactivate_continuous_mode()
178 except RequestException
as err:
179 raise CannotConnect
from err
None open(self, **Any kwargs)
None lock_n_go(self, bool unlatch)
None lock(self, **Any kwargs)
None unlock(self, **Any kwargs)
None lock_n_go(self, bool unlatch)
None unlock(self, **Any kwargs)
None lock(self, **Any kwargs)
None set_continuous_mode(self, bool enable)
None open(self, **Any kwargs)
None open(self, **Any kwargs)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
None lock(self, **Any kwargs)
None unlock(self, **Any kwargs)