Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Support for SwitchBot lock platform."""
2 
3 from typing import Any
4 
5 import switchbot
6 from switchbot.const import LockStatus
7 
8 from homeassistant.components.lock import LockEntity, LockEntityFeature
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from .const import CONF_LOCK_NIGHTLATCH, DEFAULT_LOCK_NIGHTLATCH
13 from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
14 from .entity import SwitchbotEntity
15 
16 
18  hass: HomeAssistant,
19  entry: SwitchbotConfigEntry,
20  async_add_entities: AddEntitiesCallback,
21 ) -> None:
22  """Set up Switchbot lock based on a config entry."""
23  force_nightlatch = entry.options.get(CONF_LOCK_NIGHTLATCH, DEFAULT_LOCK_NIGHTLATCH)
24  async_add_entities([SwitchBotLock(entry.runtime_data, force_nightlatch)])
25 
26 
27 # noinspection PyAbstractClass
29  """Representation of a Switchbot lock."""
30 
31  _attr_translation_key = "lock"
32  _attr_name = None
33  _device: switchbot.SwitchbotLock
34 
35  def __init__(
36  self, coordinator: SwitchbotDataUpdateCoordinator, force_nightlatch
37  ) -> None:
38  """Initialize the entity."""
39  super().__init__(coordinator)
40  self._async_update_attrs_async_update_attrs_async_update_attrs()
41  if self._device_device.is_night_latch_enabled() or force_nightlatch:
42  self._attr_supported_features_attr_supported_features = LockEntityFeature.OPEN
43 
44  def _async_update_attrs(self) -> None:
45  """Update the entity attributes."""
46  status = self._device_device.get_lock_status()
47  self._attr_is_locked_attr_is_locked = status is LockStatus.LOCKED
48  self._attr_is_locking_attr_is_locking = status is LockStatus.LOCKING
49  self._attr_is_unlocking_attr_is_unlocking = status is LockStatus.UNLOCKING
50  self._attr_is_jammed_attr_is_jammed = status in {
51  LockStatus.LOCKING_STOP,
52  LockStatus.UNLOCKING_STOP,
53  }
54 
55  async def async_lock(self, **kwargs: Any) -> None:
56  """Lock the lock."""
57  self._last_run_success_last_run_success = await self._device_device.lock()
58  self.async_write_ha_stateasync_write_ha_state()
59 
60  async def async_unlock(self, **kwargs: Any) -> None:
61  """Unlock the lock."""
62  if self._attr_supported_features_attr_supported_features & (LockEntityFeature.OPEN):
63  self._last_run_success_last_run_success = await self._device_device.unlock_without_unlatch()
64  else:
65  self._last_run_success_last_run_success = await self._device_device.unlock()
66  self.async_write_ha_stateasync_write_ha_state()
67 
68  async def async_open(self, **kwargs: Any) -> None:
69  """Open the lock."""
70  self._last_run_success_last_run_success = await self._device_device.unlock()
71  self.async_write_ha_stateasync_write_ha_state()
None unlock(self, **Any kwargs)
Definition: __init__.py:241
None lock(self, **Any kwargs)
Definition: __init__.py:228
None __init__(self, SwitchbotDataUpdateCoordinator coordinator, force_nightlatch)
Definition: lock.py:37
None async_setup_entry(HomeAssistant hass, SwitchbotConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:21