Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Switches for Yale Alarm."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from yalesmartalarmclient import YaleLock
8 
9 from homeassistant.components.switch import SwitchEntity
10 from homeassistant.core import HomeAssistant, callback
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from . import YaleConfigEntry
14 from .coordinator import YaleDataUpdateCoordinator
15 from .entity import YaleLockEntity
16 
17 
19  hass: HomeAssistant, entry: YaleConfigEntry, async_add_entities: AddEntitiesCallback
20 ) -> None:
21  """Set up the Yale switch entry."""
22 
23  coordinator = entry.runtime_data
24 
26  YaleAutolockSwitch(coordinator, lock)
27  for lock in coordinator.locks
28  if lock.supports_lock_config()
29  )
30 
31 
33  """Representation of a Yale autolock switch."""
34 
35  _attr_translation_key = "autolock"
36 
37  def __init__(self, coordinator: YaleDataUpdateCoordinator, lock: YaleLock) -> None:
38  """Initialize the Yale Autolock Switch."""
39  super().__init__(coordinator, lock)
40  self._attr_unique_id_attr_unique_id = f"{lock.sid()}-autolock"
41  self._attr_is_on_attr_is_on = self.lock_datalock_data.autolock()
42 
43  async def async_turn_on(self, **kwargs: Any) -> None:
44  """Turn the entity on."""
45  if await self.hasshasshass.async_add_executor_job(self.lock_datalock_data.set_autolock, True):
46  self._attr_is_on_attr_is_on = True
47  self.async_write_ha_stateasync_write_ha_state()
48 
49  async def async_turn_off(self, **kwargs: Any) -> None:
50  """Turn the entity off."""
51  if await self.hasshasshass.async_add_executor_job(self.lock_datalock_data.set_autolock, False):
52  self._attr_is_on_attr_is_on = False
53  self.async_write_ha_stateasync_write_ha_state()
54 
55  @callback
56  def _handle_coordinator_update(self) -> None:
57  """Handle updated data from the coordinator."""
58  self._attr_is_on_attr_is_on = self.lock_datalock_data.autolock()
None __init__(self, YaleDataUpdateCoordinator coordinator, YaleLock lock)
Definition: switch.py:37
None async_setup_entry(HomeAssistant hass, YaleConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:20