Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Platform for Schlage switch integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from functools import partial
8 from typing import Any
9 
10 from pyschlage.lock import Lock
11 
13  SwitchDeviceClass,
14  SwitchEntity,
15  SwitchEntityDescription,
16 )
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.const import EntityCategory
19 from homeassistant.core import HomeAssistant
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from .const import DOMAIN
23 from .coordinator import LockData, SchlageDataUpdateCoordinator
24 from .entity import SchlageEntity
25 
26 
27 @dataclass(frozen=True, kw_only=True)
29  """Entity description for a Schlage switch."""
30 
31  on_fn: Callable[[Lock], None]
32  off_fn: Callable[[Lock], None]
33  value_fn: Callable[[Lock], bool]
34 
35 
36 SWITCHES: tuple[SchlageSwitchEntityDescription, ...] = (
38  key="beeper",
39  translation_key="beeper",
40  device_class=SwitchDeviceClass.SWITCH,
41  entity_category=EntityCategory.CONFIG,
42  on_fn=lambda lock: lock.set_beeper(True),
43  off_fn=lambda lock: lock.set_beeper(False),
44  value_fn=lambda lock: lock.beeper_enabled,
45  ),
47  key="lock_and_leve",
48  translation_key="lock_and_leave",
49  device_class=SwitchDeviceClass.SWITCH,
50  entity_category=EntityCategory.CONFIG,
51  on_fn=lambda lock: lock.set_lock_and_leave(True),
52  off_fn=lambda lock: lock.set_lock_and_leave(False),
53  value_fn=lambda lock: lock.lock_and_leave_enabled,
54  ),
55 )
56 
57 
59  hass: HomeAssistant,
60  config_entry: ConfigEntry,
61  async_add_entities: AddEntitiesCallback,
62 ) -> None:
63  """Set up switches based on a config entry."""
64  coordinator: SchlageDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
65 
66  def _add_new_locks(locks: dict[str, LockData]) -> None:
69  coordinator=coordinator,
70  description=description,
71  device_id=device_id,
72  )
73  for device_id in locks
74  for description in SWITCHES
75  )
76 
77  _add_new_locks(coordinator.data.locks)
78  coordinator.new_locks_callbacks.append(_add_new_locks)
79 
80 
82  """Schlage switch entity."""
83 
84  entity_description: SchlageSwitchEntityDescription
85 
86  def __init__(
87  self,
88  coordinator: SchlageDataUpdateCoordinator,
89  description: SchlageSwitchEntityDescription,
90  device_id: str,
91  ) -> None:
92  """Initialize a SchlageSwitch."""
93  super().__init__(coordinator=coordinator, device_id=device_id)
94  self.entity_descriptionentity_description = description
95  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{device_id}_{self.entity_description.key}"
96 
97  @property
98  def is_on(self) -> bool:
99  """Return True if the switch is on."""
100  return self.entity_descriptionentity_description.value_fn(self._lock_lock)
101 
102  async def async_turn_on(self, **kwargs: Any) -> None:
103  """Turn the switch on."""
104  await self.hasshasshass.async_add_executor_job(
105  partial(self.entity_descriptionentity_description.on_fn, self._lock_lock)
106  )
107  await self.coordinator.async_request_refresh()
108 
109  async def async_turn_off(self, **kwargs: Any) -> None:
110  """Turn the switch off."""
111  await self.hasshasshass.async_add_executor_job(
112  partial(self.entity_descriptionentity_description.off_fn, self._lock_lock)
113  )
114  await self.coordinator.async_request_refresh()
None __init__(self, SchlageDataUpdateCoordinator coordinator, SchlageSwitchEntityDescription description, str device_id)
Definition: switch.py:91
None _add_new_locks(dict[str, LockData] locks)
Definition: sensor.py:39
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:62