Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Lock support for switch entities."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN, LockEntity
8 from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import (
11  ATTR_ENTITY_ID,
12  CONF_ENTITY_ID,
13  SERVICE_TURN_OFF,
14  SERVICE_TURN_ON,
15  STATE_ON,
16 )
17 from homeassistant.core import Event, EventStateChangedData, HomeAssistant, callback
18 from homeassistant.helpers import entity_registry as er
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from .const import CONF_INVERT
22 from .entity import BaseInvertableEntity
23 
24 
26  hass: HomeAssistant,
27  config_entry: ConfigEntry,
28  async_add_entities: AddEntitiesCallback,
29 ) -> None:
30  """Initialize Lock Switch config entry."""
31  registry = er.async_get(hass)
32  entity_id = er.async_validate_entity_id(
33  registry, config_entry.options[CONF_ENTITY_ID]
34  )
35 
37  [
38  LockSwitch(
39  hass,
40  config_entry.title,
41  LOCK_DOMAIN,
42  config_entry.options[CONF_INVERT],
43  entity_id,
44  config_entry.entry_id,
45  )
46  ]
47  )
48 
49 
50 class LockSwitch(BaseInvertableEntity, LockEntity):
51  """Represents a Switch as a Lock."""
52 
53  async def async_lock(self, **kwargs: Any) -> None:
54  """Lock the lock."""
55  await self.hasshass.services.async_call(
56  SWITCH_DOMAIN,
57  SERVICE_TURN_ON if self._invert_state_invert_state else SERVICE_TURN_OFF,
58  {ATTR_ENTITY_ID: self._switch_entity_id_switch_entity_id},
59  blocking=True,
60  context=self._context_context,
61  )
62 
63  async def async_unlock(self, **kwargs: Any) -> None:
64  """Unlock the lock."""
65  await self.hasshass.services.async_call(
66  SWITCH_DOMAIN,
67  SERVICE_TURN_OFF if self._invert_state_invert_state else SERVICE_TURN_ON,
68  {ATTR_ENTITY_ID: self._switch_entity_id_switch_entity_id},
69  blocking=True,
70  context=self._context_context,
71  )
72 
73  @callback
75  self, event: Event[EventStateChangedData] | None = None
76  ) -> None:
77  """Handle child updates."""
78  super().async_state_changed_listener(event)
79  if (
80  not self.availableavailable
81  or (state := self.hasshass.states.get(self._switch_entity_id_switch_entity_id)) is None
82  ):
83  return
84 
85  # Logic is the same as the lock device class for binary sensors
86  # on means open (unlocked), off means closed (locked)
87  if self._invert_state_invert_state:
88  self._attr_is_locked_attr_is_locked = state.state == STATE_ON
89  else:
90  self._attr_is_locked_attr_is_locked = state.state != STATE_ON
None async_state_changed_listener(self, Event[EventStateChangedData]|None event=None)
Definition: entity.py:70
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:29