Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Support for the Switchbot lock."""
2 
3 from typing import Any
4 
5 from switchbot_api import LockCommands
6 
7 from homeassistant.components.lock import LockEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from . import SwitchbotCloudData
13 from .const import DOMAIN
14 from .entity import SwitchBotCloudEntity
15 
16 
18  hass: HomeAssistant,
19  config: ConfigEntry,
20  async_add_entities: AddEntitiesCallback,
21 ) -> None:
22  """Set up SwitchBot Cloud entry."""
23  data: SwitchbotCloudData = hass.data[DOMAIN][config.entry_id]
25  SwitchBotCloudLock(data.api, device, coordinator)
26  for device, coordinator in data.devices.locks
27  )
28 
29 
31  """Representation of a SwitchBot lock."""
32 
33  _attr_name = None
34 
35  @callback
36  def _handle_coordinator_update(self) -> None:
37  """Handle updated data from the coordinator."""
38  if coord_data := self.coordinator.data:
39  self._attr_is_locked_attr_is_locked = coord_data["lockState"] == "locked"
40  self.async_write_ha_stateasync_write_ha_state()
41 
42  async def async_lock(self, **kwargs: Any) -> None:
43  """Lock the lock."""
44  await self.send_api_commandsend_api_command(LockCommands.LOCK)
45  self._attr_is_locked_attr_is_locked = True
46  self.async_write_ha_stateasync_write_ha_state()
47 
48  async def async_unlock(self, **kwargs: Any) -> None:
49  """Unlock the lock."""
50 
51  await self.send_api_commandsend_api_command(LockCommands.UNLOCK)
52  self._attr_is_locked_attr_is_locked = False
53  self.async_write_ha_stateasync_write_ha_state()
None send_api_command(self, Commands command, str command_type="command", dict|str parameters="default")
Definition: entity.py:43
None async_setup_entry(HomeAssistant hass, ConfigEntry config, AddEntitiesCallback async_add_entities)
Definition: lock.py:21