Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Representation of a switchBinary."""
2 
3 import logging
4 from typing import Any
5 
7  SwitchDeviceClass,
8  SwitchEntity,
9  SwitchEntityDescription,
10 )
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers.dispatcher import async_dispatcher_connect
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import DOMAIN, ZWaveMePlatform
17 from .entity import ZWaveMeEntity
18 
19 _LOGGER = logging.getLogger(__name__)
20 DEVICE_NAME = ZWaveMePlatform.SWITCH
21 
22 SWITCH_MAP: dict[str, SwitchEntityDescription] = {
23  "generic": SwitchEntityDescription(
24  key="generic",
25  device_class=SwitchDeviceClass.SWITCH,
26  )
27 }
28 
29 
31  hass: HomeAssistant,
32  config_entry: ConfigEntry,
33  async_add_entities: AddEntitiesCallback,
34 ) -> None:
35  """Set up the switch platform."""
36 
37  @callback
38  def add_new_device(new_device):
39  controller = hass.data[DOMAIN][config_entry.entry_id]
40  switch = ZWaveMeSwitch(controller, new_device, SWITCH_MAP["generic"])
41 
43  [
44  switch,
45  ]
46  )
47 
48  config_entry.async_on_unload(
50  hass, f"ZWAVE_ME_NEW_{DEVICE_NAME.upper()}", add_new_device
51  )
52  )
53 
54 
56  """Representation of a ZWaveMe binary switch."""
57 
58  def __init__(self, controller, device, description):
59  """Initialize the device."""
60  super().__init__(controller, device)
61  self.entity_descriptionentity_description = description
62 
63  @property
64  def is_on(self) -> bool:
65  """Return the state of the switch."""
66  return self.devicedevice.level == "on"
67 
68  def turn_on(self, **kwargs: Any) -> None:
69  """Turn the entity on."""
70  self.controllercontroller.zwave_api.send_command(self.devicedevice.id, "on")
71 
72  def turn_off(self, **kwargs: Any) -> None:
73  """Turn the entity off."""
74  self.controllercontroller.zwave_api.send_command(self.devicedevice.id, "off")
def __init__(self, controller, device, description)
Definition: switch.py:58
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:34
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103