Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for LightwaveRF switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.switch import SwitchEntity
8 from homeassistant.const import CONF_NAME
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
12 
13 from . import LIGHTWAVE_LINK
14 
15 
17  hass: HomeAssistant,
18  config: ConfigType,
19  async_add_entities: AddEntitiesCallback,
20  discovery_info: DiscoveryInfoType | None = None,
21 ) -> None:
22  """Find and return LightWave switches."""
23  if not discovery_info:
24  return
25 
26  switches = []
27  lwlink = hass.data[LIGHTWAVE_LINK]
28 
29  for device_id, device_config in discovery_info.items():
30  name = device_config[CONF_NAME]
31  switches.append(LWRFSwitch(name, device_id, lwlink))
32 
33  async_add_entities(switches)
34 
35 
37  """Representation of a LightWaveRF switch."""
38 
39  _attr_should_poll = False
40 
41  def __init__(self, name, device_id, lwlink):
42  """Initialize LWRFSwitch entity."""
43  self._attr_name_attr_name = name
44  self._device_id_device_id = device_id
45  self._lwlink_lwlink = lwlink
46 
47  async def async_turn_on(self, **kwargs: Any) -> None:
48  """Turn the LightWave switch on."""
49  self._attr_is_on_attr_is_on = True
50  self._lwlink_lwlink.turn_on_switch(self._device_id_device_id, self._attr_name_attr_name)
51  self.async_write_ha_stateasync_write_ha_state()
52 
53  async def async_turn_off(self, **kwargs: Any) -> None:
54  """Turn the LightWave switch off."""
55  self._attr_is_on_attr_is_on = False
56  self._lwlink_lwlink.turn_off(self._device_id_device_id, self._attr_name_attr_name)
57  self.async_write_ha_stateasync_write_ha_state()
def __init__(self, name, device_id, lwlink)
Definition: switch.py:41
None turn_off(self, **Any kwargs)
Definition: entity.py:1705
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:21